装饰者模式——HeaderFirst学习笔记
来源:互联网 发布:kali linux树莓派 编辑:程序博客网 时间:2024/06/05 05:55
1 装饰者模式定义
动态的将责任附加到对象上,想要扩展功能,装饰者提供有别于继承的另外一种选择。
2 装饰者模式精髓
2.1对扩展开放,对修改关闭
2.2继承属于扩展形式之一,但不见得是达到弹性设计的最佳方案
2.3设计应该允许行为被扩展而无需修改现有代码
2.4装饰者意味着一群装饰者类,这些类用来包装具体的组件
2.5装饰者和被装饰者具备同样的类型
2.6装饰者对组件的客户是透明的,除非客户程序依赖组件具体类型
2.7装饰者会产生很多小对象,运用过度会使得程序复杂
3 例子代码
namespace DecoratorDesignPattern{ public interface Deverage { string getDescription(); double cost(); } public abstract class ConditionDecorator : Deverage { public virtual string getDescription() { return string.Empty; } public virtual double cost() { return 0.0; } } public class HouseBlend : Deverage { public HouseBlend(string description) { m_description = description; } private string m_description; #region Deverage 成员 public string getDescription() { return m_description; } public double cost() { return 100; } #endregion } public class DarkRoast : Deverage { public DarkRoast(string description) { m_description = description; } private string m_description; #region Deverage 成员 public string getDescription() { return m_description; } public double cost() { return 200; } #endregion } public class Decaf : Deverage { public Decaf(string description) { m_description = description; } private string m_description; #region Deverage 成员 public string getDescription() { return "Decaf"; } public double cost() { return 300; } #endregion } public class Espresso : Deverage { public Espresso(string description) { m_description = description; } private string m_description; #region Deverage 成员 public string getDescription() { return "Espresso"; } public double cost() { return 400; } #endregion } public class Moca : ConditionDecorator { private Deverage m_Deverage; public Moca(Deverage deverage) { m_Deverage = deverage; } #region Deverage 成员 public override string getDescription() { return m_Deverage.getDescription()+",Moca"; } public override double cost() { return m_Deverage.cost()+20; } #endregion } public class Soy : ConditionDecorator { private Deverage m_Deverage; public Soy(Deverage deverage) { m_Deverage = deverage; } #region Deverage 成员 public override string getDescription() { return m_Deverage.getDescription() + ",Soy"; } public override double cost() { return m_Deverage.cost() + 30; } #endregion } public class Whip : ConditionDecorator { private Deverage m_Deverage; public Whip(Deverage deverage) { m_Deverage = deverage; } #region Deverage 成员 public override string getDescription() { return m_Deverage.getDescription() + ",Whip"; } public override double cost() { return m_Deverage.cost() + 40; } #endregion }}namespace DecoratorDesignPattern{ class Program { static void Main(string[] args) { Deverage pCoffee = new HouseBlend("深焙咖啡"); Deverage pCondiment = new Moca(pCoffee); pCondiment = new Soy(pCondiment); pCondiment = new Whip(pCondiment); Console.WriteLine("咖啡名字为:" + pCondiment.getDescription()); Console.WriteLine("咖啡价格为:" + pCondiment.cost().ToString()); } }}
4 设计类图
0 0
- 装饰者模式——HeaderFirst学习笔记
- 装饰者模式学习笔记
- 【学习笔记】装饰者模式
- 设计模式-装饰者模式学习笔记
- 模式学习笔记——Decorator/装饰模式
- 设计模式学习笔记——装饰模式
- 设计模式学习笔记——装饰模式
- 设计模式学习笔记——装饰模式
- 设计模式学习笔记——Decorator装饰模式
- 设计模式学习笔记——装饰(Decorator)模式
- 设计模式学习笔记——装饰模式
- 学习笔记——JAVA设计模式<9>装饰模式
- 三、装饰模式——设计模式学习笔记
- 设计模式学习笔记——装饰器模式
- 设计模式学习笔记(六)—Decorator装饰模式
- 《Head First 设计模式》学习笔记——观察者模式 + 装饰者模式
- 装饰模式--学习笔记
- 装饰模式学习笔记
- 【Treap】[CQBZOJ2803]普通平衡树
- CSS:关于“-webkit-appearance: none”样式使用问题
- UITabBarController
- std::shared_ptr简介
- vector的resize()和reserve()函数的区别
- 装饰者模式——HeaderFirst学习笔记
- CSS3画图2
- 创建工程的新工程中,没有Prefix文件怎么办
- OkHttp解析系列-重定向和出错重试
- C++基础::Stream
- 关于STL中vector容器的一些总结
- 添加评论
- PHPDoc/PHPDocumentor生成API文档
- Struts-ValueStack和OGNL总结