using DesignPattern.StructuralPattern.DecoratorPattern.Decorator;
using System;
using System.Collections.Generic;
using System.Text;
namespace DesignPattern.StructuralPattern.DecoratorPattern
{
public class DecoratorPattern
{
//装饰器跟链表很像,每一个对象中都有一个引用(引用的类型和本对象相同),对象方法执行时都要先执行引用中的方法,这样就会循序执行(如果后执行引用方法,就倒叙执行)
//通过组合+继承 完成对象功能的动态扩展(随意扩展,并且循序可变)
//其他的结构型设计模式:
// 外观模式(门面模式)、组合模式、桥接模式、享元模式
public static void Show()
{
AbstractStudent student = new StudentVip()
{
Name = "小高"
};
student = new ListenLiveDecorator(student);
student = new HomeworkDecorator(student);
student = new ReviewDecorator(student);
student.Study();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DesignPattern.StructuralPattern.DecoratorPattern
{
public abstract class AbstractStudent
{
public virtual string Name { set; get; }
public abstract void Study();
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DesignPattern.StructuralPattern.DecoratorPattern
{
public class StudentFree : AbstractStudent
{
public override string Name { get; set; }
public override void Study()
{
Console.WriteLine($"免费班{Name}在学习");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DesignPattern.StructuralPattern.DecoratorPattern
{
public class StudentVip : AbstractStudent
{
public override string Name { get; set; }
public override void Study()
{
Console.WriteLine($"Vip班{Name}在学习");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DesignPattern.StructuralPattern.DecoratorPattern.Decorator
{
public class BaseDecorator : AbstractStudent
{
/// <summary>
/// 让子类都有了这个字段,子类直接调用这个字段的Study方法,就不用调用父类的方法了
/// 让这个字段私有,子类必须通过父类的Study方法,这样父类的方法会重复调用
/// </summary>
private AbstractStudent _student = null;
public BaseDecorator(AbstractStudent student)
{
_student = student;
}
/// <summary>
/// 基础装饰器中的动作会重复执行
/// </summary>
public override void Study()
{
_student.Study();
//这步会重复打印
Console.WriteLine("********************************");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DesignPattern.StructuralPattern.DecoratorPattern.Decorator
{
public class ListenLiveDecorator : BaseDecorator
{
public ListenLiveDecorator(AbstractStudent student) : base(student)
{
}
public override void Study()
{
base.Study();
//_student.Study();
Console.WriteLine("现场听讲");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DesignPattern.StructuralPattern.DecoratorPattern.Decorator
{
public class HomeworkDecorator : BaseDecorator
{
public HomeworkDecorator(AbstractStudent student) : base(student)
{
}
public override void Study()
{
base.Study();
//_student.Study();
Console.WriteLine("家庭作业");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DesignPattern.StructuralPattern.DecoratorPattern.Decorator
{
public class ReviewDecorator : BaseDecorator
{
public ReviewDecorator(AbstractStudent student) : base(student)
{
}
public override void Study()
{
base.Study();
//_student.Study();
Console.WriteLine("视频回看");
}
}
}