JAVA设计模式,工厂模式

工厂模式(Factory Pattern)是一种常用的创建型设计模式,它提供了一种创建对象的最佳方式。工厂模式的主要目的是将对象的创建逻辑封装在一个单独的方法或类中,而不是在客户端代码中直接使用`new`关键字创建对象。这样可以提高代码的灵活性和可维护性。

工厂模式的类型

  1. **简单工厂模式(Simple Factory)**:通过一个工厂类来创建对象,但这个工厂类不是必须遵守任何接口或抽象类。

  2. **工厂方法模式(Factory Method)**:定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。

  3. **抽象工厂模式(Abstract Factory)**:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

示例代码

简单工厂模式

假设我们有一个应用程序,需要根据用户的选择创建不同类型的消息对象。

消息接口

```java

public interface Message {

void send();

}

```

具体消息类

```java

public class EmailMessage implements Message {

@Override

public void send() {

System.out.println("Sending email message.");

}

}

public class SMSMessage implements Message {

@Override

public void send() {

System.out.println("Sending SMS message.");

}

}

```

简单工厂类

```java

public class MessageFactory {

public static Message createMessage(String type) {

if ("email".equals(type)) {

return new EmailMessage();

} else if ("sms".equals(type)) {

return new SMSMessage();

} else {

throw new IllegalArgumentException("Unknown message type: " + type);

}

}

}

```

客户端

```java

public class Client {

public static void main(String[] args) {

Message email = MessageFactory.createMessage("email");

email.send();

Message sms = MessageFactory.createMessage("sms");

sms.send();

}

}

```

运行结果

```

Sending email message.

Sending SMS message.

```

工厂方法模式

假设我们有一个更复杂的场景,不同的子类可以根据自己的需求创建不同的消息对象。

抽象工厂类

```java

public abstract class MessageFactory {

public abstract Message createMessage();

}

```

具体工厂类

```java

public class EmailMessageFactory extends MessageFactory {

@Override

public Message createMessage() {

return new EmailMessage();

}

}

public class SMSMessageFactory extends MessageFactory {

@Override

public Message createMessage() {

return new SMSMessage();

}

}

```

客户端

```java

public class Client {

public static void main(String[] args) {

MessageFactory emailFactory = new EmailMessageFactory();

Message email = emailFactory.createMessage();

email.send();

MessageFactory smsFactory = new SMSMessageFactory();

Message sms = smsFactory.createMessage();

sms.send();

}

}

```

运行结果

```

Sending email message.

Sending SMS message.

```

抽象工厂模式

假设我们有一个更复杂的场景,需要创建一系列相关的对象,例如不同类型的邮件和短信服务。

抽象工厂接口

```java

public interface MessageServiceFactory {

Message createMessage();

Notification createNotification();

}

```

消息接口和通知接口

```java

public interface Message {

void send();

}

public interface Notification {

void notifyUser();

}

```

具体消息类和通知类

```java

public class EmailMessage implements Message {

@Override

public void send() {

System.out.println("Sending email message.");

}

}

public class SMSMessage implements Message {

@Override

public void send() {

System.out.println("Sending SMS message.");

}

}

public class EmailNotification implements Notification {

@Override

public void notifyUser() {

System.out.println("Notifying user via email.");

}

}

public class SMSNotification implements Notification {

@Override

public void notifyUser() {

System.out.println("Notifying user via SMS.");

}

}

```

具体工厂类

```java

public class EmailServiceFactory implements MessageServiceFactory {

@Override

public Message createMessage() {

return new EmailMessage();

}

@Override

public Notification createNotification() {

return new EmailNotification();

}

}

public class SMSServiceFactory implements MessageServiceFactory {

@Override

public Message createMessage() {

return new SMSMessage();

}

@Override

public Notification createNotification() {

return new SMSNotification();

}

}

```

客户端

```java

public class Client {

public static void main(String[] args) {

MessageServiceFactory emailFactory = new EmailServiceFactory();

Message email = emailFactory.createMessage();

Notification emailNotification = emailFactory.createNotification();

email.send();

emailNotification.notifyUser();

MessageServiceFactory smsFactory = new SMSServiceFactory();

Message sms = smsFactory.createMessage();

Notification smsNotification = smsFactory.createNotification();

sms.send();

smsNotification.notifyUser();

}

}

```

运行结果

```

Sending email message.

Notifying user via email.

Sending SMS message.

Notifying user via SMS.

```

总结

工厂模式通过将对象的创建逻辑封装在工厂类中,提高了代码的灵活性和可维护性。不同的工厂模式适用于不同的场景:

  • **简单工厂模式**:适用于简单的对象创建场景,不需要复杂的继承关系。

  • **工厂方法模式**:适用于需要根据不同条件创建不同对象的场景,通过子类来决定创建哪个对象。

  • **抽象工厂模式**:适用于需要创建一系列相关或依赖对象的场景,通过一个工厂接口来创建多个对象。

通过使用工厂模式,可以更好地管理和扩展对象的创建过程,使代码更加模块化和易于维护。

相关推荐
南山十一少3 小时前
Spring Security+JWT+Redis实现项目级前后端分离认证授权
java·spring·bootstrap
427724004 小时前
IDEA使用git不提示账号密码登录,而是输入token问题解决
java·git·intellij-idea
chengooooooo4 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
李长渊哦5 小时前
常用的 JVM 参数:配置与优化指南
java·jvm
计算机小白一个5 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
黑不溜秋的5 小时前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
付聪12107 小时前
策略模式介绍和代码示例
设计模式
南宫生7 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
计算机毕设定制辅导-无忧学长8 小时前
Maven 基础环境搭建与配置(一)
java·maven
ThereIsNoCode9 小时前
「软件设计模式」状态模式(State)
设计模式·状态模式