抽象工廠模式git
實現過程github
產品接口
public interface Product1 { } public interface Product2 { }
具體產品類
public class ProductA1 implements Product1 { public ProductA1(){ System.out.println("Product : " + this.getClass().getSimpleName()); } } public class ProductA2 implements Product2 { public ProductA2(){ System.out.println("Product : " + this.getClass().getSimpleName()); } }
定義抽象工廠
public abstract class AbstractFactory { public abstract Product1 create1(); public abstract Product2 create2(); }
產生產品的具體工廠
public class FactoryA extends AbstractFactory{ @Override public Product1 create1() { return new ProductA1(); } @Override public Product2 create2() { return new ProductA2(); } }
調用方法
public static void main(String[] args) { AbstractFactory abstractFactory = new FactoryA(); abstractFactory.create1(); abstractFactory.create2(); }
輸出
Product : ProductA1 Product : ProductA2
源碼ide
https://github.com/Seasons20/DisignPattern.git
ENDthis