手机--装饰者模式

 

    /* 
     * 装饰者
     * 装饰者与被装饰对象有相同的父类
     * 装饰者内包含被装饰者对象的引用
     * 
     * 组合替代继承来扩展对象行为,运行时根据需求动态扩展
     * 扩展开放,修改关闭。
     * */

    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();
    }

 

posted @ 2016-08-05 13:37  贴心小冰棍  阅读(246)  评论(0编辑  收藏  举报