Decorator pattern
1. 定义,来自wiki(http://en.wikipedia.org/wiki/Decorator_pattern)
The decorator pattern can be used to extend (decorate) the functionality of a certain object statically, or in some cases at run-time, independently of other instances of the same class, provided some groundwork is done at design time. This is achieved by designing a new decoratorclass that wraps the original class. This wrapping could be achieved by the following sequence of steps:
- Subclass the original "Component" class into a "Decorator" class (see UML diagram);
- In the Decorator class, add a Component pointer as a field;
- Pass a Component to the Decorator constructor to initialize the Component pointer;
- In the Decorator class, redirect all "Component" methods to the "Component" pointer; and
- In the ConcreteDecorator class, override any Component method(s) whose behavior needs to be modified.
2. 实例,来自wiki(http://en.wikipedia.org/wiki/Decorator_pattern)
// The abstract Coffee class defines the functionality of Coffee implemented by decorator public abstract class Coffee {public abstract double getCost(); // Returns the cost of the coffeepublic abstract String getIngredients(); // Returns the ingredients of the coffee }// Extension of a simple coffee without any extra ingredients public class SimpleCoffee extends Coffee {public double getCost() {return 1;}public String getIngredients() {return "Coffee";} }
// Abstract decorator class - note that it extends Coffee abstract class public abstract class CoffeeDecorator extends Coffee {protected final Coffee decoratedCoffee;protected String ingredientSeparator = ", ";public CoffeeDecorator (Coffee decoratedCoffee) {this.decoratedCoffee = decoratedCoffee;}public double getCost() { // Implementing methods of the abstract classreturn decoratedCoffee.getCost();}public String getIngredients() {return decoratedCoffee.getIngredients();} }
// Decorator Milk that mixes milk with coffee. // Note it extends CoffeeDecorator. class Milk extends CoffeeDecorator {public Milk (Coffee decoratedCoffee) {super(decoratedCoffee);}public double getCost() { // Overriding methods defined in the abstract superclassreturn super.getCost() + 0.5;}public String getIngredients() {return super.getIngredients() + ingredientSeparator + "Milk";} }// Decorator Whip that mixes whip with coffee. // Note it extends CoffeeDecorator. class Whip extends CoffeeDecorator {public Whip (Coffee decoratedCoffee) {super(decoratedCoffee);}public double getCost() {return super.getCost() + 0.7;}public String getIngredients() {return super.getIngredients() + ingredientSeparator + "Whip";} }// Decorator Sprinkles that mixes sprinkles with coffee. // Note it extends CoffeeDecorator. class Sprinkles extends CoffeeDecorator {public Sprinkles (Coffee decoratedCoffee) {super(decoratedCoffee);}public double getCost() {return super.getCost() + 0.2;}public String getIngredients() {return super.getIngredients() + ingredientSeparator + "Sprinkles";} }
测试类
public class Main {public static final void main(String[] args) {Coffee c = new SimpleCoffee();System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());c = new Milk(c);System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());c = new Sprinkles(c);System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());c = new Whip(c);System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());// Note that you can also stack more than one decorator of the same typec = new Sprinkles(c);System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());}}
输出结果
Cost: 1.0; Ingredients: Coffee Cost: 1.5; Ingredients: Coffee, Milk Cost: 1.7; Ingredients: Coffee, Milk, Sprinkles Cost: 2.4; Ingredients: Coffee, Milk, Sprinkles, Whip Cost: 2.6; Ingredients: Coffee, Milk, Sprinkles, Whip, Sprinkles
3. 优缺点(http://tianli.blog.51cto.com/190322/35287/)
转载于:https://www.cnblogs.com/davidwang456/p/3844770.html
Decorator pattern相关推荐
- 装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 一句话 继承一个抽象类, ...
- Laravel学习笔记之Decorator Pattern
说明:Laravel中Middleware的实现主要利用了Decorator Pattern的设计,本文主要先学习下Decorator Pattern如何实现,为后面学习Middleware的设计做个 ...
- 极速理解设计模式系列:23.装饰器模式(Decorator Pattern)
五个角色:部件(Component).具体部件(ConcreteComponent).装饰抽象类(Decorator).具体装饰对象(ConcreteDecorator).客户端(Client) 部件 ...
- Decorator Pattern - C# 3.0 Design Patterns
Decorator Pattern属于Structural Patterns 介绍: Decorator pattern的作用是提供一种方式动态的给一个对象添加新的职责或状态,被装饰的对象并不知道被& ...
- 二十四种设计模式:装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 介绍 动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活. 示例 有一个Message实体类,某个对象对它的操作有Insert ...
- 装饰器模式(Decorator Pattern)
23种设计模式完整教程 介绍 装饰者模式(Decorator Pattern)是指在不改变原有对象的基础之上,将功能附加到对 象上,提供了比继承更有弹性的替代方案(扩展原有对象的功能),属于结构型模式 ...
- C++实现装饰者模式Decorator Pattern
设计原则5:类应该对扩展开放,对修改关闭. 目标:允许类容易扩展,在不修改现有代码的情况下,就可搭配新的行为. 好处:设计具有弹性可以应对改变,可以接受新的功能来应对改变的需求. 遇到的问题:类数量爆 ...
- Java设计模式 Design Pattern:包装模式 Decorator Pattern
意图 Attach additional responsibilities to an object dynamically. 为一个对象动态的添加职责. Decorators provide a f ...
- 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)
在前两篇博客中详细的介绍了"策略模式"和"观察者模式",今天我们就通过花瓶与鲜花的例子来类比一下"装饰模式"(Decorator Patte ...
最新文章
- python tcp服务器 多线程_Python中的多线程TCP服务器
- hdu2155 小黑的镇魂曲(dp)
- I2C总线之(二)---时序
- lisp画靶子 visual_基于VisualLISP的AutoCAD绘图命令的二次开发_沈良翼
- android studio 配置国内镜像
- java 不允许默认构造_java – 如何使用ObjectMapper去除/序列化不可变对象而不使用默认构造函数?...
- python 条件概率_使用Pymc3的条件概率
- TCP/IP:TCP SYN Flood攻击原理与实现
- 慕课网上的星级评分--学习视频后模仿实现
- CPU调度算法——FCFS算法/SJF算法/优先级调度算法/RR算法
- java 基础知识3
- java mysql 多表查询_MySQL必备知识多表查询
- CMOS图像传感器基本原理
- Crazy Number
- 迅雷服务器有多少硬盘,迅雷真的比BT还伤硬盘吗?
- Centos7 glibc库升级到2.23
- 搭建手机文件服务器,普通用户的低成本家庭文件服务器(伪NAS)的搭建(手机备份篇)...
- Spark SQL 在SparkStreaming中的运用
- pycharm中python代码格式化方法
- pandas 数据结构--DataFrame
热门文章
- 测试晶面间距软件_超逼真动图解析常用15大分析测试仪器,必收藏!SEM, 红外,紫外,核磁,质谱,TEM,ICP等...
- html应用缓存,HTML5应用缓存
- lex编译dos命令_微软新的命令行工具:Windows Terminal
- as3 访问远程计算机,Flash AS3中数据发送与接收
- linux last 命令年份,【帝联运维课堂】(第七十二期)Linux下last命令如何显示年份...
- 苹果cms的php.ini,苹果cms安装及配置详细教程
- C程序背后的故事--头文件、库文件的查找
- win10 配置 maven_home 一会儿成功一会儿失败_在macbook上运行移动硬盘里的win10和macos...
- bagging 与boosting
- 7. Leetcode 611. 有效三角形的个数 (数组-双向双指针)