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();
}
}
}

设计模式之装饰器-AOP的更多相关文章

  1. Python装饰器AOP 不定长参数 鸭子类型 重载(三)

    1 可变长参数与关键字参数 *args代表任意长度可变参数 **kwargs代表关键字参数 用*args和**kwargs只是为了方便并没有强制使用它们. 缺省参数即是调用该函数时,缺省参数的值若未被 ...

  2. python设计模式之装饰器详解(三)

    python的装饰器使用是python语言一个非常重要的部分,装饰器是程序设计模式中装饰模式的具体化,python提供了特殊的语法糖可以非常方便的实现装饰模式. 系列文章 python设计模式之单例模 ...

  3. 设计模式:装饰器(Decorator)模式

    设计模式:装饰器(Decorator)模式 一.前言    装饰器模式也是一种非常重要的模式,在Java以及程序设计中占据着重要的地位.比如Java的数据流处理,我们可能看到数据流经过不同的类的包装和 ...

  4. python 设计模式之装饰器模式 Decorator Pattern

    #写在前面 已经有一个礼拜多没写博客了,因为沉醉在了<妙味>这部小说里,里面讲的是一个厨师苏秒的故事.现实中大部分人不会有她的天分.我喜欢她的性格:总是想着去解决问题,好像从来没有怨天尤人 ...

  5. PHP设计模式之装饰器模式(Decorator)

    PHP设计模式之装饰器模式(Decorator) 装饰器模式 装饰器模式允许我们给一个类添加新的功能,而不改变其原有的结构.这种类型的类属于结构类,它是作为现有的类的一个包装 装饰器模式的应用场景 当 ...

  6. python设计模式之装饰器模式

    装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...

  7. PHP设计模式之装饰器模式

    装饰器模式:如果已有对象的部分内容或功能性发生改变,但是不需要修改原始对象的结构或不使用继承,动态的扩展一个对象的功能,则应该使用装饰器模式.简单点说:就是我们不应该去修改已有的类,而是通过创建另外一 ...

  8. [译]Java 设计模式之装饰器

    (文章翻译自Java Design Pattern: Decorator – Decorate your girlfriend) 1.装饰模式的来历 让我们假设你在寻找一个女朋友.有来自像没美国中国日 ...

  9. java设计模式之 装饰器模式

    装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构. 这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装 ...

  10. php设计模式八-----装饰器模式

    1.介绍: 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...

随机推荐

  1. clickhouse在各大厂商的应用

    案例-ClickHouse在头条的技术演进

  2. 【IOT安全】ASA5520基本知识和配置

    本文主要介绍ASA5520防火墙的基本知识和配置 环境搭建 Linux eveng 5.17.8-eve-ng-uksm-wg+ #1 SMP PREEMPT Mon May 16 10:08:59 ...

  3. C#(winform)自定义ListItem类方便ComboBox操作

    public class ListItem { /// <summary> /// Key /// </summary> public string Key { get; se ...

  4. KingbaseES 行级安全策略介绍

    本文详细介绍了KingbaseES中通过CREATE POLICY为一个表定义一条行级安全性策略.注意为了应用已被创建的策略,在表上必须启用行级安全性. 策略名称是针对每个表的.因此,一个策略名称可以 ...

  5. 【已解决】ajax和flask路由传json格式数据出现undefined和object错误

    描述一下问题背景: 前台封装一个json字符串给后台传输数据,后台的ajax获取请求之后把接收的数据显示到前台html表格上. jsonify:这个方法可以把字典转化为json字符串 通过jsonif ...

  6. #轻重链剖分,线段树#洛谷 3703 [SDOI2017]树点涂色

    题目传送门 分析 可以发现相同的颜色一定组成一个连通块. 那么操作2就相当于 \(f_x+f_y-2*f_{lca}+1\) 操作3就相当于询问 \(f\) 的最大值. 最关键的就是操作1,考虑往上跳 ...

  7. #贪心#洛谷 6927 [ICPC2016 WF]Swap Space

    题目 分析 可以发现能将硬盘容量变大的优先,这种硬盘就是以格式化前的大小升序排序. 然后如果硬盘容量变小,那就是先填格式化后较大的硬盘(因为装完可以提供较大的空间) 代码 #include <c ...

  8. 在DAYU200上实现OpenHarmony视频播放器

    内容简介 本文介绍了如何使用ArkUI框架提供的video组件,实现一个具有简易播放器.通过VideoController控制器来控制倍速.全屏.进度调节等功能. 由于使用本地视频文件会影响App的包 ...

  9. C#-GroupBox包含控件,如何获取这些控件的名称

    您可以使用 Enumerable.OfType在GroupBox中查找和投射您的RadioButtons: var radioButtons = groupBox1.Controls.OfType&l ...

  10. HarmonyOS Connect FAQ第四期

    原文:https://mp.weixin.qq.com/s/bvaV086QTnpnDFyYAVxQwQ,点击链接查看更多技术内容.在HarmonyOS Connect生态产品的认证测试环节,你是否存 ...