抽象工厂源代码
public interface Creator { /** * 工厂方法 */ public Product factory(); }
public interface Product { }
public class ConcreteCreator1 implements Creator { /** * 工厂方法 */ public Product factory() { return new ConcreteProduct1(); } }
public class ConcreteCreator2 implements Creator { /** * 工厂方法 */ public Product factory() { return new ConcreteProduct2(); } }
public class ConcreteProduct1 implements Product { public ConcreteProduct1() { //do something } }
public class ConcreteProduct2 implements Product { public ConcreteProduct2() { //do something } }
客户源代码
public class Client { private static Creator creator1,creator2; private static Product product1,product2; public static void main(String[] args) { creator1=new ConcreteCreator1(); product1=creator1.factory(); creator2=new ConcreteCreator1(); product2=creator2.factory(); } }
原文地址:http://blog.csdn.net/u013011841/article/details/38984709