概念
定义一个接口,不同产品分别实现该接口,使用工厂将产品具体的实现封装起来,需要生成某产品对象时从工厂中取即可。缺点是每增加一个同类产品就需要修改工厂类并增加一个具体的产品类。
类图
Java 实现
/**
* description: a simple demo to help understand factory pattern
* author: lbt
* date: 2020年8月26日
*/
public class FactoryPatternDemo{
public static void main(String[] s){
FruitFactory fruitFactory = new FruitFactory();
Fruit apple = fruitFactory.getFruit("apple");
Fruit banana = fr