抽象工厂模式

抽象工厂(Creator)角色

package org.designPattern.abstractFactory;


public interface FruitFactory {
    
    public Fruit getApple();
    
    public Fruit getBanana();
}

2.具体工厂( Concrete  Creator)角色

package org.designPattern.abstractFactory;


public class NorthFruitFactory implements FruitFactory {

    public Fruit getApple() {
        return new NorthApple();
    }

    public Fruit getBanana() {
        return new NorthBanana();
    }

}

3.抽象(Product)角色

package org.designPattern.abstractFactory;


public abstract class Apple implements Fruit{

    public abstract void get();
}

package org.designPattern.abstractFactory;


public abstract class Banana implements Fruit{

    public abstract void get();
}

4.具体产品(Concrete Product)角色

package org.designPattern.abstractFactory;


public class NorthApple extends Apple {

    public void get() {
        System.out.println("采摘北方苹果");
    }

}

package org.designPattern.abstractFactory;


public class NorthBanana extends Banana {

    public void get() {
        System.out.println("采摘北方香蕉");
    }

}



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