The Simple Factory Pattern is a creative design pattern that is primarily used to create object instances. In a simple factory design pattern, the factory class is mainly responsible for creating objects of other classes, while the client does not need to create objects directly, but only needs to request the required objects from the factory class. Through a simple factory, we can encapsulate the creation logic of objects, which can reduce the coupling between the client and the specific object, and improve the scalability of business code.
Design ideas for the simple factory model
The first step is to define an interface or abstract class
The first thing that needs to be determined in a simple factory design pattern is the creation of a common interface or abstract class of an object, which is mainly used to define the behavior or methods of the object.
The second step is to create a concrete class
Implement specific operation classes of an interface or abstract class based on an interface class, which implements the methods defined in the interface or abstract class, and then implements them concretely.
The third step is to create a factory class
Create a factory class, which is responsible for creating specific object instances based on client requests. This factory class usually contains a method that can create objects based on different conditions or parameters, and its return value is a different type of object instance.
Step 4: Client use
When making a client call, the required object instance is created through the factory class method, and the client does not need to create the object directly. You just need to know what type of object you need, and you don't need to know the details of how the object is created.
An example of an implementation of the Simple Factory pattern
Let's say we have a shape interface, Shape, and two implementation classes, Circle and Rectangle, both of which implement the Shape interface. We create different types of shape objects with a simple factory pattern. It is shown below.
// 形状接口
public interface Shape {
void draw();
}
// 圆形类
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("画一个圆形");
}
}
// 矩形类
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("画一个矩形");
}
}
// 形状工厂类
public class ShapeFactory {
// 根据类型创建形状对象
public Shape createShape(String type) {
if ("circle".equalsIgnoreCase(type)) {
return new Circle();
} else if ("rectangle".equalsIgnoreCase(type)) {
return new Rectangle();
} else {
throw new IllegalArgumentException("不支持的形状类型");
}
}
}
// 客户端代码
public class Client {
public static void main(String[] args) {
// 创建形状工厂对象
ShapeFactory factory = new ShapeFactory();
// 创建圆形对象
Shape circle = factory.createShape("circle");
circle.draw(); // 输出:画一个圆形
// 创建矩形对象
Shape rectangle = factory.createShape("rectangle");
rectangle.draw(); // 输出:画一个矩形
}
}
In the example above, the ShapeFactory class acts as a simple factory, taking care of creating different types of shape objects based on client requests. The client only needs to request the desired shape object from the factory class, and does not need to interact directly with the concrete shape class. This reduces the coupling between the client and the concrete shape class, and makes it easy to extend new shape classes.
Simple factory mode is suitable for the following scenarios:
- Relatively simple object creation logic: If the logic of object creation is relatively simple and does not involve complex condition judgment or dependency relationships, you can consider using the simple factory pattern.
- The client only needs to know the object type: The simple factory pattern can be used when the client only needs to know the type of object and does not need to know the details of the object's creation. The client only needs to call the method of the factory class and pass in the corresponding parameters or conditions to get the required object instance.
- Encapsulating the creation logic of objects: If you need to encapsulate the creation logic of objects to facilitate unified object allocation, management and maintenance, we can consider using the simple factory pattern. By encapsulating the creation logic of an object in a factory class through a simple factory pattern, you can hide some of the implementation details of the object.
Based on the above introduction, we know that the decoupling between objects is achieved through the simple factory pattern. This makes the code more flexible and easier to extend.