抽象工厂模式

抽象工厂模式


public abstract class AbstractProductA {
    public void shareMethod(){
        
    }
    
    public abstract void doSomething();
}

public abstract class AbstractProductB {
    public void shareMethod() {

    }

    public abstract void doSomething();
}

public class ProductA1 extends AbstractProductA{

    public void doSomething(){
        System.out.println("产品A1的实现方法");
    }
}

public class ProductA2 extends AbstractProductA{
    public void doSomething(){
        System.out.println("产品A2的实现方法");
    }
}

public class ProductB1 extends AbstractProductB{
    public void doSomething(){
        System.out.println("产品B1的实现方法");
    }
}

public class ProductB2 extends AbstractProductB{
    public void doSomething(){
        System.out.println("产品B2的实现方法");
    }
}

public abstract class AbstractCreator {

    public abstract AbstractProductA createProductA();
    public abstract AbstractProductB createProductB();
}

public class Creator1 extends AbstractCreator{

    public AbstractProductA createProductA(){
        return new ProductA1();
    }
    public AbstractProductB createProductB(){
        return new ProductB1();
    }
}

public class Creator2 extends AbstractCreator{

    public AbstractProductA createProductA(){
        return new ProductA2();
    }
    public AbstractProductB createProductB(){
        return new ProductB2();
    }
}

public class Client {

    public static void main(String[] args){
        AbstractCreator creator1 = new Creator1();
        AbstractCreator creator2 = new Creator2();
        
        AbstractProductA a1 = creator1.createProductA();
        AbstractProductA a2 = creator2.createProductA();
        AbstractProductB b1 = creator1.createProductB();
        AbstractProductB b2 = creator2.createProductB();
        a1.doSomething();
        a2.doSomething();
        b1.doSomething();
        b2.doSomething();
    }
}


你可能感兴趣的:(抽象工厂模式)