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.

```

总结

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

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

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

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

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

相关推荐
四谎真好看13 小时前
Java 黑马程序员学习笔记(进阶篇19)
java·笔记·学习·学习笔记
從南走到北13 小时前
JAVA代泊车接机送机服务代客泊车系统源码支持小程序+APP+H5
java·开发语言·微信小程序·小程序
apocelipes19 小时前
golang unique包和字符串内部化
java·python·性能优化·golang
Full Stack Developme19 小时前
java.text 包详解
java·开发语言·python
刘梦凡呀20 小时前
C#获取钉钉平台考勤记录
java·c#·钉钉
best_virtuoso21 小时前
PostgreSQL 常见数组操作函数语法、功能
java·数据结构·postgresql
yudiandian201421 小时前
02 Oracle JDK 下载及配置(解压缩版)
java·开发语言
楚韵天工21 小时前
宠物服务平台(程序+文档)
java·网络·数据库·spring cloud·编辑器·intellij-idea·宠物
helloworddm21 小时前
Orleans Stream SubscriptionId 生成机制详解
java·系统架构·c#
失散1321 小时前
分布式专题——43 ElasticSearch概述
java·分布式·elasticsearch·架构