zoukankan      html  css  js  c++  java
  • DesignPattern.Ch6.Decorator.Note

    装饰模式(Decorator),动态给一些对象添加额外的职责,就增加功能而言,装饰模式比声明子类更加灵活[DP]。

    装饰模式使用:

        利用SetComponent来对对象进行包装的,这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中的[DEP]。

    装饰模式的原型实现:

    Pattern.Decorator.Prototype.Code
     1 namespace Pattern.Decorator
     2 {
     3     abstract class Component
     4     {
     5         public abstract void Operation();
     6     }
     7 
     8     class ConcreteComponent : Component
     9     {
    10         public override void Operation()
    11         {
    12             Console.WriteLine("具体对象的操作。");
    13         }
    14     }
    15 
    16     abstract class Decorator : Component
    17     {
    18         protected Component component;
    19 
    20         public void SetComponent(Component comimpl)
    21         {
    22             this.component = comimpl;
    23         }
    24 
    25         public override void Operation()
    26         {
    27             if (component != null)
    28                 component.Operation();
    29         }
    30     }
    31 
    32     class CreateDecoratorA : Decorator
    33     {
    34         private string AddedState;
    35         public override void Operation()
    36         {
    37             base.Operation(); ;
    38             this.AddedState = "New state";
    39             Console.WriteLine("具体装饰对象A的操作。");
    40         }
    41     }
    42     class CreateDecoratorB : Decorator
    43     {
    44         public override void Operation()
    45         {
    46             base.Operation();
    47             AddBehavior();
    48             Console.WriteLine("具体装饰对象B的操作。");
    49         }
    50         private void AddBehavior()
    51         {
    52         }
    53     }
    54 }

    装饰模式之菜鸟式:

    Pattern.Decorator.Newbie.Code
     1 //小菜实现的装饰模式
     2 namespace Pattern.Decorator
     3 {
     4     class Person
     5     {
     6         private string Name { get; set; }
     7         public Person()
     8         { }
     9 
    10         public Person(string name)
    11         {
    12             this.Name = name;
    13         }
    14 
    15         public virtual void Show()
    16         {
    17             Console.WriteLine("装饰的名称{0}",this.Name);
    18         }
    19     }
    20 
    21     class Finery : Person
    22     {
    23         protected Person Component { get; set; }
    24 
    25         public void Decorator(Person component)
    26         {
    27             this.Component = component;    
    28         }
    29 
    30         public override void Show()
    31         {
    32             if (this.Component != null)
    33                 this.Component.Show();
    34         }
    35     }
    36 
    37     class TShirts : Finery
    38     {
    39         public override void Show()
    40         {
    41             Console.WriteLine("大T恤。");
    42             base.Show();
    43         }
    44     }
    45     class BigTrouser : Finery
    46     {
    47         public override void Show()
    48         {
    49             Console.WriteLine("垮裤");
    50             base.Show();
    51         }
    52     }
    53 
    54 }

    装饰模式的使用:

    namespace Pattern.Decorator
    {
        class Program
        {
            static void Main(string[] args)
            {
                NewbieDecoratorImpl();
    
                //DecoratorPrototypeTest();
            }
    
            static void NewbieDecoratorImpl()
            {
                Person newbie = new Person();
                TShirts ts = new TShirts();
                BigTrouser bt = new BigTrouser();
                ts.Decorator(newbie);
                bt.Decorator(ts);
                bt.Show();
            }
    
            static void DecoratorPrototypeTest()
            {
                ConcreteComponent cc = new ConcreteComponent();
                CreateDecoratorA ca = new CreateDecoratorA();
                CreateDecoratorB cb = new CreateDecoratorB();
                ca.SetComponent(cc);
                cb.SetComponent(ca);
                cb.Operation();
            }
        }
    }

    装饰模式总结.

    装饰模式是为已有功能动态添加更多功能的一种方式。

    客户代码可以在运行时根据需要有选择、按顺序的使用装饰功能包装对象[DP]。

    装饰模式的优先总结:

    与起初设计相比,把类中的装饰功能从类中搬移去除,可以简化原有的类。这样做更大的好处是把类的核心功能和装饰功能区分开来。而且可以去除相关类中的重复装饰逻辑。

    个人总结:装饰功能就是把组合功能串联起来进行控制,可以动态设置的对象链,一层一层设置抽象的实现对象,主要运用base.Action()逐步(正、反向)调用。

  • 相关阅读:
    两个数组的交集 II
    存在重复
    旋转数组
    从排序数组中删除重复项
    ps 修补工具
    最长公共前缀
    meta 属性
    19年面试总结
    java文件下载以及中文乱码解决
    关于web开发中,图片加载失败替换为默认图片
  • 原文地址:https://www.cnblogs.com/lovey/p/2982438.html
Copyright © 2011-2022 走看看