设计模式之工厂方法模式精讲

工厂方法模式又叫虚拟构造函数(Virtual Constructor)模式或者多态性工厂(Polymorphic Factory)模式。工厂方法模式的用意是定义一个创建产品对象的工厂接口,将实际创建性工作推迟到子类中。

工厂模式可以分为简单工厂、工厂方法和抽象工厂模式。

  1. 简单工厂模式:需要注意的是,简单工厂并不包含在《GoF》一书中定义的23种设计模式,因为它过于简单,更像是一种编程习惯,并且它违反了开闭原则,增加工厂需要修改类的实现。
  2. 工厂方法模式:是简单工厂模式的进一步抽象和推广,将具体创建工作交给子类去做,避免违反开闭原则。
  3. 抽象工厂模式:如果说工厂方法模式针对的是一个产品等级结构,那么抽象工厂模式面对的就是多个产品等级结构。
    由于简单工厂模式严格来说并不算是一种设计模式,就不再画UML图了,大家通过一个例子感受一下它的用法:
java 复制代码
public interface Shape {
    void draw();
}

public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("draw a circle");
    }
}

public class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("draw a square");
    }
}

public class SimpleFactory {
    public static Shape createShape(String shapeType) {
        if (shapeType == null) {
            throw new IllegalArgumentException("Shape type cannot be null.");
        }
        switch (shapeType.toLowerCase()) {
            case "circle":
                return new Circle();
            case "square":
                return new Square();
            default:
                throw new IllegalArgumentException("Unsupported shape type: " + shapeType);
        }
    }
}

public class Demo {
    public static void main(String[] args) {
        Shape circle = SimpleFactory.createShape("circle");
        circle.draw();
        Shape square = SimpleFactory.createShape("square");
        square.draw();
        Shape triangle = SimpleFactory.createShape("triangle");
        triangle.draw();
    }
}

工厂方法模式的UML图如下:

下面还是以一个例子来说明工厂方法模式的用法。假设有一个果农接口,相当于图中的Creator,有一个葡萄果农和一个苹果农分别实现果农接口,这两个类相当于ConcreteCreator类。然后有一个水果接口 ,相当于Product接口,一个苹果类和一个葡萄类分别实现水果接口。这时候注意体会工厂方法模式和简单工厂模式的区别,工厂方法模式把创建对象的工作分别放到葡萄果农和苹果农的类中去实现,也就是在具体类中实现。

java 复制代码
public interface GuoNong {
    Fruit createFruit();
}

public class GrapeNong implements GuoNong {
    @Override
    public Fruit createFruit() {
        return new Grape();
    }
}

public class AppleNong implements GuoNong {
    @Override
    public Fruit createFruit() {
        return new Apple();
    }
}

public interface Fruit {
    void plant();
    void grow();
    void harvest();
}

public class Grape implements Fruit {
    @Override
    public void plant() {
        System.out.println("种葡萄");
    }
    @Override
    public void grow() {
        System.out.println("葡萄生长");
    }
    @Override
    public void harvest() {
        System.out.println("收葡萄");
    }
}

public class Apple implements Fruit{
    @Override
    public void plant() {
        System.out.println("种苹果");
    }
    @Override
    public void grow() {
        System.out.println("苹果生长");
    }
    @Override
    public void harvest() {
        System.out.println("收苹果");
    }
}

public class Demo {
    public static void main(String[] args) {
        GrapeNong grapeNong = new GrapeNong();
        Fruit grape = grapeNong.createFruit();
        grape.plant();
        grape.grow();
        grape.harvest();
        System.out.println("**************分割线**********************");
        AppleNong appleNong = new AppleNong();
        Fruit apple = appleNong.createFruit();
        apple.plant();
        apple.grow();
        apple.harvest();
    }
}
相关推荐
HeLiang73 分钟前
proguard 混淆 使用JDK17 的 springboot4 + JPA
java·spring boot·proguard
武子康4 分钟前
Java-10 深入浅出 MyBatis 一对多与多对多查询配置详解
java·后端
一 乐5 分钟前
网上订餐系统|基于springboot的网上订餐系统设计与实现(源码+数据库+文档)
java·数据库·spring boot·后端·论文·毕设·网上订餐系统
摇滚侠5 分钟前
我把一个依赖安装到了本地仓库,但是IDEA 刷新 maven 提示远程私服仓库找不到,怎么解决
java·maven·intellij-idea
.Cnn10 分钟前
SpringBoot 文件上传与阿里云 OSS 集成
java·spring boot·后端·阿里云
Mininglamp_271812 分钟前
现在入局Agent开发还来得及吗?
java·开发语言
疯狂成瘾者14 分钟前
GHCR 是什么?GitHub 容器镜像仓库技术介绍
java·linux
方也_arkling19 分钟前
【Java-Day10】多态
java·开发语言
布朗克16824 分钟前
04 变量与数据类型
java·变量与数据类型
hdsoft_huge25 分钟前
以2026世界杯晋级逻辑,生动拆解SpringBoot软件架构
java·spring boot·后端