Para exercitar a utilização do padrão de projeto Abstract Factory, desenvolva uma fábrica de automóveis.
O cliente é um motorista.
Ele solicita à fábrica a criação de um automóvel que tem um motor e pneus.
O cliente entra no automóvel, liga o motor, e eventualmente fura um pneu.
A AbstractFactory cria automóveis (AbstractProduct) com os pneus e motores.
As ConcreteFactory são Volkswagen e Ford, que criam automóveis (ConcreteProduct) Gol e Ka, respectivamente.
Os pneus da Volkswagen são Goodyear, enquanto os pneus da Ford são Firestone.
O motor Volkswagen é AP, enquanto que a Ford usa CHT.
Quando o motorista entra no Gol ele faz um barulho BRUM!, enquanto que o Ka faz um barulho TREM!.
O motor AP faz um barulho AP Ligado, enquanto que o motor CHT faz um barulho CHT ligado.
O pneu Goodyear quando fura faz um barulho Chash!, enquanto que o pneu Firestone quando fura faz um barulho TCHEEE!.
/**
* 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;
/**
* AbstractProduct
*/
public interface Motor
{
/**
* Retornar o tipo do motor
*
* @return tipo do motor
*/
public String getTipo();
/**
* Ligar o motor
*
* @return barulho ao ligar o motor
*/
public String ligar();
}
/**
* 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;
/**
* Product
*/
public class AP implements Motor
{
/* (non-Javadoc)
* @see Motor#getTipo()
*/
public String getTipo()
{
return "AP";
}
/* (non-Javadoc)
* @see Motor#ligar()
*/
public String ligar()
{
return "AP Ligado";
}
}
/**
* 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;
/**
* Product
*/
public class CHT implements Motor
{
/* (non-Javadoc)
* @see Motor#getTipo()
*/
public String getTipo()
{
return "CHT";
}
/* (non-Javadoc)
* @see Motor#ligar()
*/
public String ligar()
{
return "CHT Ligado";
}
}
/**
* 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;
/**
* AbstractProduct
*/
public interface Pneus
{
/**
* Furar o pneu
*
* @return barrulho quando o pneu fura
*/
public String furar();
/**
* Retornar o fabricante do pneu
*
* @return fabricante do pneu
*/
public String getFabricante();
}
/**
* 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;
/**
* Product
*/
public class Firestone implements Pneus
{
/* (non-Javadoc)
* @see Pneus#furar()
*/
public String furar()
{
return "TCHEEE!";
}
/* (non-Javadoc)
* @see Pneus#getFabricante()
*/
public String getFabricante()
{
return "Firestone";
}
}
/**
* 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;
/**
* Product
*/
public class Goodyear implements Pneus
{
/* (non-Javadoc)
* @see abstractFactory.Pneus#furar()
*/
public String furar()
{
return "Chash!";
}
/* (non-Javadoc)
* @see abstractFactory.Pneus#getFabricante()
*/
public String getFabricante()
{
return "Goodyear";
}
}
/**
* 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;
/**
* AbstractFactory
*/
public abstract class Montadora
{
/**
* Retornar a fabrica concreta desejada
*
* @param fabricante fabricante de automoveis
* @return fabrica concreta
*/
public static Montadora getFabricante(String fabricante)
{
if("Ford".equals(fabricante))
{
return new Ford();
}
return new Volkswagen();
}
/**
* Retornar o motor do automovel
*
* @return motor do automovel
*/
public abstract Motor getMotor();
/**
* Retornar os pneus do automovel
*
* @return pneus do automovel
*/
public abstract Pneus getPneus();
}
/**
* 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;
/**
* ConcreteFactory
*/
public class Ford extends Montadora
{
/* (non-Javadoc)
* @see Montadora#getMotor()
*/
public Motor getMotor()
{
return new CHT();
}
/* (non-Javadoc)
* @see Montadora#getPneus()
*/
public Pneus getPneus()
{
return new Firestone();
}
}
/**
* 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;
/**
* ConcreteFactory
*/
public class Volkswagen extends Montadora
{
/* (non-Javadoc)
* @see Montadora#getMotor()
*/
public Motor getMotor()
{
return new AP();
}
/* (non-Javadoc)
* @see Montadora#getPneus()
*/
public Pneus getPneus()
{
return new Goodyear();
}
}
/**
* 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;
/**
* Client
*/
public class Cliente
{
/**
* Metodo principal da linguagem de programacao Java
*
* @param args argumentos da linha de comando (nao utilizado)
*/
public static void main(String[] args)
{
Montadora montadora = Montadora.getFabricante("Volkswagen");
System.out.println("Volkswagen"); // Volkswagen
Motor motor = montadora.getMotor();
System.out.println(motor.getTipo()); // AP
System.out.println(motor.ligar()); // AP Ligado
Pneus pneus = montadora.getPneus();
System.out.println(pneus.getFabricante()); // Goodyear
System.out.println(pneus.furar()); // Chash!
montadora = Montadora.getFabricante("Ford");
System.out.println("Ford"); // Ford
motor = montadora.getMotor();
System.out.println(motor.getTipo()); // CHT
System.out.println(motor.ligar()); // CHT Ligado
pneus = montadora.getPneus();
System.out.println(pneus.getFabricante()); // Firestone
System.out.println(pneus.furar()); // TCHEEE!
}
}