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

创建型-工厂方法

简单工厂

  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. 工厂方法,每次增加一个产品时,都需要增加一个具体类和对应的工厂,使系统中类的数量成倍增加,增加了系统的复杂度和具体类的依赖;
相关推荐
gladiator+4 小时前
Java中的设计模式------策略设计模式
java·开发语言·设计模式
在未来等你7 小时前
AI Agent设计模式 Day 2:Plan-and-Execute模式:先规划后执行的智能策略
设计模式·llm·react·ai agent·plan-and-execute
在未来等你14 小时前
AI Agent设计模式 Day 3:Self-Ask模式:自我提问驱动的推理链
设计模式·llm·react·ai agent·plan-and-execute
xiaodaidai丶21 小时前
设计模式之策略模式
设计模式·策略模式
_院长大人_1 天前
设计模式-工厂模式
java·开发语言·设计模式
王道长服务器 | 亚马逊云2 天前
AWS + 苹果CMS:影视站建站的高效组合方案
服务器·数据库·搜索引擎·设计模式·云计算·aws
在未来等你2 天前
AI Agent设计模式 Day 1:ReAct模式:推理与行动的完美结合
设计模式·llm·react·ai agent·plan-and-execute
乐悠小码2 天前
Java设计模式精讲---03建造者模式
java·设计模式·建造者模式
_院长大人_2 天前
设计模式-代理模式
设计模式·代理模式
guangzan2 天前
TypeScript 中的单例模式
设计模式