03_装饰模式
2016-11-22 10:16:05 5 举报
《大话设计模式》-装饰模式
作者其他创作
大纲/内容
垮裤
+ 展示形象()
服饰
Decorator
Component Component;
+Operation()
internal class ConcreteComponent : Component { public override void Operation() { Console.WriteLine(\"具体对象的操作\"); } }
领带
ConcreteDecoratorA
AddedState;
Component
internal abstract class Decorator : Component { protected Component Component; public void SetComponent(Component component) { Component = component; } public override void Operation() { if (Component != null) { Component.Operation(); } } }
T恤
皮鞋
static void Main(string[] args) { var component = new ConcreteComponent(); var decoratorA = new ConcreteDecoratorA(); var decoratorB = new ConcreteDecoratorB(); decoratorA.SetComponent(component); decoratorB.SetComponent(decoratorA); decoratorB.Operation(); Console.Read(); }
internal class ConcreteDecoratorA : Decorator { public string AddedState; public override void Operation() { base.Operation(); AddedState = \"New State\"; Console.WriteLine(\"具体装饰对象A的操作\"); } }
人
ConcreteDecoratorB
+Operation()+AddedBehavior()
西服
internal class ConcreteDecoratorB : Decorator { public override void Operation() { base.Operation(); AddedBehavior(); Console.WriteLine(\"具体装饰对象B的操作\"); } private void AddedBehavior() { } }
示例:
ConcreteComponent
+ Operation()
输出
internal abstract class Component { public abstract void Operation(); }
0 条评论
下一页