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.

```

总结

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

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

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

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

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

相关推荐
大飞哥~BigFei1 小时前
RabbitMq消费延迟衰减重试实现思路
java·分布式·rabbitmq
有趣的野鸭2 小时前
JAVA课程十一次实验课程主要知识点示例
java·前端·数据库
q***07143 小时前
Spring Boot 多数据源解决方案:dynamic-datasource-spring-boot-starter 的奥秘(上)
java·spring boot·后端
q***49864 小时前
Spring Boot 3.4 正式发布,结构化日志!
java·spring boot·后端
沐浴露z6 小时前
【微服务】基本概念介绍
java·微服务
Z3r4y7 小时前
【代码审计】RuoYi-4.7.3&4.7.8 定时任务RCE 漏洞分析
java·web安全·ruoyi·代码审计
Kuo-Teng8 小时前
LeetCode 160: Intersection of Two Linked Lists
java·算法·leetcode·职场和发展
Jooou8 小时前
Spring事务实现原理深度解析:从源码到架构全面剖析
java·spring·架构·事务
盖世英雄酱581368 小时前
commit 成功为什么数据只更新了部分?
java·数据库·后端