您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Java设计模式中装饰者模式怎么实现
## 一、装饰者模式概述
装饰者模式(Decorator Pattern)是一种结构型设计模式,它允许向一个现有的对象动态添加新的功能,同时又不改变其结构。这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。
### 1.1 模式定义
装饰者模式通过将对象放入包含行为的特殊封装对象中来为原对象绑定新的行为,遵循**开闭原则**(对扩展开放,对修改关闭)。
### 1.2 核心角色
- **Component(抽象组件)**:定义对象的接口
- **ConcreteComponent(具体组件)**:实现抽象组件的具体类
- **Decorator(抽象装饰类)**:继承/实现Component并持有Component引用
- **ConcreteDecorator(具体装饰类)**:实现具体的装饰逻辑
## 二、装饰者模式实现步骤
### 2.1 基础实现代码框架
```java
// 1. 定义抽象组件
public interface Component {
void operation();
}
// 2. 实现具体组件
public class ConcreteComponent implements Component {
@Override
public void operation() {
System.out.println("具体组件的操作");
}
}
// 3. 定义抽象装饰类
public abstract class Decorator implements Component {
protected Component component;
public Decorator(Component component) {
this.component = component;
}
@Override
public void operation() {
component.operation();
}
}
// 4. 实现具体装饰类
public class ConcreteDecoratorA extends Decorator {
public ConcreteDecoratorA(Component component) {
super(component);
}
@Override
public void operation() {
super.operation();
addedBehavior();
}
private void addedBehavior() {
System.out.println("装饰器A新增的行为");
}
}
public class Client {
public static void main(String[] args) {
// 原始对象
Component component = new ConcreteComponent();
// 第一次装饰
component = new ConcreteDecoratorA(component);
// 第二次装饰
component = new ConcreteDecoratorB(component);
component.operation();
}
}
假设我们需要设计一个咖啡订单系统,基础咖啡(如美式咖啡)可以添加多种调料(牛奶、摩卡、糖等),每种调料都会影响最终价格。
Beverage(抽象组件)
/ | \
/ | \
Americano CondimentDecorator(抽象装饰类)
/ \
/ \
Milk Mocha
// 抽象组件:饮料
public abstract class Beverage {
protected String description = "Unknown Beverage";
public String getDescription() {
return description;
}
public abstract double cost();
}
// 具体组件:美式咖啡
public class Americano extends Beverage {
public Americano() {
description = "Americano Coffee";
}
@Override
public double cost() {
return 18.0;
}
}
// 抽象装饰类:调料
public abstract class CondimentDecorator extends Beverage {
protected Beverage beverage;
public CondimentDecorator(Beverage beverage) {
this.beverage = beverage;
}
@Override
public abstract String getDescription();
}
// 具体装饰类:牛奶
public class Milk extends CondimentDecorator {
public Milk(Beverage beverage) {
super(beverage);
}
@Override
public String getDescription() {
return beverage.getDescription() + ", Milk";
}
@Override
public double cost() {
return beverage.cost() + 3.5;
}
}
// 具体装饰类:摩卡
public class Mocha extends CondimentDecorator {
public Mocha(Beverage beverage) {
super(beverage);
}
@Override
public String getDescription() {
return beverage.getDescription() + ", Mocha";
}
@Override
public double cost() {
return beverage.cost() + 5.0;
}
}
public class CoffeeShop {
public static void main(String[] args) {
// 点一杯美式咖啡
Beverage beverage = new Americano();
System.out.println(beverage.getDescription()
+ " ¥" + beverage.cost());
// 加牛奶
beverage = new Milk(beverage);
System.out.println(beverage.getDescription()
+ " ¥" + beverage.cost());
// 再加摩卡
beverage = new Mocha(beverage);
System.out.println(beverage.getDescription()
+ " ¥" + beverage.cost());
}
}
// 典型的装饰者模式实现
InputStream in = new FileInputStream("test.txt");
// 缓冲装饰
in = new BufferedInputStream(in);
// 数据输入装饰
in = new DataInputStream(in);
List<String> list = new ArrayList<>();
// 装饰为不可修改列表
List<String> unmodifiableList = Collections.unmodifiableList(list);
// 装饰为同步列表
List<String> synchronizedList = Collections.synchronizedList(list);
装饰者模式通过组合而非继承的方式实现了功能的动态扩展,是Java设计模式中非常实用的一种结构型模式。掌握装饰者模式可以帮助我们设计出更加灵活、可扩展的系统架构,特别是在需要动态添加对象功能的场景下表现出色。 “`
该文章共计约2100字,详细介绍了装饰者模式的概念、实现步骤、实际案例、优缺点分析以及与其他模式的对比,采用Markdown格式编写,包含代码示例和结构化标题。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。