/*
* 模式名称:工厂模式
* 模式特征:通过一个通用的接口创建不同的类对象
* 模式用途:面向接口编程,动态联编,多态性
* */
package Creator.Factory;
public class FactoryTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Factoryable factory;
factory=new Factory().getFactoryable(Factory.FACTORYA_ID);
factory.showName();
factory=new Factory().getFactoryable(Factory.FACTORYB_ID);
factory.showName();
}
}
interface Factoryable{
public void showName();
}
class FactoryA implements Factoryable{
public void showName(){
System.out.println("FactoryA.showName()");
}
}
class FactoryB implements Factoryable{
public void showName(){
System.out.println("FactoryB.showName()");
}
}
class Factory{
public static final int FACTORYA_ID=1,
FACTORYB_ID=2;
public static Factoryable getFactoryable(int factoryID){
switch(factoryID){
case 1:
return new FactoryA();
case 2:
return new FactoryB();
default:
return null;
}
}
}