JAVA设计模式——(九)工厂模式

JAVA设计模式------(九)工厂模式

介绍

定义一个 工厂类的接口,帮助一个 实际对象 创建实例 ,并让其工厂类的子类决定实例化哪个类。

理解

工厂模式中,必定分为了两部分,一部分是被工厂模式实例化的对象(Product),一部分是工厂模式的接口(Factory)。

这里主要需要注意的是一个对象,工厂类只负责一个对象的创建

实现

Product

java 复制代码
package cn.sh.designepattern.example01;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/4/29 10:41
 * @Contact 643947568@qq.com
 */
public interface Product {

    public void product();
}

实现类:

java 复制代码
package cn.sh.designepattern.example01;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/4/29 10:41
 * @Contact 643947568@qq.com
 */
public class ActualProduct implements Product {
    @Override
    public void product() {
        System.out.println("生产具体的产品");
    }
}

Factory

java 复制代码
package cn.sh.designepattern.example01;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/4/29 10:42
 * @Contact 643947568@qq.com
 */
public interface Factory {
    public Product factory();

}

工厂子类:

java 复制代码
package cn.sh.designepattern.example01;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/4/29 10:42
 * @Contact 643947568@qq.com
 */
public class ActualFactory implements Factory {
    @Override
    public Product factory() {
        return new ActualProduct();
    }
}

测试

java 复制代码
package cn.sh.designepattern.example01;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/4/29 10:18
 * @Contact 643947568@qq.com
 */
public class Main {
    public static void main(String[] args) {
        ActualFactory actualFactory = new ActualFactory();
        Product factory = actualFactory.factory();

        factory.product();
    }
}

泛型扩展

采用泛型的工厂类

java 复制代码
package cn.sh.designepattern.example02;

import java.lang.reflect.InvocationTargetException;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/4/29 11:22
 * @Contact 643947568@qq.com
 */
public interface Factory {
    public <T> T factory(Class<T> t) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException;
}
java 复制代码
package cn.sh.designepattern.example02;

import java.lang.reflect.InvocationTargetException;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/4/29 11:27
 * @Contact 643947568@qq.com
 */
public class ActualFactory implements Factory {
    @Override
    public <T> T factory(Class<T> t) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        return t.getDeclaredConstructor().newInstance();
    }
}

具体类1:

java 复制代码
package cn.sh.designepattern.example02;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/4/29 11:24
 * @Contact 643947568@qq.com
 */
public interface Product01 {
    public void product01();

}
java 复制代码
package cn.sh.designepattern.example02;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/4/29 11:24
 * @Contact 643947568@qq.com
 */
public class ActualProductO1 implements Product01{
    @Override
    public void product01() {
        System.out.println("生产01");
    }
}

具体类2:

java 复制代码
package cn.sh.designepattern.example02;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/4/29 11:24
 * @Contact 643947568@qq.com
 */
public interface Product02 {
    public void product02();


}
java 复制代码
package cn.sh.designepattern.example02;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/4/29 11:24
 * @Contact 643947568@qq.com
 */
public class ActualProductO2 implements Product02 {
    @Override
    public void product02() {
        System.out.println("生产02");
    }
}

测试:

java 复制代码
package cn.sh.designepattern.example02;

import java.lang.reflect.InvocationTargetException;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/4/29 11:28
 * @Contact 643947568@qq.com
 */
public class Main {

    public static void main(String[] args) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        ActualFactory actualFactory = new ActualFactory();
        ActualProductO1 productO1 = actualFactory.factory(ActualProductO1.class);
        productO1.product01();


        ActualProductO2 productO2 = actualFactory.factory(ActualProductO2.class);
        productO2.product02();
    }
}

应用

上述的工厂模式是一个简单的工厂模式,每次需要对具体类均创建一个工厂类,所以适用性单一。当然也有采用泛型的工厂方法,能够适用较多的场景。

可用于对对象的封装 ,降低模块的耦合度,因为不需要知道具体类的实现细节,即使具体类的实现改变,也只需要修改具体的工厂类方法,所以有较好的扩展性

相关推荐
接着奏乐接着舞几秒前
大模型开发 agent 知识库
java
xuhaoyu_cpp_java12 分钟前
SpringBoot学习(四)
java·经验分享·spring boot·笔记·学习
plainGeekDev38 分钟前
kapt 替换为 KSP
android·java·kotlin
蜡台1 小时前
通过Gradle脚本声明更改Java变量
android·java·开发语言·python·kotlin·gradle·groovy
小喻同学i1 小时前
std::thread函数参数传递
开发语言·c++
Larcher1 小时前
当 AI 学会写代码:一个自动生成 React 项目的 Agent 实战
人工智能·设计模式·程序员
程序员天天困1 小时前
后端视角看 EventBus:发布订阅总线的原理、场景与用法
java·后端
要开心吖ZSH1 小时前
AI医疗分诊与健康咨询助手agent开发——(4-2)从零到一:我给AI医疗分诊助手加了个“知识库大脑“,终于搞懂了RAG是什么
java·docker·agent·医疗·rag
Ulyanov2 小时前
Python实现6-DOF刚体仿真器(下)——环境扰动与控制闭环
开发语言·python·算法·系统仿真·雷达电子对抗·导引头
分布式存储与RustFS2 小时前
RustFS Beta.10 性能解读:PUT 全面反超 MinIO,Rust 重写对象存储成了?
开发语言·后端·rust