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.

```

总结

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

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

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

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

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

相关推荐
似水明俊德17 分钟前
04-C#.Net-委托和事件-面试题
java·开发语言·面试·c#·.net
好家伙VCC40 分钟前
# 发散创新:用 Rust构建高性能游戏日系统,从零实现事件驱动架构 在现代游戏开发中,**性能与可扩展性**是核心命题。传统基于
java·python·游戏·架构·rust
爱笑的源码基地1 小时前
门诊his系统源码,中西医结合的数字化门诊解决方案
java·spring boot·源码·二次开发·门诊系统·云诊所系统·诊所软件源码
庞轩px1 小时前
缓存Key设计的“七要七不要”
java·jvm·redis·缓存
小璐资源网1 小时前
Java 21 新特性实战:虚拟线程详解
java·开发语言·python
SimonKing1 小时前
全网爆火的OpenClaw保姆级教程Linux版,它来了。
java·后端·程序员
于慨1 小时前
tauri
java·服务器·前端
WZTTMoon1 小时前
从互斥锁到无锁,Java 20年并发安全进化史
java·python·安全
2501_918126911 小时前
学习所有6502写游戏控制器的语句
java·linux·网络·汇编·嵌入式硬件
青春易逝丶2 小时前
策略模式
java·开发语言·策略模式