(精华)2020年8月13日 C#基础知识点 23种设计模式(结构型模式---装饰器模式)

/// 
    /// 被称之为结构型设计模式的巅峰之作
    /// 组合+继承的融合应用
    /// 
    /// 能够任意扩展要求  还能调整顺序
    /// 
    public class Program
    {
        public static void Show()
        {
            {
                AbstractStudent student = new StudentVip()
                {
                    Id = 123,
                    Name = "小瓶子"
                };
                student = new StudentDecoratorHomework(student);
                student = new StudentDecoratorVideo(student);//包一层
                student = new StudentDecoratorAnswer(student);

                student.Study();
            }
        }
    }
    public abstract class AbstractStudent// : Object
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public abstract void Study();
    }
    /// 
    /// 装饰器 is a  学生
    /// 组合+继承
    /// abstract是为了避免外部直接实例化
    /// 
    public class AbstractDecorator : AbstractStudent
    {
        private AbstractStudent _Student = null;
        public AbstractDecorator(AbstractStudent student) : base()
        {
            this._Student = student;
        }
        public override void Study()
        {
            this._Student.Study();
        }
    }
    /// 
    /// 装饰器 is a  学生
    /// 组合+继承
    /// 
    public class StudentDecoratorAnswer : AbstractDecorator
    {
        public StudentDecoratorAnswer(AbstractStudent student) : base(student)
        {
        }
        public override void Study()
        {
            base.Study();
            Console.WriteLine("获取在线答疑。。。");
        }
    }
    /// 
    /// 装饰器 is a  学生
    /// 组合+继承
    /// 
    public class StudentDecoratorHomework : AbstractDecorator
    {
        public StudentDecoratorHomework(AbstractStudent student) : base(student)
        {
        }
        public override void Study()
        {
            base.Study();
            Console.WriteLine("获取作业巩固。。。");
        }
    }
    /// 
    /// 装饰器 is a  学生
    /// 组合+继承
    /// 
    public class StudentDecoratorVideo : AbstractDecorator
    {
        public StudentDecoratorVideo(AbstractStudent student) : base(student)
        {
        }
        public override void Study()
        {
            base.Study();
            Console.WriteLine("获取视频代码。。。");
        }
    }

你可能感兴趣的:(C#,c#)