设计模式——装饰器模型
1.创建基础类
/// <summary> ///抽象的女朋友 /// </summary> public abstract class AbstractGirlFriend { public int Id { get; set; } public string Name { get; set; } /// <summary> /// 化妆 /// </summary> public abstract void MakeUp(); }
/// <summary> /// 装饰器:组合+继承 /// </summary> public class BaseGirlFrienDecorator : AbstractGirlFriend { private AbstractGirlFriend _GirlFriend = null;//用了组合加override public BaseGirlFrienDecorator(AbstractGirlFriend girlFriend) { this._GirlFriend = girlFriend; } public override void MakeUp() { this._GirlFriend.MakeUp(); //Console.WriteLine("****************"); //基类装饰器必须是个空的行为 会重复 } }
2.继承
/// <summary> /// 睫毛 /// </summary> public class GirlFrienEyelashDecorator : BaseGirlFrienDecorator { public GirlFrienEyelashDecorator(AbstractGirlFriend girlFriend) : base(girlFriend)//表示父类的构造函数 { } /// <summary> /// 睫毛 /// </summary> public override void MakeUp() { base.MakeUp(); Console.WriteLine("整理睫毛..."); } }
3.调用
abstractGirlFriend = new BaseGirlFrienDecorator(abstractGirlFriend); //abstractGirlFriend.MakeUp(); abstractGirlFriend = new GirlFrienEyelashDecorator(abstractGirlFriend); abstractGirlFriend.MakeUp();