package com.maom.design.D_02_FactoryMethod;
/**
* @Title: D_02_FactoryMethod
* @Description: 02_工厂方法模式
* @Author maohb
* @Version 1.0
*/
public class D_02_FactoryMethod {
public static void main(String[] args) {
//获得AppleFactory
FruitFactory ff = new AppleFactory();
//通过AppleFactory来获得Apple实例对象
Fruit apple = ff.getFruit();
apple.get();
//获得BananaFactory
FruitFactory ff2 = new BananaFactory();
Fruit banana = ff2.getFruit();
banana.get();
//获得PearFactory
FruitFactory ff3 = new PearFactory();
Fruit pear = ff3.getFruit();
pear.get();
}
}
interface Fruit {
/* 采集 */
public void get();
}
interface FruitFactory {
public Fruit getFruit();
}
class Banana implements Fruit {
public void get(){
System.out.println("采集香蕉");
}
}
class BananaFactory implements FruitFactory {
public Fruit getFruit() {
return new Banana();
}
}
class Apple implements Fruit {
public void get(){
System.out.println("采集苹果");
}
}
class AppleFactory implements FruitFactory {
public Fruit getFruit() {
return new Apple();
}
}
class Pear implements Fruit {
public void get() {
System.out.println("采集梨子");
}
}
class PearFactory implements FruitFactory {
public Fruit getFruit() {
return new Pear();
}
}
设计模式笔记_02_工厂方法模式
最新推荐文章于 2024-10-10 22:44:55 发布