目录
-
- 装饰器设计模式最佳实践
-
- [1. 基础demo](#1. 基础demo)
- [2. spring中应用demo](#2. spring中应用demo)
装饰器设计模式最佳实践
装饰器模式是一种结构型设计模式,它允许你通过将对象放入包含行为的特殊封装对象中来为原对象动态地添加职责。这种模式可以用来扩展对象的功能,而无需修改原始类。
以下是一个使用装饰器模式的示例,我们将创建一个简单的咖啡订单系统,其中Coffee是一个接口,SimpleCoffee是一个基础实现,而CoffeeDecorator是一个装饰器基类,其他装饰器类(如MilkDecorator和SugarDecorator)可以继承自它来添加额外的功能。
<<Interface>> Component +operation() ConcreteComponent +operation() Decorator +Component component +Decorator(component: Component) +operation() ConcreteDecoratorA +operation() +addedBehavior() ConcreteDecoratorB +operation() +anotherBehavior()
1. 基础demo
首先,我们定义Coffee接口和基础实现:
java
public interface Coffee {
double cost();
String description();
}
public class SimpleCoffee implements Coffee {
@Override
public double cost() {
return 1.0; // 基础咖啡的价格
}
@Override
public String description() {
return "Simple Coffee";
}
}
然后,我们创建一个装饰器基类:
java
public abstract class CoffeeDecorator implements Coffee {
protected Coffee decoratedCoffee;
public CoffeeDecorator(Coffee decoratedCoffee) {
this.decoratedCoffee = decoratedCoffee;
}
@Override
public double cost() {
return decoratedCoffee.cost();
}
@Override
public String description() {
return decoratedCoffee.description();
}
}
接下来,我们创建具体的装饰器类来添加额外的功能:
java
public class MilkDecorator extends CoffeeDecorator {
public MilkDecorator(Coffee decoratedCoffee) {
super(decoratedCoffee);
}
@Override
public double cost() {
return super.cost() + 0.5; // 牛奶的价格
}
@Override
public String description() {
return super.description() + ", Milk";
}
}
public class SugarDecorator extends CoffeeDecorator {
public SugarDecorator(Coffee decoratedCoffee) {
super(decoratedCoffee);
}
@Override
public double cost() {
return super.cost() + 0.3; // 糖的价格
}
@Override
public String description() {
return super.description() + ", Sugar";
}
}
现在,我们可以使用这些装饰器来创建复杂的咖啡订单:
java
public class CoffeeShop {
public static void main(String[] args) {
Coffee myOrder = new SugarDecorator(new MilkDecorator(new SimpleCoffee()));
System.out.println("Description: " + myOrder.description());
System.out.println("Cost: $" + myOrder.cost());
}
}
输出结果:
Description: Simple Coffee, Milk, Sugar
Cost: $1.8
2. spring中应用demo
首先,我们定义一个基本的MessageService接口和一个实现类:
java
public interface MessageService {
void sendMessage(String msg);
}
public class EmailService implements MessageService {
@Override
public void sendMessage(String msg) {
System.out.println("发送电子邮件:" + msg);
}
}
然后,我们创建一个装饰器接口和实现类:
java
public interface MessageServiceDecorator extends MessageService {
// 可以添加额外的功能
}
public class EmailServiceDecorator implements MessageServiceDecorator {
private MessageService messageService;
public EmailServiceDecorator(MessageService messageService) {
this.messageService = messageService;
}
@Override
public void sendMessage(String msg) {
// 在发送消息之前或之后添加额外的功能
System.out.println("在发送消息之前执行一些操作...");
messageService.sendMessage(msg);
System.out.println("在发送消息之后执行一些操作...");
}
}
在Spring的配置类中,我们定义MessageService bean,并使用它来创建装饰器:
java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public MessageService messageService() {
return new EmailService();
}
@Bean
public MessageService decoratedMessageService(MessageService messageService) {
return new EmailServiceDecorator(messageService);
}
}
最后,我们可以在Spring的上下文中使用这个MessageService bean:
java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessageClient {
private final MessageService messageService;
@Autowired
public MessageClient(MessageService messageService) {
this.messageService = messageService;
}
public void sendMessage(String msg) {
messageService.sendMessage(msg);
}
}