设计模式-工厂方法(创建型)

创建型-工厂方法

简单工厂

  1. 将被创建的对象称为"产品",将生产"产品"对象称为"工厂";
  2. 如果创建的产品不多,且不需要生产新的产品,那么只需要一个工厂就可以,这种模式叫做"简单工厂",它不属于23中设计模式之一;
  3. 简单工厂这种模式不符合对拓展开放,对修改关闭 的开闭原则,因为这种设计模式,新增产品的时候需要修改工厂类代码;

例

简单工厂:

java 复制代码
/** 简单工厂,工厂类*/
public class SimpleFactory {
    private static SimpleFactory INSTANCE = new SimpleFactory();

    public static SimpleFactory getInstance(){
        return INSTANCE;
    }
    public Car createCar() {
        return new Car();
    }

    public Plane createPlane() {
        return new Plane();
    }

    public Train creatTrain(){
        return new Train();
    }
}

简单工厂使用:

java 复制代码
public class main {
    public static void main(String[] args) {
        SimpleFactory simpleFactory = SimpleFactory.getInstance();
        Car car = simpleFactory.createCar();
        Plane plane = simpleFactory.createPlane();
        Train train = simpleFactory.creatTrain();
    }
}

这种模式如果产生一个other交通工具,还需要在工厂类中新增creatOther()方法。

工厂方法

角色

  1. 抽象工厂:提供创建产品的接口,调用者通过它访问具体的工厂的工厂方法;
  2. 具体工厂:实现抽象工厂中的抽象方法,完成具体的创建;
  3. 抽象产品:定义产品的规范,描述产品的主要特性;
  4. 具体产品:实现抽象产品角色定义的接口,它与具体工厂一一对应;

图解

实现案例

产品抽象类:

java 复制代码
public interface Vehicle {
    void go();
}

产品具体实现类:

java 复制代码
public class Car implements Vehicle{
    @Override
    public void go() {
        System.out.println("汽车在跑!");
    }
}

public class Plane implements Vehicle{
    @Override
    public void go() {
        System.out.println("火车在跑!");
    }
}

public class Train implements Vehicle{
    @Override
    public void go() {
        System.out.println("飞机在飞!");
    }
}

抽象工厂类:

java 复制代码
public interface VehicleFactory {
    Vehicle create();
}

具体工厂类:

java 复制代码
public class CarFactory implements VehicleFactory{
    private static CarFactory INSTANCE = new CarFactory();
    @Override
    public Vehicle create() {
        return new Car();
    }
    public static CarFactory getInstance(){
        return INSTANCE;
    }
}

public class PlaneFactory implements VehicleFactory{
    private static PlaneFactory INSTANCE = new PlaneFactory();
    @Override
    public Vehicle create() {
        return new Plane();
    }
    public static PlaneFactory getInstance(){
        return INSTANCE;
    }
}
public class TrainFactory implements VehicleFactory{
    private static TrainFactory INSTANCE = new TrainFactory();
    @Override
    public Vehicle create() {
        return new Train();
    }
    public static TrainFactory getInstance(){
        return INSTANCE;
    }
}

使用:

java 复制代码
public class main {
    public static void main(String[] args) {
        CarFactory carFactory = CarFactory.getInstance();
        Vehicle vehicle = carFactory.create();
        vehicle.go();
    }
}

总结

  1. 简单工厂不符合开闭原则。
  2. 工厂方法,每次增加一个产品时,都需要增加一个具体类和对应的工厂,使系统中类的数量成倍增加,增加了系统的复杂度和具体类的依赖;
相关推荐
c萱1 小时前
AI产品经理——01基础认知篇
设计模式·prompt·aigc·产品经理·ai编程·gitcode·ai-native
YYYing.2 小时前
【设计模式系列 (八) 】组合模式
c++·后端·设计模式·组合模式·c/c++
geovindu16 小时前
rust: Composite Pattern
开发语言·后端·设计模式·rust·组合模式
YYYing.20 小时前
【设计模式系列 (七) 】桥接模式
c++·后端·设计模式·桥接模式
阿狗童鞋2 天前
设计模式实战指南
设计模式
电子科技圈2 天前
基于车载SerDes和OpenGMSL协议的SoC设计与验证:行业高频问题解析
设计模式·车载系统·软件构建·设计规范
ClinicTech2 天前
高速冷冻离心机的温控系统与安全保护逻辑解析
设计模式·架构·健康医疗·设计语言
GISMagic2 天前
3.《设计模式》:软件设计与解耦能力提炼
设计模式·ai coding
染指11102 天前
131.Agent-Agent设计模式-MAS多智能体系统(Multi-Agent-System)
人工智能·设计模式·langchain·agent·agents
ShyanZh3 天前
【Python3基础】13-Python 常用设计模式
开发语言·python·设计模式