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

    价值:在不改变对象原有行为的基础上,增加新的行为和属性。

    装饰器模式 UML图:

    代码如下:

     一、被装饰类

        #region 被装饰类
        public abstract class Person
        {
            public string name;
            public int age;
            public Person()
            {
    
            }
            public Person(string name,int age=20)
            {
                this.name = name;
                this.age = age;
            }
    
            public virtual void Show()
            {
                Console.WriteLine(string.Format("我的名字:{0},年龄:{1}",name,age));
            }
        }
    
        /// <summary>
        /// 白人子类
        /// </summary>
        public class WhitePerson : Person
        {
            public WhitePerson()
            {
    
            }
            public WhitePerson(string name)
                : base(name)
            {
    
            }
            public override void Show()
            {
                base.Show();
                Console.WriteLine(string.Format("肤色:{0}", "White"));
            }
    
        }
        #endregion
    被装饰类

    二、装饰类

        #region 装饰类
        /// <summary>
        /// 装饰类
        /// 1、继承Person,目的是在装饰A后的A1,将A1传递给B,再装饰为B1,不继承的话,无法将A1传递给B
        /// 2、引用Person的目的,是为了将A1当做参数传递给B,否则无法传递
        /// </summary>
        public abstract class Decorator:Person
        {
            protected Person person=null;
            public void Decorate(Person person)           
            {
                this.person = person;
            }
    
            public override void Show()
            {
                if(person!=null)
                    person.Show();//此处是Person.Show(),而非 base.show()
            }
        }
    
        public class TShirtsDecorator : Decorator
        {
            public override void Show()
            {
                base.Show();     
                Console.Write(" TShirt ");
                                  
            }
        }
    
        public class BigTrouseDecorator : Decorator
        {
            public override void Show()
            {
                base.Show();
                Console.Write(" BigTrouse ");
                
            }
    
        }
    
        public class SneakersDecorator : Decorator 
        {
            public override void Show()
            { 
                base.Show();
                Console.Write(" Sneakers ");
               
            }
        }
        #endregion
    装饰类

    三、外部调用,进行装饰

     class Program
        {
            static void Main(string[] args)
            {
                //被装饰对象
                Person person = new WhitePerson("张三");
    
                //第一种装饰
                Console.WriteLine("第一种装饰:");
                TShirtsDecorator tShirts = new TShirtsDecorator();
                BigTrouseDecorator bigTrouse = new BigTrouseDecorator();
                SneakersDecorator sneakers = new SneakersDecorator();
    
                tShirts.Decorate(person);
                bigTrouse.Decorate(tShirts);//将tShirts当做参数,而非person
                sneakers.Decorate(bigTrouse);
    
                sneakers.Show();
    
    
    
                //第二种装饰
                Console.WriteLine("
    第二种装饰:");
    
                TShirtsDecorator tShirts2 = new TShirtsDecorator();
                TShirtsDecorator tShirts22 = new TShirtsDecorator();
                BigTrouseDecorator bigTrouse2 = new BigTrouseDecorator();
                SneakersDecorator sneakers2 = new SneakersDecorator();
    
    
                tShirts2.Decorate(person);
                tShirts22.Decorate(tShirts2);
                bigTrouse2.Decorate(tShirts22);//将tShirts当做参数,而非person
                sneakers2.Decorate(bigTrouse2);
    
                sneakers2.Show();
    
                Console.Read();
    
            }
        }
    外部进行装饰
  • 相关阅读:
    TUXEDO启动常见错误和解决方法 动常见错误和解决方法
    tuxedo远程客户端无法访问类故障(持续更新ing)
    ORA-00845: MEMORY_TARGET not supported on this system
    vim使用方法
    GP_CAT:209: ERROR: Write error 27, file /app/dir/dom/log/ULOG.011508
    txuedo TMS_ORACLE启动失败
    type类型定义
    oralce
    登录指定端口的ftp_server
    Failed to initialize NVML: Driver/library version mismatch
  • 原文地址:https://www.cnblogs.com/qiupiaohujie/p/11958943.html
Copyright © 2011-2022 走看看