Ybadoo - Soluções em Software Livre
Tutoriais
Programação Orientada a Objetos

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.

  1. Qual o padrão de projeto mais adequado para ser usado no desenvolvimento deste sistema?
  2. Apresente o diagrama de classes em UML da solução proposta.
  3. Apresente a codificação na linguagem de programação Java da solução proposta.

 

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.

Diagrama de classes
Diagrama de classes da solução em Java

c. Apresente a codificação na linguagem de programação Java da solução proposta.

Arquivo Text.java

/*************************************************************************
 * Copyright (C) 2009/2024 - 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();
}

Arquivo Word.java

/*************************************************************************
 * Copyright (C) 2009/2024 - 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;
  }
}

Arquivo Decorator.java

/*************************************************************************
 * Copyright (C) 2009/2024 - 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();
  }
}

Arquivo BoldDecorator.java

/*************************************************************************
 * Copyright (C) 2009/2024 - 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>";
  }
}

Arquivo ItalicDecorator.java

/*************************************************************************
 * Copyright (C) 2009/2024 - 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>";
  }
}

Arquivo UnderlinedDecorator.java

/*************************************************************************
 * Copyright (C) 2009/2024 - 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>";
  }
}

Arquivo Phrase.java

/*************************************************************************
 * Copyright (C) 2009/2024 - 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();
  }
}

Arquivo Application.java

/*************************************************************************
 * Copyright (C) 2009/2024 - 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);
  }
}

Saída da Solução em Java

normal <b>negrito</b> <i>itálico</i> <s>sublinhado</s> <b><i>negrito e itálico</i></b> <i><s>itálico sublinhado</s></i>