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

创建型-工厂方法

简单工厂

  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. 工厂方法,每次增加一个产品时,都需要增加一个具体类和对应的工厂,使系统中类的数量成倍增加,增加了系统的复杂度和具体类的依赖;
相关推荐
FLZJ_KL2 小时前
【设计模式】【行为型模式】状态模式(State)
ui·设计模式·状态模式
黑不溜秋的15 小时前
C++设计模式 - 模板模式
c++·设计模式
andy7_15 小时前
Spring中常见的设计模式
java·spring·设计模式
FLZJ_KL18 小时前
【设计模式】【行为型模式】命令模式(Command)
设计模式·命令模式
点点滴滴的记录19 小时前
开发中用到的设计模式
设计模式
添砖Java中1 天前
深度解析策略模式:从理论到企业级实战应用
spring boot·spring·spring cloud·设计模式·maven·策略模式
illus10n_CHOU1 天前
【项目总结】易到家家政服务平台 —— 派单调度(7)
java·spring boot·后端·学习·设计模式
new6669991 天前
Java实现模版方法模式
java·设计模式
艾斯特_1 天前
前端设计模式介绍及案例(单例模式、代理模式、工厂模式、装饰者模式、观察者模式)
前端·javascript·观察者模式·单例模式·设计模式
weixin_438335401 天前
设计模式之策略模式
设计模式·策略模式