设计模式(四)装饰模式

来源:互联网 发布:后序遍历非递归算法 编辑:程序博客网 时间:2024/06/03 07:52

装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。就增加功能来说,装饰模式比生成子类更为灵活。

       为什么说装饰模式比声称子类更为灵活呢?因为装饰模式是采用组合手法,而不是继承的手法。装饰模式实现了在运行时的扩展对象的能力。而且可根据需要扩展多个功能。避免了使用继承带来的灵活性差多子类衍生问题

装饰模式是一种结构型模式,它主要是解决:“过度地使用了继承来扩展对象的功能”,由于继承为类型引入的静态特质,使得这种扩展方式缺乏灵活性;并且随着子类的增多(扩展功能的增多),各种子类的组合(扩展功能的组合)会导致更多子类的膨胀(多继承)。继承为类型引入的静态特质的意思是说以继承的方式使某一类型要获得功能是在编译时。所谓静态,是指在编译时;动态,是指在运行时。

下面通过小菜搬靓,来看一看装饰模式的执行过程:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 装扮2{    class Program    {        static void Main(string[] args)        {            person xc=new person("小菜");            Console.WriteLine("\n第一种装扮:");            Tshirt dtx= new Tshirt ();            BigTrouser  kk= new BigTrouser ();            WearSneakers  pqx= new WearSneakers ();            pqx.decorate(xc);//装饰过程            kk.decorate(pqx);            dtx.decorate(kk);            dtx.Show();            Console.WriteLine("\n第二种装扮:");            Wresuit  xz=new Wresuit ();            wearTie  ld=new wearTie ();            Leathershoes  px= new Leathershoes() ;            xz.decorate(xc);            ld.decorate(xz);//装饰过程            px.decorate(ld);            px.Show();            Console.Read();        }        class person        {            public person()            { }            private string name;            public person(string  name)            {                this.name = name;            }            public  virtual  void Show()            {                Console.WriteLine("装扮{0}",name);            }        }        class Finey :person        {            protected person component;            //打扮            public void decorate (person component)            {                this.component =component    ;            }            public override void Show()            {                if (component !=null)                {                    component.Show();                }            }        }               class Tshirt : Finey        {            public override void Show()            {                Console.Write("大T恤");                base.Show();            }        }        class BigTrouser:Finey        {            public override void Show()            {                Console.Write("垮裤");                base.Show();            }        }        class WearSneakers:Finey         {            public override void Show()            {                Console.Write ("破球鞋");                base.Show();            }        }        class Wresuit:Finey        {            public override void Show()            {                Console.Write("西服");                base.Show();            }        }        class  wearTie:Finey        {            public override void Show()            {                Console.Write("领带");                base.Show();            }        }        class  Leathershoes:Finey        {            public override void Show()             {                Console.Write("皮鞋");                base.Show();             }        }             }}

装饰模式代码已经看过了,那什么时候来选择装饰模式编写代码呢?

1.需要扩展一个类的功能,或给一个类增加附加责任。 

2.需要动态地给一个对象增加功能,这些功能可以再动态地撤销。 

3.需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变得不现实。

下面总结一下装饰模式的优缺点:

优点:

1. 装饰模式与继承关系的目的都是要扩展对象的功能,但是装饰模式可以提供比继承更多的灵活性。

2. 通过使用不同的具体装饰类以及这些装饰类的排列组合,设计师可以创造出很多不同行为的组合

缺点:

1. 这种比继承更加灵活机动的特性,也同时意味着更加多的复杂性。

2. 装饰模式会导致设计中出现许多小类,如果过度使用,会使程序变得很复杂。

3. 装饰模式是针对抽象组件(Component)类型编程。但是,如果你要针对具体组件编程时,就应该重新思考你的应用架构,以及装饰者是否合适。当然也可以改变Component接口,增加新的公开的行为,实现“半透明”的装饰者模式。在实际项目中要做出最佳选择。






0 0