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

Para exercitar a utilização do padrão de projeto Prototype, desenvolva uma revenda de automóveis.

O Client é um revendedor de automóveis Volkswagen.

O Prototype é um modelo de automóvel Gol.

Os ConcretePrototype são modelos de Automóveis Gol:

• Modelo Luxo - Gol com Motor AP de 104HP e Pneus 195/50R15

• Modelo Standard - Gol com Motor AP de 76HP e Pneus 175/65R14

Crie uma aplicação de venda de automóveis que vende Gol através da clonagem dos modelos Luxo e Standard.

 

Arquivo Gol.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Interface com as funcionalidades do modelo Gol
 *
 * Prototype - declara uma interface para clonar a si proprio
 */
public interface Gol
{
  /**
   * Retornar um clone do modelo Gol
   *
   * @return clone do modelo Gol
   */
  public Gol clone();

  /**
   * Retornar o motor do modelo Gol
   *
   * @return motor do modelo Gol
   */
  public String getMotor();

  /**
   * Retornar os pneus do modelo Gol
   *
   * @return pneus do modelo Gol
   */
  public String getPneus();
}

Arquivo GolStandard.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Classe responsavel pela representacao de um gol - modelo standard
 *
 * ConcretePrototype - implementa uma operacao para clonar a si proprio
 */
public class GolStandard implements Gol
{
  /**
   * Contador de instancias clonadas
   */
  private int counter;

  /**
   * Construtor padrao
   */
  public GolStandard()
  {
    counter = 0;
  }

  /**
   * Construtor para atualizar o contador de instancias clonadas
   *
   * @param counter contador de instancias clonadas
   */
  public GolStandard(int counter)
  {
    this.counter = counter + 1;
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Gol#clone()
   */
  public Gol clone()
  {
    return new GolStandard(counter);
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Gol#getMotor()
   */
  public String getMotor()
  {
    return "Motor AP de 76 HP";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Gol#getPneus()
   */
  public String getPneus()
  {
    return "Pneus 175/65R14";
  }

  /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
  public String toString()
  {
    return "Gol Standard (" + counter + "): " + getMotor() + " - "
      + getPneus();
  }
}

Arquivo GolLuxo.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Classe responsavel pela representacao de um gol - modelo luxo
 *
 * ConcretePrototype - implementa uma operacao para clonar a si proprio
 */
public class GolLuxo implements Gol
{
  /**
   * Contador de instancias clonadas
   */
  private int counter;

  /**
   * Construtor padrao
   */
  public GolLuxo()
  {
    counter = 0;
  }

  /**
   * Construtor para atualizar o contador de instancias clonadas
   *
   * @param counter contador de instancias clonadas
   */
  public GolLuxo(int counter)
  {
    this.counter = counter + 1;
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Gol#clone()
   */
  public Gol clone()
  {
    return new GolLuxo(counter);
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Gol#getMotor()
   */
  public String getMotor()
  {
    return "Motor AP de 104 HP";
  }

  /* (non-Javadoc)
   * @see com.ybadoo.tutoriais.poo.Gol#getPneus()
   */
  public String getPneus()
  {
    return "Pneus 195/50R15";
  }

  /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
  public String toString()
  {
    return "Gol Luxo (" + counter + "): " + getMotor() + " - " + getPneus();
  }
}

Arquivo Revendedor.java

/**
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.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 copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

/**
 * Classe responsavel pela representacao de um revendedor de automoveis
 * Volkswagen
 *
 * client - cria um novo objeto solicitando a um prototipo que clone a si
 * proprio
 */
public class Revendedor
{
  /**
   * Metodo principal da linguagem de programacao Java
   *
   * @param args argumentos da linha de comando (nao utilizado)
   */
  public static void main(String[] args)
  {
    Gol standard = new GolStandard();
    Gol luxo = new GolLuxo();

    System.out.println("Objetos originais");
    System.out.println(standard);
    System.out.println(luxo);

    Gol standard1 = standard.clone();
    Gol luxo1 = luxo.clone();

    System.out.println("\nPrimeira geração");
    System.out.println(standard1);
    System.out.println(luxo1);

    Gol standard2 = standard1.clone();
    Gol luxo2 = luxo1.clone();

    System.out.println("\nSegunda geração");
    System.out.println(standard2);
    System.out.println(luxo2);

    Gol standard3 = standard2.clone();
    Gol luxo3 = luxo2.clone();

    System.out.println("\nTerceira geração");
    System.out.println(standard3);
    System.out.println(luxo3);

    Gol standard4 = standard.clone();
    Gol luxo4 = luxo.clone();

    System.out.println("\nPrimeira geração");
    System.out.println(standard4);
    System.out.println(luxo4);
  }
}

Saída da solução em Java

Objetos originais
Gol Standard (0): Motor AP de 76 HP - Pneus 175/65R14
Gol Luxo (0): Motor AP de 104 HP - Pneus 195/50R15

Primeira geração
Gol Standard (1): Motor AP de 76 HP - Pneus 175/65R14
Gol Luxo (1): Motor AP de 104 HP - Pneus 195/50R15

Segunda geração
Gol Standard (2): Motor AP de 76 HP - Pneus 175/65R14
Gol Luxo (2): Motor AP de 104 HP - Pneus 195/50R15

Terceira geração
Gol Standard (3): Motor AP de 76 HP - Pneus 175/65R14
Gol Luxo (3): Motor AP de 104 HP - Pneus 195/50R15

Primeira geração
Gol Standard (1): Motor AP de 76 HP - Pneus 175/65R14
Gol Luxo (1): Motor AP de 104 HP - Pneus 195/50R15