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

应用

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

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

相关推荐
心之语歌18 小时前
基于注解+拦截器的API动态路由实现方案
java·后端
华仔啊19 小时前
Stream 代码越写越难看?JDFrame 让 Java 逻辑回归优雅
java·后端
ray_liang19 小时前
用六边形架构与整洁架构对比是伪命题?
java·架构
Ray Liang20 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Java水解21 小时前
Java 中间件:Dubbo 服务降级(Mock 机制)
java·后端
七月丶1 天前
别再手动凑 PR 了:这个 AI Skill 会按仓库习惯自动建分支、拆提交、提 PR
人工智能·设计模式·程序员
刀法如飞1 天前
从程序员到架构师:6大编程范式全解析与实践对比
设计模式·系统架构·编程范式
九狼1 天前
Flutter + Riverpod +MVI 架构下的现代状态管理
设计模式
SimonKing1 天前
OpenCode AI辅助编程,不一样的编程思路,不写一行代码
java·后端·程序员
FastBean1 天前
Jackson View Extension Spring Boot Starter
java·后端