Definir uma interface para criar um objeto, mas deixar as subclasses decidirem que classe instanciar. O Factory Method permite adiar a instanciação para subclasse.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package factorymethod2;
/**
*
* @author patrick
*/
public abstract class Car {
private String model;
private String factory;
private String category;
public void showInformation() {
System.out.println("Model:" + this.getModel()+ " \nFactory:" + this.getFactory() + "\nCategory: " + this.getCategory());
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getFactory() {
return factory;
}
public void setFactory(String factory) {
this.factory = factory;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package factorymethod2;
/**
*
* @author patrick
*/
public abstract class CarCreator {
public void buildCar()
{
Car carro = factoryMethod();
}
protected abstract Car factoryMethod();
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package factorymethod2;
import javax.swing.JOptionPane;
/**
*
* @author patrick
*/
public class Client {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CarCreator creator1 = new FiatConcreteCreator();
creator1.buildCar();
CarCreator creator2 = new VolksConcreteCreator();
creator2.buildCar();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package factorymethod2;
/**
*
* @author patrick
*/
public class ConcreteProductGol extends Car{
public ConcreteProductGol(){
this.setModel("Gol");
this.setFactory("Volks");
this.setCategory("Hatch");
this.showInformation();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package factorymethod2;
/**
*
* @author patrick
*/
public class ConcreteProductPalio extends Car{
public ConcreteProductPalio(){
this.setModel("Palio");
this.setFactory("Fiat");
this.setCategory("Hatch");
this.showInformation();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package factorymethod2;
/**
*
* @author patrick
*/
public class FiatConcreteCreator extends CarCreator{
protected Car factoryMethod(){
return new ConcreteProductPalio();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package factorymethod2;
/**
*
* @author patrick
*/
public class VolksConcreteCreator extends CarCreator{
protected Car factoryMethod(){
return new ConcreteProductGol();
}
}