装饰模式:通过将所有的具体类继承自同一个类,并且在具体的类中引用父类的对象,来达到任意搭配具体类中方法的执行顺序的目的(人穿衣服的例子;) #region 装饰模式 class Person { private string name; public String Name { set { name = value; } get { return name; } } public Person(string nameStr) { this.name = nameStr; } public Person() { } public virtual void Show() { Console.WriteLine("装饰的{0}", this.name); } } class Decorator : Person { private Person _person; public void SetDecorator(Person person) { this._person = person; } public override void Show() { if (_person != null) { _person.Show(); } } } class TakeShoes : Decorator { public override void Show() { Console.Write(" 鞋子 "); base.Show(); } } class TakeTShirts : Decorator { public override void Show() { Console.Write(" T恤 "); base.Show(); } } class TakeTrousers : Decorator { public override void Show() { Console.Write(" 大裤子 "); base.Show(); } } #endregion class Program { static void Main(string[] args) { #region 装饰模式 Console.WriteLine("装饰模式效果演示:"); Person xiaoQiang = new Person("小强"); TakeShoes tShoes = new TakeShoes(); TakeTrousers tTrousers = new TakeTrousers(); TakeTShirts tTShirts = new TakeTShirts(); tShoes.SetDecorator(xiaoQiang); tTShirts.SetDecorator(tShoes); tTrousers.SetDecorator(tTShirts); tTrousers.Show(); tTShirts.Show(); #endregion } }