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

创建型-工厂方法

简单工厂

  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. 工厂方法,每次增加一个产品时,都需要增加一个具体类和对应的工厂,使系统中类的数量成倍增加,增加了系统的复杂度和具体类的依赖;
相关推荐
七月丶1 天前
别再手动凑 PR 了:这个 AI Skill 会按仓库习惯自动建分支、拆提交、提 PR
人工智能·设计模式·程序员
刀法如飞1 天前
从程序员到架构师:6大编程范式全解析与实践对比
设计模式·系统架构·编程范式
九狼1 天前
Flutter + Riverpod +MVI 架构下的现代状态管理
设计模式
静水流深_沧海一粟2 天前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式
StarkCoder2 天前
从UIKit到SwiftUI的迁移感悟:数据驱动的革命
设计模式
阿星AI工作室2 天前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
_哆啦A梦3 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
阿闽ooo6 天前
中介者模式打造多人聊天室系统
c++·设计模式·中介者模式
小米4966 天前
js设计模式 --- 工厂模式
设计模式