Factory Pattern
Abstract Factory-Provide an interface for creating families of related or dependent objects without specifying their concrete class.
Factory Method-Define an interface for creating an object but let subclass decide which class to instantiate. Factory Method lets a class defer instantiation to the subclass.
Problem:
if(picni)
duck =new MallardDuck();
else if(hunting)
duck = new DecoyDuck();
else
duck = new RubberDuck();
Solution:take the creation code and move it out into another object that is only going to be concerned with creating parts.
Example:
//code
abstract class CPU {
int cache;
int Frequency;
int Voltage;
String CPUName;
public void DisplayCPUCapability()
{
System.out.println("CPU name is:"+CPUName);
System.out.println("cache:"+cache+";Frequency:"+Frequency+";Voltage:"+Voltage);
}
}
class AMD extends CPU
{
public AMD(int cache,int Frequency,int Voltage)
{
this.cache=cache;
this.Frequency=Frequency;
this.Voltage = Voltage;
this.CPUName = "AMD";
}
}
class Intel extends CPU
{
public Intel(int cache,int Frequency,int Voltage)
{
this.cache=cache;
this.Frequency=Frequency;
this.Voltage = Voltage;
this.CPUName = "Intel";
}
}
abstract class Memory
{
int Capacity;
int Frequency;
int type;
String Name;
public void DisplayMemoryParam()
{
System.out.println("Memory name is:"+Name);
System.out.println("Capacity:"+Capacity+";Frequency:"+Frequency+";Type:"+type);
}
}
class Kingston extends Memory
{
public Kingston(int cache,int Frequency,int Voltage)
{
this.Capacity=cache;
this.Frequency=Frequency;
this.type = Voltage;
this.Name = "Kingston";
}
}
class ADATA extends Memory
{
public ADATA(int cache,int Frequency,int Voltage)
{
this.Capacity=cache;
this.Frequency=Frequency;
this.type = Voltage;
this.Name = "ADATA";
}
}
abstract class Mainboard
{
int BusFrequency;
String Name;
public void DisplayMainboard()
{
System.out.println("Mainboard name is:"+Name);
System.out.println("Bus frequency is:"+BusFrequency);
}
}
class JingYingMainboard extends Mainboard
{
public JingYingMainboard(int BusFrequency)
{
this.BusFrequency = BusFrequency;
this.Name = "JingYingMainboard";
}
}
public CPU CreateCPU();
public Memory CreateMemory();
public Mainboard CreateMainboard();
}
class QuanZhouHouse implements TraditionHouse
{
public CPU CreateCPU()
{
return new Intel(13,14,15);
}
public Memory CreateMemory()
{
return new Kingston(3,4,5);
}
public Mainboard CreateMainboard()
{
return new JingYingMainboard(111);
}
}
class NanJingHouse implements TraditionHouse
{
public CPU CreateCPU()
{
return new AMD(15,14,15);
}
public Memory CreateMemory()
{
return new ADATA(13,4,5);
}
public Mainboard CreateMainboard()
{
return new JingYingMainboard(121);
}
}
CPU cpu;
Mainboard mainboard;
Memory memory;
TraditionHouse tranHouse;
public void SetFactory(TraditionHouse tran)
{
tranHouse=tran;
}
public void prepareComputer()
{
cpu=tranHouse.CreateCPU();
mainboard=tranHouse.CreateMainboard();
memory=tranHouse.CreateMemory();
DisplayComputerParam();
}
private void DisplayComputerParam()
{
cpu.DisplayCPUCapability();
mainboard.DisplayMainboard();
memory.DisplayMemoryParam();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//declare two computer factory
TraditionHouse tran1=new QuanZhouHouse();
TraditionHouse tran2=new NanJingHouse();
//declare computers in setup
Computer computer1=new Computer();
Computer computer2=new Computer();
//select Computer factory which user select
computer1.SetFactory(tran1);
computer2.SetFactory(tran2);
//setup and display results
computer1.prepareComputer();
computer2.prepareComputer();
}
}
//if u find sth interesting,pls contact me. qq:95491590
When u see code like this, u know that when it comes time for changes or extensions, u will have to reopen this code and examine what needs to be added( or deleted). Often this kind of code ends up in several parts of the application making maintenance and update more difficult and error-prone. So, in other words, when u have code that makes use of lots of concrete classes, u are looking for trouble because that code may have to be changed as new concrete classes are added.