Um texto é constituído por palavras. Quando o texto é apresentado, através do método render
, cada palavra pode aparecer sem qualquer modificação de aspecto (utiliza-se o método render
correspondente). É ainda possível modificar dinamicamente o aspecto das palavras, permitindo que sejam apresentadas em negrito, itálico e sublinhado, ou ainda em combinações variadas, como por exemplo, negrito e itálico ou itálico sublinhado. No entanto, a apresentação é sempre realizada da mesma forma, através do método render
.
a. Qual o padrão de projeto mais adequado para ser usado no desenvolvimento deste sistema?
Decorator
b. Apresente o diagrama de classes em UML da solução proposta.
c. Apresente a codificação na linguagem de programação Java da solução proposta.
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
*************************************************************************/
package com.ybadoo.tutoriais.poo;
/**
* Component - define a interface para objetos que podem ter
* responsabilidades acrescentadas aos mesmos dinamicamente
*/
public interface Text
{
/**
* Renderizar o texto
*
* @return texto renderizado
*/
public String render();
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
*************************************************************************/
package com.ybadoo.tutoriais.poo;
/**
* ConcreteComponent - define um objeto para o qual
* responsabilidades adicionais podem ser atribuidas
*/
public class Word implements Text
{
/**
* Palavra
*/
private String word;
/**
* Construtor para inicializar a palavra
*
* @param word palavra
*/
public Word(String word)
{
this.word = word;
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.TextStyle#render()
*/
public String render()
{
return word;
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
*************************************************************************/
package com.ybadoo.tutoriais.poo;
/**
* Decorator - mantem uma referencia para um objeto Component
* e define uma interface que segue a interface de Component
*/
public class Decorator implements Text
{
/**
* Referencia para um objeto Component
*/
private Text component;
/**
* Construtor para inicializar o componente
*
* @param component referencia para um objeto Component
*/
public Decorator(Text component)
{
this.component = component;
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.TextStyle#render()
*/
public String render()
{
return component.render();
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
*************************************************************************/
package com.ybadoo.tutoriais.poo;
/**
* ConcreteDecorator - acrescenta responsabilidades ao componente
*/
public class BoldDecorator extends Decorator
{
/**
* Construtor para inicializar o componente
*
* @param component referencia para um objeto Component
*/
public BoldDecorator(Text component)
{
super(component);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.TextStyle#render()
*/
public String render()
{
return "<b>" + super.render() + "</b>";
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
*************************************************************************/
package com.ybadoo.tutoriais.poo;
/**
* ConcreteDecorator - acrescenta responsabilidades ao componente
*/
public class ItalicDecorator extends Decorator
{
/**
* Construtor para inicializar o componente
*
* @param component referencia para um objeto Component
*/
public ItalicDecorator(Text component)
{
super(component);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.TextStyle#render()
*/
public String render()
{
return "<i>" + super.render() + "</i>";
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
*************************************************************************/
package com.ybadoo.tutoriais.poo;
/**
* ConcreteDecorator - acrescenta responsabilidades ao componente
*/
public class UnderlinedDecorator extends Decorator
{
/**
* Construtor para inicializar o componente
*
* @param component referencia para um objeto Component
*/
public UnderlinedDecorator(Text component)
{
super(component);
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.TextStyle#render()
*/
public String render()
{
return "<s>" + super.render() + "</s>";
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
*************************************************************************/
package com.ybadoo.tutoriais.poo;
import java.util.LinkedList;
import java.util.List;
/**
* Classe responsavel pela representacao de uma frase
*/
public class Phrase
{
/**
* Palavras que compoem a frase
*/
private List<Text> words;
/**
* Construtor padrao
*/
public Phrase()
{
words = new LinkedList<Text>();
}
/**
* Adicionar uma palavra a frase
*
* @param word palavra a ser adicionada a frase
*/
public void add(Text word)
{
words.add(word);
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString()
{
StringBuilder str = new StringBuilder();
for(Text word: words)
{
str.append(word.render()).append(' ');
}
return str.toString();
}
}
/*************************************************************************
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br) *
* Ybadoo - Solucoes em Software Livre (ybadoo.com.br) *
* *
* Permission is granted to copy, distribute and/or modify this document *
* under the terms of the GNU Free Documentation License, Version 1.3 or *
* any later version published by the Free Software Foundation; with no *
* Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A *
* A copy of the license is included in the section entitled "GNU Free *
* Documentation License". *
*************************************************************************/
package com.ybadoo.tutoriais.poo;
import java.util.Map;
/**
* Classe responsavel pela execucao do padrao Decorator
*/
public class Application
{
/**
* Metodo principal da linguagem de programacao Java
*
* @param args argumentos da linha de comando (nao utilizado)
*/
public static void main(String[] args)
{
Phrase phrase = new Phrase();
phrase.add(new Word("normal"));
phrase.add(new BoldDecorator(new Word("negrito")));
phrase.add(new ItalicDecorator(new Word("itálico")));
phrase.add(new UnderlinedDecorator(new Word("sublinhado")));
phrase.add(new BoldDecorator(
new ItalicDecorator(new Word("negrito e itálico"))));
phrase.add(new ItalicDecorator(
new UnderlinedDecorator(new Word("itálico sublinhado"))));
System.out.println(phrase);
}
}