装饰器模式
Posted zxp6
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了装饰器模式相关的知识,希望对你有一定的参考价值。
/// <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("喜欢吃骨头"); } }
以上是关于装饰器模式的主要内容,如果未能解决你的问题,请参考以下文章