Fornece uma interface para criação de famílias de objetos relacionadas ou dependentes sem especificar suas classes concretas.
/*
* 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 abfactory2;
/**
*
* @author patrick
*/
public abstract class CarFactory {
public abstract SedanCar buildSedanCar();
public abstract HatchCar buildHatchCar();
}
/*
* 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 abfactory2;
/**
*
* @author patrick
*/
public class Client {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CarFactory factory = new FiatFactory();
SedanCar sedan = factory.buildSedanCar();
HatchCar hatch = factory.buildHatchCar();
sedan.showSedanInformation();
System.out.println();
hatch.showHatchInformation();
System.out.println();
factory = new VolksFactory();
sedan = factory.buildSedanCar();
hatch = factory.buildHatchCar();
sedan.showSedanInformation();
System.out.println();
hatch.showHatchInformation();
}
}
/*
* 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 abfactory2;
/**
*
* @author patrick
*/
public class FiatFactory extends CarFactory {
public SedanCar buildSedanCar() {
return new Siena();
}
public HatchCar buildHatchCar() {
return new Palio();
}
}
/*
* 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 abfactory2;
/**
*
* @author patrick
*/
public class Gol extends HatchCar {
public void showHatchInformation() {
System.out.println("Model: Gol \nFactory: Volks \nCategory:Hatch");
}
}
/*
* 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 abfactory2;
/**
*
* @author patrick
*/
public abstract class HatchCar {
public abstract void showHatchInformation();
}
/*
* 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 abfactory2;
/**
*
* @author patrick
*/
public class Palio extends HatchCar {
public void showHatchInformation() {
System.out.println("Model: Palio \nFactory: Fiat \nCategory:Hatch");
}
}
/*
* 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 abfactory2;
/**
*
* @author patrick
*/
public abstract class SedanCar {
public abstract void showSedanInformation();
}
/*
* 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 abfactory2;
/**
*
* @author patrick
*/
public class Siena extends SedanCar {
public void showSedanInformation() {
System.out.println("Model: Siena \nFactory: Fiat \nCategory:Sedan");
}
}
/*
* 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 abfactory2;
/**
*
* @author patrick
*/
public class VolksFactory extends CarFactory {
public SedanCar buildSedanCar() {
return new Voyage();
}
public HatchCar buildHatchCar() {
return new Gol();
}
}
/*
* 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 abfactory2;
/**
*
* @author patrick
*/
public class Voyage extends SedanCar {
public void showSedanInformation() {
System.out.println("Model: Voyage \nFactory: Volks \nCategory:Sedan");
}
}