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.

```

总结

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

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

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

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

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

相关推荐
TT哇16 分钟前
【Java EE初阶】计算机是如何⼯作的
java·redis·java-ee
Fireworkitte7 小时前
Apache POI 详解 - Java 操作 Excel/Word/PPT
java·apache·excel
weixin-a153003083167 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
DCTANT7 小时前
【原创】国产化适配-全量迁移MySQL数据到OpenGauss数据库
java·数据库·spring boot·mysql·opengauss
Touper.7 小时前
SpringBoot -- 自动配置原理
java·spring boot·后端
黄雪超8 小时前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice8 小时前
对象的finalization机制Test
java·开发语言·jvm
GodKeyNet8 小时前
设计模式-模板模式
设计模式·模板模式
望获linux9 小时前
【实时Linux实战系列】CPU 隔离与屏蔽技术
java·linux·运维·服务器·操作系统·开源软件·嵌入式软件
JosieBook9 小时前
【Java编程动手学】使用IDEA创建第一个HelloJava程序
java·开发语言·intellij-idea