设计模式之装饰器-AOP
HelloWorld简单例子如下:此例子好好体会下继承 is a和组合 has a的异同。
using System; using System.Runtime.InteropServices; namespace TestEnviroment { class Program { static void Main(string[] args) { BaseClass ins = new Ins(); ins = new Before(ins); ins = new After(ins); ins.Do(); Console.ReadLine(); } } public abstract class BaseClass { public abstract void Do(); } public class Ins : BaseClass { public override void Do() { Console.Write("World"); //底层行为 } } public abstract class BaseDecorator: BaseClass { public BaseClass baseClass; public BaseDecorator(BaseClass baseClass) { this.baseClass = baseClass; } } public class After : BaseDecorator { public After(BaseClass baseClass) : base(baseClass) { } public override void Do() { base.baseClass.Do(); Console.Write(" Jack"); //扩展区 } } public class Before: BaseDecorator { public Before(BaseClass baseClass) : base(baseClass) { } public override void Do() { Console.Write("Hello "); //扩展区 base.baseClass.Do(); } } }
下面是综合例子:
using System; using System.Drawing; using System.Runtime.InteropServices; namespace TestEnviroment { public interface IText { string Content { get; } } /// <summary> /// 状态接口 /// </summary> public interface IState { bool Equals(IState newState); } public interface IDecorator : IText { IState State { get; set; } void Refresh<T>(IState newState) where T : IDecorator; } public abstract class DecoratorBase : IDecorator { protected IText target; public DecoratorBase(IText target) { this.target = target; } public abstract string Content { get; } protected IState state; public IState State { get => this.state; set => this.state = value; } public virtual void Refresh<T>(IState newState) where T : IDecorator { if (this.GetType() == typeof(T)) { if (newState == null) state = null; if (State != null && !State.Equals(newState)) { State = newState; } return; } if (target != null) { ((IDecorator)target).Refresh<T>(newState); } } } public class BoldState : IState { public bool IsBold; public bool Equals(IState newState) { if (newState == null) return false; return ((BoldState)newState).IsBold == IsBold; } } public class ColorState : IState { public Color Color = Color.Black; public bool Equals(IState newState) { if (newState == null) return false; return ((ColorState)newState).Color == Color; } } /// <summary> /// 具体装饰类 /// </summary> public class BoldDecorator : DecoratorBase { public BoldDecorator(IText target) : base(target) { base.state = new BoldState(); } public override string Content { get { if (((BoldState)State).IsBold) return $"<b>{target.Content}</b>"; else return target.Content; } } } /// <summary> /// 具体装饰类 /// </summary> public class ColorDecorator : DecoratorBase { public ColorDecorator(IText target) : base(target) { base.state = new ColorState(); } public override string Content { get { string colorName = ((ColorState)State).Color.Name; return $"<{colorName}>{target.Content}</{colorName}>"; } } } /// <summary> /// 具体装饰类 /// </summary> public class BlockAllDecorator : DecoratorBase { public BlockAllDecorator(IText target) : base(target) { } public override string Content => string.Empty; } public class TextObject : IText { public string Content => "hello"; } class Program { static void Main(string[] args) { IText text = new TextObject(); text = new BoldDecorator(text); text = new ColorDecorator(text); ColorState newColorState = new ColorState(); newColorState.Color = Color.Red; IDecorator root = (IDecorator)text; root.Refresh<ColorDecorator>(newColorState); Console.WriteLine($"color text.Content={text.Content}"); BoldState newBoldState = new BoldState(); newBoldState.IsBold = true; root.Refresh<BoldDecorator>(newBoldState); Console.WriteLine($"bold text.Content={text.Content}"); text = new BlockAllDecorator(text); Console.WriteLine($"text.Content={text.Content}"); Console.ReadLine(); } } }
分类:
面向对象
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)