手機--裝飾者模式

 

    /* 
     * 裝飾者
     * 裝飾者與被裝飾對象有相同的父類
     * 裝飾者內包含被裝飾者對象的引用
     * 
     * 組合替代繼承來擴展對象行爲,運行時根據需求動態擴展
     * 擴展開放,修改關閉。
     * */

    public class Phone { }

    public class IPhone : Phone
    {
        public IPhone() { Console.WriteLine("IPhone"); }
    }
    public class VIVO : Phone
    {
        public VIVO() { Console.WriteLine("VIVO"); }
    }

    public abstract class Decorator : Phone
    {
        public Decorator(Phone phone) { Decorate(); }
        public abstract void Decorate();
    }
    public class Pack : Decorator
    {
        public Pack(Phone phone) : base(phone) { }
        public override void Decorate() { Console.WriteLine("Pack"); }
    }
    public class Film : Decorator
    {
        public Film(Phone phone) : base(phone) { }
        public override void Decorate() { Console.WriteLine("Film"); }
    }

    static void Main(string[] args)
    {
        IPhone iphone = new IPhone();
        Pack packIphone = new Pack(iphone);
        Film filmIphone = new Film(iphone);
        Console.WriteLine("========================");
        Film filmVivo = new Film(new Pack(new VIVO()));
        Console.ReadLine();
    }
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息