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();
    }
}

应用

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

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

相关推荐
RainbowSea20 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea20 小时前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
考虑考虑1 天前
Jpa使用union all
java·spring boot·后端
用户3721574261351 天前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊1 天前
Java学习第22天 - 云原生与容器化
java
渣哥1 天前
原来 Java 里线程安全集合有这么多种
java
间彧1 天前
Spring Boot集成Spring Security完整指南
java
间彧1 天前
Spring Secutiy基本原理及工作流程
java
数据智能老司机1 天前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
Java水解1 天前
JAVA经典面试题附答案(持续更新版)
java·后端·面试