设计模式之装饰器-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();
}
}
}
设计模式之装饰器-AOP的更多相关文章
- Python装饰器AOP 不定长参数 鸭子类型 重载(三)
1 可变长参数与关键字参数 *args代表任意长度可变参数 **kwargs代表关键字参数 用*args和**kwargs只是为了方便并没有强制使用它们. 缺省参数即是调用该函数时,缺省参数的值若未被 ...
- python设计模式之装饰器详解(三)
python的装饰器使用是python语言一个非常重要的部分,装饰器是程序设计模式中装饰模式的具体化,python提供了特殊的语法糖可以非常方便的实现装饰模式. 系列文章 python设计模式之单例模 ...
- 设计模式:装饰器(Decorator)模式
设计模式:装饰器(Decorator)模式 一.前言 装饰器模式也是一种非常重要的模式,在Java以及程序设计中占据着重要的地位.比如Java的数据流处理,我们可能看到数据流经过不同的类的包装和 ...
- python 设计模式之装饰器模式 Decorator Pattern
#写在前面 已经有一个礼拜多没写博客了,因为沉醉在了<妙味>这部小说里,里面讲的是一个厨师苏秒的故事.现实中大部分人不会有她的天分.我喜欢她的性格:总是想着去解决问题,好像从来没有怨天尤人 ...
- PHP设计模式之装饰器模式(Decorator)
PHP设计模式之装饰器模式(Decorator) 装饰器模式 装饰器模式允许我们给一个类添加新的功能,而不改变其原有的结构.这种类型的类属于结构类,它是作为现有的类的一个包装 装饰器模式的应用场景 当 ...
- python设计模式之装饰器模式
装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...
- PHP设计模式之装饰器模式
装饰器模式:如果已有对象的部分内容或功能性发生改变,但是不需要修改原始对象的结构或不使用继承,动态的扩展一个对象的功能,则应该使用装饰器模式.简单点说:就是我们不应该去修改已有的类,而是通过创建另外一 ...
- [译]Java 设计模式之装饰器
(文章翻译自Java Design Pattern: Decorator – Decorate your girlfriend) 1.装饰模式的来历 让我们假设你在寻找一个女朋友.有来自像没美国中国日 ...
- java设计模式之 装饰器模式
装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构. 这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装 ...
- php设计模式八-----装饰器模式
1.介绍: 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...
随机推荐
- ElasticSearch分页查询的实现
1.设置mapping PUT /t_order { "settings": { "number_of_shards": 1, "number_of_ ...
- #Kruskal重构树,Dijkstra,倍增#洛谷 4768 [NOI2018]归程
题目传送门 分析 首先Dijkstra是必需的(关于SPFA,它死了233) 无向图,所以先求出1号节点到所有点的距离,然后肯定希望起点能驾驶到离一号点最短的汽车可到的地方 但是怎么办,考虑海拔大的边 ...
- #莫比乌斯反演#ZOJ 3435 Ideal Puzzle Bobble SP7001 VLATTICE
ZOJ 3435 Ideal Puzzle Bobble SP7001 VLATTICE - Visible Lattice Points(洛谷题目传送门) SP7001 VLATTICE - Vis ...
- 使用OHOS SDK构建filament
参照OHOS IDE和SDK的安装方法配置好开发环境. 从gitee下载源码. 执行如下命令: git clone https://gitee.com/oh-graphics/filament.git ...
- 6. Eigenvalues and Eigenvectors
Keys: What are Eigenvalues and Eigenvectors? How to find Eigenvalues and Eigenvectors? Applications ...
- Qt多语言动态切换
有个软件,里面做了13种语言,销售要求实现重新设置软件语言后,不需要重启软件,就可以看到软件显示出对应的语言. 软件中所有需要翻译的地方都是用的 tr(QString) 来做的,并且软件是多个窗口 ...
- MCM箱模型建模方法及大气O3来源解析
OBM箱模型可用于模拟光化学污染的发生.演变过程,研究臭氧的生成机制和进行敏感性分析,探讨前体物的排放对光化学污染的影响.箱模型通常由化学机理.物理过程.初始条件.输入和输出模块构成,化学机理是其核心 ...
- Python scipy.ndimage.find_objects用法及代码示例
用法 scipy.ndimage.find_objects(input, max_label=0) 在标记数组中查找对象. 参数: input: 整数数组 包含由不同标签定义的对象的数组.值为 0 的 ...
- HarmonyOS线上Codelabs系列挑战赛第二期:调用三方库,制作酷炫的视觉效果
HarmonyOS线上Codelabs系列挑战赛正如火如荼进行中,开发者们可以通过体验基于HarmonyOS特性和能力的应用开发,快速构建有趣.有用的应用程序.火速加入,与众多开发者一起碰撞想法, ...
- 重新点亮shell————awk 控制语句[十三]
前言 简单介绍一下控制语句. 正文 例子1: 例子2: 例子3 for循环: 例子4, sum会复用: 同样,其他的while 和 do while 也是可以在awk中使用的. 结 下一节awk数组.