【软考学习】设计模式——装饰模式
2017-12-17 23:25
357 查看
【背景】
设计模式是非常重要的一块知识,每个设计模式都值得深入了解和学习。
【内容】
结构型设计模式总结:
装饰设计模式总结:
一、定义:动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
二、UML结构图:
三、代码实现:
设计模式是非常重要的一块知识,每个设计模式都值得深入了解和学习。
【内容】
结构型设计模式总结:
装饰设计模式总结:
一、定义:动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
二、UML结构图:
三、代码实现:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 装饰模式 { class Program { static void Main(string[] args) { ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); Console.Read(); } } abstract class Component { public abstract void Operation(); } class ConcreteComponent : Component { public override void Operation() { Console.WriteLine("具体对象的操作"); } } abstract class Decorator : Component { protected Component component; public void SetComponent(Component component) { this.component = component; } public override void Operation() { if (component!=null) { component.Operation(); } } } class ConcreteDecoratorA : Decorator { private string addedState; public override void Operation() { base.Operation(); addedState = "New State"; Console.WriteLine("具体装饰对象A的操作"); } } class ConcreteDecoratorB : Decorator { public override void Operation() { base.Operation(); AddedBehavior(); Console.WriteLine("具体装饰对象B的操作"); } private void AddedBehavior() { } } }
相关文章推荐
- 设计模式学习(九) 装饰模式
- 【软考学习】设计模式——抽象工厂
- 【软考学习】设计模式——责任链模式
- 【软考学习】设计模式——享元模式
- 【软考学习】设计模式——命令模式
- 软考设计模式——装饰模式(C++)
- 装饰模式【设计模式学习-03】
- 【软考学习】设计模式——建造者模式
- 【软考学习】设计模式——备忘录模式
- 【软考学习】设计模式——组合模式
- GOF-23种设计模式——装饰模式(学习笔记)
- 【软考学习】设计模式——适配器模式
- 【软考学习】设计模式——单例模式
- 【软考学习】设计模式——桥接模式
- 【软考学习】设计模式——状态模式
- 【软考学习】设计模式——访问者模式
- 【软考学习】设计模式——原型模式
- 【Python学习日记】【设计模式】装饰器(装饰模式)
- 【软考学习】设计模式——模板方法模式
- 设计模式学习(结构型模式)—装饰模式(Decorator)