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

版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/u010858791/article/details/26160739

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

     装饰模式由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();
        }
    }
最后是client的代码:

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 ();
        }
    }

结果显示为:



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






posted on 2019-04-02 18:03  xfgnongmin  阅读(124)  评论(0编辑  收藏  举报

导航