Decorator

装饰器模式(Decorator):可以在程序运行中,为对象动态的增加功能(不修改业务类,可以随意的添加功能,还可以随意调整顺序)。(组合+继承。两种方式的结合)

        static void Main(string[] args)
        {
            AbstractStudent student = new CommonStudent()
            {
                ID = 9527,
                Name = "stephen"
            };
            student = new StudentVip(student);
        }


        public abstract class AbstractStudent
        {
            public int ID { get; set; }
            public string Name { get; set; }

            public abstract void Study();
        }

        public class StudentVip : AbstractStudent//这里用到了继承
        {
            AbstractStudent _student;
            public StudentVip(AbstractStudent s)//这里是组合
            {
                this._student = s;
            }
            public override void Study()
            {
                Console.WriteLine("我是VIP,我可以加点一般学员没有的功能在这里!");
                this._student.Study();
            }
        }
        public class CommonStudent : AbstractStudent
        {
            public override void Study()
            {
                Console.WriteLine("我是普通学员!");
            }
        }

 

posted @ 2019-09-27 15:41  水墨晨诗  阅读(245)  评论(0编辑  收藏  举报