package com.maom.design.D_03_AbstractFactory;
/**
* @Title: D_03_AbstractFactory
* @Description: 03_抽象工厂模式
* @Author maohb
* @Version 1.0
*/
public class D_03_AbstractFactory {
public static void main(String[] args) {
FruitFactory ff = new NorthFruitFactory();
Fruit apple = ff.getApple();
apple.get();
Fruit banana = ff.getBanana();
banana.get();
FruitFactory ff2 = new SouthFruitFactory();
Fruit apple2 = ff2.getApple();
apple2.get();
Fruit banana2 = ff2.getBanana();
banana2.get();
}
}
interface Fruit {
/* 采集 */
public void get();
}
interface FruitFactory {
//实例化Apple
public Fruit getApple();
//实例化Banana
public Fruit getBanana();
}
abstract class Apple implements Fruit {
public abstract void get();
}
abstract class Banana implements Fruit {
public abstract void get();
}
class NorthApple extends Apple {
public void get() {
System.out.println("采集北方苹果");
}
}
class NorthBanana extends Banana {
public void get() {
System.out.println("采集北方香蕉");
}
}
class NorthFruitFactory implements FruitFactory {
public Fruit getApple() {
return new NorthApple();
}
public Fruit getBanana() {
return new NorthBanana();
}
}
class SouthApple extends Apple {
public void get() {
System.out.println("采集南方苹果");
}
}
class SouthBanana extends Banana {
public void get() {
System.out.println("采集南方香蕉");
}
}
class SouthFruitFactory implements FruitFactory {
public Fruit getApple() {
return new SouthApple();
}
public Fruit getBanana() {
return new SouthBanana();
}
}
设计模式笔记_03_抽象工厂模式
最新推荐文章于 2024-09-10 17:07:09 发布