JAVA设计模式------(九)工厂模式
介绍
定义一个 工厂类的接口,帮助一个 实际对象 创建实例 ,并让其工厂类的子类决定实例化哪个类。
理解
工厂模式中,必定分为了两部分,一部分是被工厂模式实例化的对象(Product),一部分是工厂模式的接口(Factory)。
这里主要需要注意的是一个对象,工厂类只负责一个对象的创建。
实现
Product
java
package cn.sh.designepattern.example01;
/**
* @Author song
* @Version 0.0.1
* @Date 2025/4/29 10:41
* @Contact [email protected]
*/
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 [email protected]
*/
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 [email protected]
*/
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 [email protected]
*/
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 [email protected]
*/
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 [email protected]
*/
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 [email protected]
*/
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 [email protected]
*/
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 [email protected]
*/
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 [email protected]
*/
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 [email protected]
*/
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 [email protected]
*/
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();
}
}
应用
上述的工厂模式是一个简单的工厂模式,每次需要对具体类均创建一个工厂类,所以适用性单一。当然也有采用泛型的工厂方法,能够适用较多的场景。
可用于对对象的封装 ,降低模块的耦合度,因为不需要知道具体类的实现细节,即使具体类的实现改变,也只需要修改具体的工厂类方法,所以有较好的扩展性。