装饰模式--私人定制冬装夏装

来源:互联网 发布:python 3开发技术详解 编辑:程序博客网 时间:2024/04/28 05:06

        设计模式中的装饰模式,最大的好处就是可以动态地给一个对象增加一些额外的职责,把类中的装饰功能移除简化原有类。这也体现了面向对象的核心开放-封闭原则。

     装饰模式由4个角色构成:

       (1)抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
  (2)具体构件(Concrete Component)角色:定义一个将要接收附加责任的类。
  (3)装饰(Decorator)角色:持有一个构件(Component)对象的实例,并实现一个与抽象构件接口一致的接口。
  (4)具体装饰(Concrete Decorator)角色:负责给构件对象添加上附加的责任。

     但在具体的实例过程中,有了具体构件或具体装饰也就没有必要再建立抽象构件或装饰了。

    下面就以为电视剧里的”若曦“和”四爷“打造一身 冬装和夏装为例实现一下代码:

     首先是Concrete Component:Person类

      

class Person    {        public Person()        { }        private string name;        public Person(string name)        {            this.name = name;        }        public virtual void Show()        {            Console.WriteLine("装扮的{0}", name);        }    }
     然后是Decorator:Finery类

class Finary : Person    {        protected Person component;        //打扮        public void Decorate(Person component)        {            this.component = component;        }        public override void Show()        {            if (component != null)            {                component.Show();            }        }    }

     ConcreteDecorator:具体的Finery类

 class TShirts : Finary    {        public override void Show()        {            Console.WriteLine("T恤");            base.Show();        }    }    class Shorts : Finary    {        public override void Show()        {            Console.WriteLine("短裤");            base.Show();        }    }    class Snadal : Finary    {        public override void Show()        {            Console.WriteLine("凉鞋");            base.Show();        }    }    class CottonShoes : Finary    {        public override void Show()        {            Console.WriteLine("棉鞋");            base.Show();        }    }    class Jeans : Finary    {        public override void Show()        {            Console.WriteLine("牛仔裤");            base.Show();        }    }    class Scarf : Finary    {        public override void Show()        {            Console.WriteLine("围巾");            base.Show();        }    }    class DownJacks : Finary    {        public override void Show()        {            Console.WriteLine("羽绒服");            base.Show();        }    }
最后是客户端的代码:

class Program    {        static void Main(string[] args)        {            // 夏天的装扮            Person sy = new Person("四爷");            Console.WriteLine("夏天装扮的“四爷”:");                              Snadal lx = new Snadal ();            Shorts dk = new Shorts();            TShirts tx = new TShirts();            //装饰过程            lx.Decorate(sy);            dk.Decorate(lx);            tx.Decorate(dk);            tx.Show();            //冬天的装扮            Person rx = new Person("若曦");            Console.WriteLine("\n\n冬天装扮的“若曦”:");            CottonShoes  mx = new CottonShoes ();            Jeans nj = new Jeans ();            DownJacks yrf = new DownJacks();            Scarf wj = new Scarf();            //装饰过程            mx.Decorate(rx);            nj.Decorate(mx);            yrf.Decorate(nj);            wj.Decorate(yrf);            wj.Show();            Console.Read ();        }    }

结果显示为:



     这样就用装饰模式就为“四爷”和“若曦”穿越回现在的样子打扮好了,只是穿的是不是有点太接地气了呢?想要为他们打造华丽的服饰还是自己试试吧!






12 0