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

Considere os seguintes conceitos do mundo real: pizzaria, pizzaiolo, pizza, consumidor. Considere ainda que em uma determinada pizzaria, dois pizzaiolos se alternam. Um deles trabalha segundas, quartas e sextas e só sabe fazer pizza de calabresa (queijo + calabresa + tomate), o outro trabalha terças, quintas e sábados e só sabe fazer pizza de presunto (queijo + presunto + tomate). A pizzaria fecha aos domingos. Tente mapear os conceitos acima para o padrão Abstract Factory (hierarquia de fábricas, hierarquia de produtos, cliente) e implemente um programa que receba uma data como parâmetro (formato dd/mm/yyyy) e imprima os ingredientes da pizza que é feita no dia ou, se a pizzaria estiver fechada, informe isso na tela. Agora imagine que a pizzaria agora faz também calzones (novamente, de calabresa ou presunto). Complemente a solução com mais este componente.

 

Diagrama de Classes na Linguagem de Programação Java Calzone.java CalzoneCalabresa.java CalzonePresunto.java Pizza.java PizzaCalabresa.java PizzaPresunto.java Pizzaiolo.java PizzaioloCalabresa.java PizzaioloPresunto.java PizzariaCloseException.java Application.java
Diagrama de Classes
Diagrama de Classes na Linguagem de Programação Java

Arquivo Calzone.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".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial08.exercicio17;

/**
 * Representação abstrata de um calzone
 *
 * Abstract Product - declara uma interface para um tipo de objeto-produto
 * (Gamma, 2000)
 */
public interface Calzone
{
  /**
   * Retornar os ingredientes que compõem o calzone
   *
   * @return ingredientes que compõem o calzone
   */
  public String ingredientes();
}

Arquivo CalzoneCalabresa.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".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial08.exercicio17;

/**
 * Representação de um calzone de calabresa
 *
 * Concrete Product - define um objeto-produto a ser criado pela
 * correspondente fábrica concreta (Gamma, 2000)
 */
public class CalzoneCalabresa implements Calzone
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.tutorial08.exercicio17.Calzone#ingredientes()
   */
  @Override
  public String ingredientes()
  {
    return "queijo, calabresa e tomate";
  }
}

Arquivo CalzonePresunto.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".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial08.exercicio17;

/**
 * Representação de um calzone de presunto
 *
 * Concrete Product - define um objeto-produto a ser criado pela
 * correspondente fábrica concreta (Gamma, 2000)
 */
public class CalzonePresunto implements Calzone
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.tutorial08.exercicio17.Calzone#ingredientes()
   */
  @Override
  public String ingredientes()
  {
    return "queijo, presunto e tomate";
  }
}

Arquivo Pizza.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".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial08.exercicio17;

/**
 * Representação abstrata de uma pizza
 *
 * Abstract Product - declara uma interface para um tipo de objeto-produto
 * (Gamma, 2000)
 */
public interface Pizza
{
  /**
   * Retornar os ingredientes que compõem a pizza
   *
   * @return ingredientes que compõem a pizza
   */
  public String ingredientes();
}

Arquivo PizzaCalabresa.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".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial08.exercicio17;

/**
 * Representação de uma pizza de calabresa
 *
 * Concrete Product - define um objeto-produto a ser criado pela
 * correspondente fábrica concreta (Gamma, 2000)
 */
public class PizzaCalabresa implements Pizza
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.tutorial08.exercicio17.Pizza#ingredientes()
   */
  @Override
  public String ingredientes()
  {
    return "queijo, calabresa e tomate";
  }
}

Arquivo PizzaPresunto.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".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial08.exercicio17;

/**
 * Representação de uma pizza de presunto
 *
 * Concrete Product - define um objeto-produto a ser criado pela
 * correspondente fábrica concreta (Gamma, 2000)
 */
public class PizzaPresunto implements Pizza
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.tutorial08.exercicio17.Pizza#ingredientes()
   */
  @Override
  public String ingredientes()
  {
    return "queijo, presunto e tomate";
  }
}

