packageabstractFactory;public abstract class Apple implementsFruit {public abstract voidget();
}packageabstractFactory;public abstract class Banana implementsFruit {public abstract voidget();
}packageabstractFactory;public interfaceFruit {public voidget();
}packageabstractFactory;public interfaceFruitFactory {//实例化Apple
publicFruit getApple();//实例化Banana
publicFruit getBanana();
}packageabstractFactory;public class InnerApple extendsApple {
@Overridepublic voidget() {
System.out.println("长在室内的苹果");
}
}packageabstractFactory;public class InnerBanana extendsBanana {
@Overridepublic voidget() {
System.out.println("长在室内的香蕉");
}
}packageabstractFactory;public class InnerFruitFactory implementsFruitFactory {
@OverridepublicFruit getApple() {return newInnerApple();
}
@OverridepublicFruit getBanana() {return newInnerBanana();
}
}packageabstractFactory;public class NorthApple extendsApple {
@Overridepublic voidget() {
System.out.println("长在北方的苹果");
}
}packageabstractFactory;public class NorthBanana extendsBanana {
@Overridepublic voidget() {
System.out.println("长在北方的香蕉");
}
}packageabstractFactory;public class NorthFruitFactory implementsFruitFactory {
@OverridepublicFruit getApple() {return newNorthApple();
}
@OverridepublicFruit getBanana() {return newNorthBanana();
}
}packageabstractFactory;public class SouthApple extendsApple {
@Overridepublic voidget() {
System.out.println("长在南方的苹果");
}
}packageabstractFactory;public class SouthBanana extendsBanana {
@Overridepublic voidget() {
System.out.println("长在南方的香蕉");
}
}packageabstractFactory;public class SouthFruitFactory implementsFruitFactory {
@OverridepublicFruit getApple() {return newSouthApple();
}
@OverridepublicFruit getBanana() {return newSouthBanana();
}
}//测试类
packageabstractFactory;public classMainClass {public static voidmain(String[] args) {
FruitFactory ff= newNorthFruitFactory();
Fruit apple=ff.getApple();
apple.get();
Fruit banana=ff.getBanana();
banana.get();
System.out.println("~~~~~~~~~~~~~~~~~~~~");
FruitFactory bb= newSouthFruitFactory();
Fruit apple2=bb.getApple();
apple2.get();
Fruit banana2=bb.getBanana();
banana2.get();
System.out.println("~~~~~~~~~~~~~~~~~~~~");//比如要增加室内innerApple,InnerBanana
FruitFactory cc = newInnerFruitFactory();
Fruit apple3=cc.getApple();
apple3.get();
Fruit banana3=cc.getBanana();
banana3.get();
}
}