设计模式笔记之Decorator Pattern
来源:互联网 发布:中国洪涝灾害数据 编辑:程序博客网 时间:2024/06/14 17:44
装饰模式: Decorator Pattern
概括: 动态地给一个对象添加一些额外的职责。单从增加功能来说,用装饰模式比简单使用子类继承的方式来的灵活。
关键字: Has-a,
重点: 每个装饰类都要实现定义好的实现功能的纯虚装饰函数。各个装饰类都has-a一个拥有基本功能的类,通过对接口的实现,给has-a关系的基础功能增加新的功能
类图:
简单例子: http://en.wikipedia.org/wiki/Decorator_pattern
待装饰的基类:
// The Coffee Interface defines the functionality of Coffee implemented by decoratorpublic interface Coffee { public double getCost(); // returns the cost of the coffee public String getIngredients(); // returns the ingredients of the coffee} // implementation of a simple coffee without any extra ingredientspublic class SimpleCoffee implements Coffee { public double getCost() { return 1; } public String getIngredients() { return "Coffee"; }}
----------------------虚装饰类:
// abstract decorator class - note that it implements Coffee interfaceabstract public class CoffeeDecorator implements Coffee { protected final Coffee decoratedCoffee; // has-a一个待装饰的基类 protected String ingredientSeparator = ", "; public CoffeeDecorator(Coffee decoratedCoffee) { this.decoratedCoffee = decoratedCoffee; } public double getCost() { // implementing methods of the interface return decoratedCoffee.getCost(); } public String getIngredients() { return decoratedCoffee.getIngredients(); }}--------------------------------------------------------------------------------------
实际增加功能的装饰类,即提供给用户使用的装饰类:
// Decorator Milk that mixes milk with coffee// note it extends CoffeeDecoratorpublic class Milk extends CoffeeDecorator { public Milk(Coffee decoratedCoffee) { super(decoratedCoffee); } public double getCost() { // overriding methods defined in the abstract superclass return super.getCost() + 0.5; } public String getIngredients() { return super.getIngredients() + ingredientSeparator + "Milk"; }}--------------------------------------------------------------------------------------
实际用户子类:
public class Main{ public static 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()); ... }}
- 设计模式笔记之Decorator Pattern
- 设计模式之Decorator Pattern
- 设计模式笔记之装饰者模式(Decorator Pattern)
- 设计模式之装饰模式(Decorator Pattern)
- 设计模式之装饰模式--- Pattern Decorator
- 设计模式之二(decorator pattern)
- 设计模式--Decorator Pattern
- 设计模式笔记3:装饰者模式(Decorator Pattern)
- 设计模式学习笔记--装饰者模式(Decorator Pattern)
- 设计模式学习笔记十五:装饰模式(Decorator Pattern)
- 设计模式系列之四:装饰者模式(Decorator Pattern)
- 设计模式之 -- Decorator pattern (修饰模式)
- 设计模式之装饰者模式Decorator Pattern
- 设计模式(二)之装饰模式(Decorator Pattern)
- C#设计模式之装饰者模式(Decorator Pattern)
- JAVA设计模式之 装饰模式【Decorator Pattern】
- 设计模式(结构型)之装饰者模式(Decorator Pattern)
- 设计模式总结之Decorator Pattern(装饰者模式)
- C++中的虚函数的隐藏规则
- [记录]HTC Hero 升级到Android 2.3.4
- 汉澳sinox2010高级桌面服务器版本将彻底免费
- VS2010及各版本的VS快捷键操作
- 由ajax,js,xml,php实现的三级联动省市县下拉功能
- 设计模式笔记之Decorator Pattern
- 常用的关机和重新启动命令
- C__winForm自定义鼠标样式的两种方法
- StringBuffer的应用举例
- 如何调整心态让自己不低落
- C++书推荐
- 对于CIO来说,SOA有什么好处?
- asp.net使用Cookies实现购物车
- 《乔布斯传》作者公开与乔布斯的访谈视频 60 Minutes