Arquivo Pizzaiolo.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".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial08.exercicio17;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Representação abstrata de um pizzaiolo
 *
 * Abstract Factory - declara uma interface para operações que criam
 * objetos-produto abstratos (Gamma, 2000).
 */
public abstract class Pizzaiolo
{
  /**
   * Retornar o pizzaiolo do dia
   *
   * @param data data do pedido
   * @return pizzaiolo do dia
   */
  public static Pizzaiolo pizzaiolo(String data)
  {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");

    Calendar calendar = Calendar.getInstance();

    try
    {
      calendar.setTime(simpleDateFormat.parse(data));
    }
    catch(ParseException exception)
    {

    }

    switch(calendar.get(Calendar.DAY_OF_WEEK))
    {
      case Calendar.MONDAY:
      case Calendar.WEDNESDAY:
      case Calendar.FRIDAY: return new PizzaioloCalabresa();
      case Calendar.TUESDAY:
      case Calendar.THURSDAY:
      case Calendar.SATURDAY: return new PizzaioloPresunto();
      default: throw new PizzariaCloseException("Pizzaria fechada aos domingos!");
    }
  }

  /**
   * Retornar um calzone
   *
   * @return calzone
   */
  public abstract Calzone calzone();

  /**
   * Retornar uma pizza
   *
   * @return pizza
   */
  public abstract Pizza pizza();
}

Arquivo PizzaioloCalabresa.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".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial08.exercicio17;

/**
 * Representação de um pizzaiolo especializado em calabresa
 *
 * Concrete Factory - implementa as operações que criam objetos-produto
 * concretos (Gamma, 2000)
 */
public class PizzaioloCalabresa extends Pizzaiolo
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.tutorial08.exercicio17.Pizzaiolo#calzone()
   */
  @Override
  public Calzone calzone()
  {
    return new CalzoneCalabresa();
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.tutorial08.exercicio17.Pizzaiolo#pizza()
   */
  @Override
  public Pizza pizza()
  {
    return new PizzaCalabresa();
  }
}

Arquivo PizzaioloPresunto.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".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial08.exercicio17;

/**
 * Representação de um pizzaiolo especializado em presunto
 *
 * Concrete Factory - implementa as operações que criam objetos-produto
 * concretos (Gamma, 2000)
 */
public class PizzaioloPresunto extends Pizzaiolo
{
  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.tutorial08.exercicio17.Pizzaiolo#calzone()
   */
  @Override
  public Calzone calzone()
  {
    return new CalzonePresunto();
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.tutorial08.exercicio17.Pizzaiolo#pizza()
   */
  @Override
  public Pizza pizza()
  {
    return new PizzaPresunto();
  }
}

Arquivo PizzariaCloseException.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".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial08.exercicio17;

/**
 * Pizzaria fechada
 */
public class PizzariaCloseException extends RuntimeException
{
  /**
   * Identificador de serialização da classe
   */
  private static final long serialVersionUID = 1L;

  /**
   * Inicializar a exceção
   */
  public PizzariaCloseException()
  {
    super();
  }

  /**
   * Inicializar a exceção com uma mensagem
   *
   * @param message mensagem
   */
  public PizzariaCloseException(String message)
  {
    super(message);
  }
}

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".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial08.exercicio17;

/**
 * Classe responsável pela execução do padrão de projeto Abstract Factory
 */
public class Application
{
  /**
   * Construtor para inicializar a execução do padrão de projeto
   * Abstract Factory
   */
  private Application()
  {

  }

  /**
   * Método principal da linguagem de programação Java
   *
   * @param args argumentos da linha de comando (não utilizado)
   */
  public static void main(String[] args)
  {
    Pizzaiolo pizzaiolo = Pizzaiolo.pizzaiolo("19/10/2016");

    System.out.println(pizzaiolo.pizza().ingredientes());

    System.out.println(pizzaiolo.calzone().ingredientes());

    pizzaiolo = Pizzaiolo.pizzaiolo("20/10/2016");

    System.out.println(pizzaiolo.pizza().ingredientes());

    System.out.println(pizzaiolo.calzone().ingredientes());
  }
}