跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
最近更改
随机页面
MediaWiki帮助
代码酷
搜索
搜索
中文(中国大陆)
外观
创建账号
登录
个人工具
创建账号
登录
未登录编辑者的页面
了解详情
贡献
讨论
编辑“︁
Java装饰模式
”︁
页面
讨论
大陆简体
阅读
编辑
编辑源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
编辑
编辑源代码
查看历史
常规
链入页面
相关更改
特殊页面
页面信息
外观
移至侧栏
隐藏
您的更改会在有权核准的用户核准后向读者展示。
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
{{DISPLAYTITLE:Java装饰模式}} {{编程概念导航}} == 概述 == '''装饰模式'''(Decorator Pattern)是一种结构型设计模式,允许通过将对象放入包含行为的特殊封装类中来动态地扩展对象的功能。该模式提供了一种灵活的替代方案,比继承更易于维护和扩展,遵循'''开闭原则'''(对扩展开放,对修改封闭)。 === 核心思想 === * 动态地为对象添加额外职责,而无需修改其原始类。 * 通过组合而非继承实现功能扩展。 * 装饰器类与被装饰对象实现同一接口,形成透明嵌套结构。 == 结构 == <mermaid> classDiagram class Component { +operation() } class ConcreteComponent { +operation() } class Decorator { -component: Component +operation() } class ConcreteDecoratorA { +operation() +addedBehavior() } class ConcreteDecoratorB { +operation() +addedState } Component <|-- ConcreteComponent Component <|-- Decorator Decorator o-- Component Decorator <|-- ConcreteDecoratorA Decorator <|-- ConcreteDecoratorB </mermaid> * '''Component''':定义对象的接口,可以是抽象类或接口。 * '''ConcreteComponent''':实现基础功能的具体类。 * '''Decorator''':持有对Component的引用,并实现其接口。 * '''ConcreteDecorator''':具体装饰器,添加新功能或状态。 == 代码示例 == 以下是一个模拟咖啡订单系统的示例,展示如何通过装饰模式动态添加配料(如牛奶、糖)的价格和描述。 === 基础组件 === <syntaxhighlight lang="java"> // 抽象组件 public interface Coffee { double getCost(); String getDescription(); } // 具体组件 public class SimpleCoffee implements Coffee { @Override public double getCost() { return 2.0; // 基础咖啡价格 } @Override public String getDescription() { return "Simple coffee"; } } </syntaxhighlight> === 装饰器实现 === <syntaxhighlight lang="java"> // 抽象装饰器 public abstract class CoffeeDecorator implements Coffee { protected Coffee decoratedCoffee; public CoffeeDecorator(Coffee coffee) { this.decoratedCoffee = coffee; } @Override public double getCost() { return decoratedCoffee.getCost(); } @Override public String getDescription() { return decoratedCoffee.getDescription(); } } // 具体装饰器:牛奶 public class MilkDecorator extends CoffeeDecorator { public MilkDecorator(Coffee coffee) { super(coffee); } @Override public double getCost() { return super.getCost() + 0.5; // 增加牛奶价格 } @Override public String getDescription() { return super.getDescription() + ", with milk"; } } // 具体装饰器:糖 public class SugarDecorator extends CoffeeDecorator { public SugarDecorator(Coffee coffee) { super(coffee); } @Override public double getCost() { return super.getCost() + 0.2; // 增加糖价格 } @Override public String getDescription() { return super.getDescription() + ", with sugar"; } } </syntaxhighlight> === 使用示例 === <syntaxhighlight lang="java"> public class Main { public static void main(String[] args) { Coffee coffee = new SimpleCoffee(); System.out.println("Cost: " + coffee.getCost() + "; Description: " + coffee.getDescription()); coffee = new MilkDecorator(coffee); System.out.println("Cost: " + coffee.getCost() + "; Description: " + coffee.getDescription()); coffee = new SugarDecorator(coffee); System.out.println("Cost: " + coffee.getCost() + "; Description: " + coffee.getDescription()); } } </syntaxhighlight> '''输出''': <pre> Cost: 2.0; Description: Simple coffee Cost: 2.5; Description: Simple coffee, with milk Cost: 2.7; Description: Simple coffee, with milk, with sugar </pre> == 实际应用场景 == * '''Java I/O流''':<code>BufferedReader</code>、<code>InputStreamReader</code>等通过嵌套装饰器增强功能。 * '''GUI组件''':为窗口添加滚动条、边框等动态效果。 * '''Web开发''':中间件链(如权限检查、日志记录)的装饰器实现。 == 优缺点 == {| class="wikitable" |+ 装饰模式分析 ! 优点 !! 缺点 |- | 避免继承导致的类爆炸 || 增加代码复杂度(多层嵌套) |- | 动态扩展对象功能 || 调试困难(需逐层检查装饰器) |- | 符合单一职责原则(每个装饰器独立) || |} == 进阶主题 == * 与[[代理模式]]的区别:装饰器关注增强功能,代理控制访问。 * 多层装饰器的性能影响(如Java I/O的嵌套开销)。 {{设计模式}} [[Category:编程语言]] [[Category:Java]] [[Category:Java设计模式]]
摘要:
请注意,所有对代码酷的贡献均被视为依照知识共享署名-非商业性使用-相同方式共享发表(详情请见
代码酷:著作权
)。如果您不希望您的文字作品被随意编辑和分发传播,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)
该页面使用的模板:
模板:编程概念导航
(
编辑
)
模板:设计模式
(
编辑
)