设计模式

设计模式 - 装饰模式

2016-01-25  本文已影响59人  Mitchell
using System;
namespace Factory1
{
    //人
    class Person{
        public Person(){}
        private string name;
        public Person(string name)
        {
            this.name = name;
        }
        public virtual void Show()
        {
            Console.WriteLine ("\nresult = {0}", name);
        }
    }
    //装饰 人
    class Finery:Person{
        protected Person component;
        public void Decorate(Person component)
        {
            this.component = component;
        }
        public override void Show()
        {
            if (component != null) {
                component.Show ();
            }
        }
    }
    //装饰 人 + Tshirt
    class TShirts:Finery{
        public override void Show()
        {
            Console.Write ("T-Shirt");
            base.Show ();
        }
    }
    //装饰 人 + BigTrouser
    class BigTrouser:Finery{
        public override void Show(){
            Console.Write("BigTrouser");
            base.Show ();
        }
    }
    class MainClass
    {
        public static void Main (string[] args)
        {
            Person sc = new Person ("little");
            Console.WriteLine ("the First category");
            TShirts ts = new TShirts ();
            BigTrouser bt = new BigTrouser ();
            ts.Decorate (sc);
            bt.Decorate (ts);
            bt.Show ();
            Console.Read ();
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读