阅读541 返回首页    go 阿里云 go 技术社区[云栖]


设计模式之装饰模式

          装饰模式,动态的给一个对象添加一些额外的职责,就增加功能而言,装饰模式比生成子类更为灵活。这种模式可以有效的将类的核心职责和装饰功能区分开来。

        设计原则:

         1、多用组合,少用继承

            利用继承设计子类的行为,是在编译时静态决定的,而且所有的子类都会继承到相同的行为。然而,如果能够利用组合的做法扩展对象的行为,就可以在运行时动态地进行扩展。

           2、类的设计应对扩展开放,对修改关闭。

         要点:

                       1. 装饰者和被装饰对象有相同的超类型。

                   2. 可以用一个或多个装饰者包装一个对象。

                   3. 装饰者可以在所委托被装饰者的行为之前或之后,加上自己的行为,以达到特定的目的。

                   4. 对象可以在任何时候被装饰,所以可以在运行时动态的,不限量的用你喜欢的装饰者来装饰对象。

                   5. 装饰模式中使用继承的关键是想达到装饰者和被装饰对象的类型匹配,而不是获得其行为。

                   6. 装饰者一般对组件的客户是透明的,除非客户程序依赖于组件的具体类型。在实际项目中可以根据需要为装饰者添加新的行为,做到“半透明”装饰者。

                   7. 适配器模式的用意是改变对象的接口而不一定改变对象的性能,而装饰模式的用意是保持接口并增加对象的职责。

            结构图:


             适用性:

                 1、需要扩展一个类的功能,或给一个类添加附加职责。
                 2、动态的给一个对象添加功能,这些功能可以再动态的撤销。
                 3、当不能采用生成子类的方法进行扩充时。
             例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 装饰模式
{
    class Program
    {
        static void Main(string[] args)
        {
            Person WD = new Person("dd");

            Console.WriteLine("\n第一种装扮");

            Sneakers pqx = new Sneakers();
            BigTrouser kk = new BigTrouser();
            TShirts dtx = new TShirts();
            pqx.Decorate(WD);
            kk.Decorate(pqx);
            dtx.Decorate(kk);

            dtx.Show();

            Console.WriteLine("\n第二种装饰");

            LeatherShoes px = new LeatherShoes();
            Tie ld = new Tie();
            Suit xz = new Suit();

            px.Decorate(WD);
            ld.Decorate(px);
            xz.Decorate(ld);
            xz.Show();

            Console.Read();
        }
        class Person//具体定义了对象,相当于concreteComponent
        {
            public Person()
            { }
            private  string name;
            public Person (string name)
            {
                this.name = name;
            }
            public virtual void Show()
            {
                Console.WriteLine("装饰的{0}",name);
            }
        }
        class Finery:Person //装饰抽象类,相当于decorator
        {
            protected Person component;
            public void Decorate(Person component)
            {
                this.component = component;
            }
            public override void Show()
            {
                if (component != null )
                {
                    component.Show();
                }
            }

        }
        class TShirts:Finery
        {
            public override void Show()
            {
                Console.WriteLine("大体恤");
                base.Show();
            }
        }
        class BigTrouser:Finery
        {
            public override void Show()
            {
                Console.WriteLine("垮裤");
                base.Show();
            }
        }
        class Sneakers:Finery 
        {
            public override void Show()
            {
                Console.WriteLine("破球鞋");
                base.Show();
            }
        }
        class Suit : Finery
        {
            public override void Show()
            {
                Console.WriteLine("西装");
                base.Show();
            }
        }
        class Tie : Finery
        {
            public override void Show()
            {
                Console.WriteLine("领带");
                base.Show();
            }
        }
        class LeatherShoes : Finery
        {
            public override void Show()
            {
                Console.WriteLine("皮鞋");
                base.Show();
            }
        }

    }
}
           装饰模式在此例中产生的效果就是衣服是按顺序穿的,类的核心职责和装饰功能是分开的,使得功能的扩展、应用更加灵活。


                 


最后更新:2017-04-03 12:55:04

  上一篇:go Android Studio 中提示 Private field 'mType' is assigned but never accessed 的原因
  下一篇:go Win7 文件夹无法赋权限