码迷,mamicode.com
首页 > 其他好文 > 详细

装饰器模式

时间:2018-07-10 23:31:29      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:str   color   父类   animal   pre   ima   OLE   sum   []   

    /// <summary>
    /// 抽象类 
    /// </summary>
    public abstract class Animal
    {
        public abstract void Show();
    }
    public class Dog : Animal
    {
        public override void Show()
        {
            Console.WriteLine("我是狗");
        }
    }
 /// <summary>
    /// 装饰器吃的
  /// <summary>
    /// 装饰器爱好
    /// </summary>
    public class HobbyDecorator : Animal
    {
        private Animal _animal = null;
        public HobbyDecorator(Animal animal)
        {
     static void Main(string[] args)
        {
            ///装饰器添加属性
            Animal dog = new Dog();
            dog = new HobbyDecorator(dog);
            dog = new EatDecorator(dog);
            dog.Show();
            Console.ReadKey();
        }

 

this._animal = animal;
        }
        public override void Show()
        {
            //调用父类的方法
            this._animal.Show();
            Console.WriteLine("比较喜欢吃屎");

        }
    }

 

方法
    /// </summary>
    public class EatDecorator : Animal
    {
        public Animal _animal = null;
        public EatDecorator(Animal animal)
        {
            _animal = animal;
        }
        public override void Show()
        {
            _animal.Show();
            Console.WriteLine("喜欢吃骨头");
        }
    }

 

装饰器模式

标签:str   color   父类   animal   pre   ima   OLE   sum   []   

原文地址:https://www.cnblogs.com/zxp6/p/9291916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!