package 工厂方法模式;

来源:互联网 发布:公安大数据宣传片 编辑:程序博客网 时间:2024/06/06 01:01
package 工厂方法模式;


interface Fruit {
public void show();
}


class Banana implements Fruit {
public void show() {
System.out.println("我是香蕉");
}
}


class Apple implements Fruit {
public void show() {
System.out.println("我是苹果");
}
}


interface FruitFactory {
public Fruit getInstance();
}


class AppleFactory implements FruitFactory {
private Apple apple = new Apple();
public Fruit getInstance() {
return apple;
}


}


class BananaFactory implements FruitFactory {
private Banana banana = new Banana();
public Fruit getInstance() {
return banana;
}


}


public class FactoryMethod {
public static void main(String[] args) {
System.out.println("工厂方法模式");
AppleFactory afAppleFactory = new AppleFactory();
Fruit apple = afAppleFactory.getInstance();
apple.show();
}
}
0 0
原创粉丝点击