zoukankan      html  css  js  c++  java
  • 装饰模式

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    namespace 装饰模式v2._1 {    

        class Person    

        {        

             public Person() { }

             public string name;        

             public Person(string name)        

             {            

                     this.name = name;        

              }

              public virtual void Show()        

             {            

                     Console.WriteLine("装扮的{0}", name);        

              }    

         }

        /// <summary>   

        /// 服装类    

        /// </summary>    

        class Finery : Person    

        {              

            protected Person component;                

            //打扮        

            public void Decorate(Person component)        

            {      

                   this.component = component;       

             }

            public override void Show()        

            {            

                if (component != null)            

                {                

                    component.Show();            

                 }       

             }    

        }

        class Sneakers : Finery    

    {

            public override void Show()

            {

                Console.Write("运动鞋 ");

                base.Show();

            }

        }

        class BigTrouser : Finery

        {

            public override void Show()

            {

                Console.Write("垮裤 ");

                base.Show();

            }

        }

        class TShirts : Finery

        {

            public override void Show()

            {

                Console.Write("大T恤 ");

                base.Show();

            }

        }

        //其余类类似,省略     //... ...

    }

    //Main程序

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    /*  

    *为已有功能动态地添加更多更能的一种方式,  

    *当系统需求新功能的时候,是向旧的类中添加新的代码,

     *这些新加的代码通常装饰了所有类的核心职责或主要行为。

     *这种方式在主类中加人了新的字段,方法和逻辑,从而增加了主类的复杂度,  

    *而这些新加入的东西仅仅是为了满足一些只在某种特定情况下才会执行的特殊行为的需求,  

    *而装饰模式却提供了一个非常好的方案,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象。  

    *因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地,按顺序的装饰功能包装对象了。

     *好处是有效的把类的核心职责和装饰功能区分开了。而且可以去除相关类中重复的装饰逻辑。

     */

    namespace 装饰模式v2._1

    {    

        class Program    

        {

            static void Main(string[] args)

            {

                Person xc = new Person("小菜");

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

                Sneakers pqx1 = new Sneakers();

                TShirts p1 = new TShirts();

                BigTrouser pq1 = new BigTrouser();

                //运动鞋类调用服装类的show方法,

                //并通过打扮方法给服装类中的Person类component对象附上xc的值,

                //再通过xc调用show方法调用person类的show方法。打印出来这个。

                pqx1.Decorate(xc);

                pqx1.Show();

                //T恤类的show方法跳转到person时,由于对象是穿着运动鞋的人,

                //所以又跳转到运动鞋类的show方法,其余与上面描述类似。

                p1.Decorate(pqx1);

                p1.Show();

                pq1.Decorate(p1);

                pq1.Show();

                Console.Read();

            }

        }

    }

    大家有好的东西可以给我一个链接。谢谢了。o(∩_∩)o

  • 相关阅读:
    获取物料批次特性取值BAPI
    获取域的固定值的函数:DD_DOMVALU…
    CJ20N中对WBS元素执行删除操作时触…
    SM30相关操作
    CL03对应的BAPI(根据类查特性)
    两个数值交换
    char*,string,float,int 转换
    字符串 分割
    C++ 关于I/O
    TCP
  • 原文地址:https://www.cnblogs.com/loveyatou/p/2970508.html
Copyright © 2011-2022 走看看