设计模式中的静态工厂,到底解决了什么问题?
文章目录
一、基本说明
静态工厂模式是一种创建型设计模式,通常是通过一个静态方法创建实例对象,而不是通过构造函数直接暴露给客户端。静态工厂模式解决了以下主要问题:
- 名称更明确:构造函数本身没有名称,它们仅通过参数列表进行区分。如果一个类需要多种方法来初始化其对象,静态工厂方法可以有描述性的名称,使得客户端代码更容易理解和使用。
- 不必每次调用都创建新对象:如果应用需要重用实例,静态工厂方法可以控制其创建过程。例如,单例模式或者缓存已经创建的实例,可以通过静态工厂方法返回这些实例,避免了不必要的对象创建。
- 返回接口类型:静态工厂方法返回的可以是接口类型,进而提高了模块的可扩展性,用户只需知道接口而不必关心具体实现,方便替换不同的实现。
- 降低客户端和具体实现之间的耦合:客户端只依赖于静态工厂返回的抽象类型(如接口或抽象类),实际的类可以在不更改客户端代码的情况下自由更换。
- 参数化实例创建:可以通过传递参数给静态工厂方法来动态地选择返回哪一个具体实现的对象。
尽管静态工厂模式有很多优点,但它也有一些缺点,如下所述:
- 类如果不含有 public 或 protected 的构造函数,那么就不能被子类化。
- 静态工厂方法与其他的静态方法实际上没有任何区分,对于编程新手可能会造成理解上的困惑。
总而言之,静态工厂方法是一种非常有用的设计模式,它提供了一种比构造函数更灵活的对象创建机制。它可以让你的代码更加清晰、灵活和易于维护。
二、代码演示
1、名称更明确
public class RGBColor {
private int red;
private int green;
private int blue;
// 私有构造方法,避免外部直接使用new来创建对象
private RGBColor(int red, int green, int blue) {
this.red = red;
this.green = green;
this.blue = blue;
}
// 静态工厂方法,名称明确表示创建对象的意图
public static RGBColor fromRGB(int red, int green, int blue) {
return new RGBColor(red, green, blue);
}
public static RGBColor fromHex(String hex) {
// 解析hex字符串并创建RGBColor对象
int red = ...
int green = ...
int blue = ...
return new RGBColor(red, green, blue);
}
// ...其他属性和方法
}
2、不必每次调用都创建新对象
public class BooleanWrapper {
private boolean value;
private BooleanWrapper(boolean value) {
this.value = value;
}
private static final BooleanWrapper TRUE = new BooleanWrapper(true);
private static final BooleanWrapper FALSE = new BooleanWrapper(false);
public static BooleanWrapper valueOf(boolean value) {
return value ? TRUE : FALSE;
}
}
3、返回接口类型
public interface Shape {
void draw();
}
public class Circle implements Shape {
public void draw() {
System.out.println("Drawing a circle.");
}
// 静态工厂方法返回接口类型
public static Shape newShape() {
return new Circle();
}
}
// 客户端代码
public class Client {
public static void main(String[] args) {
Shape shape = Circle.newShape(); // 接口类型指向具体实现类的对象
shape.draw();
}
}
4、降低客户端和具体实现之间的耦合
public interface MessageService {
void sendMessage(String message);
}
// 具体实现
public class EmailService implements MessageService {
public void sendMessage(String message) {
System.out.println("Sending email with message: " + message);
}
}
// 工厂类
public class MessagingFactory {
public static MessageService getEmailService() {
return new EmailService();
}
}
// 客户端代码,只关心MessageService接口
public class Client {
public static void main(String[] args) {
MessageService messageService = MessagingFactory.getEmailService();
messageService.sendMessage("Hello World!");
}
}
5、参数化实例创建
public enum ShapeType {
CIRCLE,
RECTANGLE
}
public class ShapeFactory {
public static Shape getShape(ShapeType type) {
switch (type) {
case CIRCLE:
return new Circle();
case RECTANGLE:
return new Rectangle();
default:
throw new IllegalArgumentException("Shape type not recognized.");
}
}
}
// 客户端代码
public class Client {
public static void main(String[] args) {
Shape shape = ShapeFactory.getShape(ShapeType.CIRCLE);
shape.draw(); // 绘制圆形
shape = ShapeFactory.getShape(ShapeType.RECTANGLE);
shape.draw(); // 绘制矩形
}
}