设计模式之装饰器-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)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...
随机推荐
- .net 开源混淆器 ConfuserEx
官网:http://yck1509.github.io/ConfuserEx/ 下载地址:https://github.com/yck1509/ConfuserEx/releases 使用参考:htt ...
- #树上差分 or 01-Trie#洛谷 6623 [省选联考 2020 A 卷] 树
题目 分析(01trie) 考虑用trie做需要满足什么操作:加入某个数.01-Trie的合并.全局加一. 主要是全局加一比较难做,考虑改变的地方就是 \(X*2^T+2^T-1\). 把01-Tri ...
- 国产化改造就用 Java Solon,v2.7.3 发布
Java Solon 是什么框架? 是一个可平替 Spring 生态的 Java 应用开发框架.从零开始构建(非 java-ee 架构),有自己的标准规范与开放生态.(历时七年,具备全球第二级别的生态 ...
- IE8页面失去焦点,动态删除element
当页面失去焦点(切换到其他标签页 / 切换到其他软件),触发失焦事件,然后动态删除element 兼容IE8 if(window.ActiveXObject){ window.attachEvent( ...
- 基于istio实现多集群流量治理
本文分享自华为云社区<基于istio实现多集群流量治理>,作者: 可以交个朋友. 一 背景 对多云.混合云等异构基础设施的服务治理是Istio重点支持的场景之一.为了提高服务的可用性,避免 ...
- OOM异常类型总结
OOM是什么?英文全称为 OutOfMemoryError(内存溢出错误).当程序发生OOM时,如何去定位导致异常的代码还是挺麻烦的. 要检查OOM发生的原因,首先需要了解各种OOM情况下会报的异常信 ...
- HarmonyOS音频开发指导:使用AVPlayer开发音频播放功能
如何选择音频播放开发方式 在HarmonyOS系统中,多种API都提供了音频播放开发的支持,不同的API适用于不同音频数据格式.音频资源来源.音频使用场景,甚至是不同开发语言.因此,选择合适的音频 ...
- 课程预告丨12月15日官方直播带你领略ArkUI的声明式开发范式之美
方舟开发框架(ArkUI)的声明式开发范式有什么优势?Java/JS/eTS(extended TypeScript)三种语言,用哪种语言更好? 12月15日 19:00-20:30,Hello Ha ...
- ArkUI,更高效的框架设计
原文:https://mp.weixin.qq.com/s/uSIzuBby7Z92drNDmejKXw,点击链接查看更多技术内容. 上期文章我们讲到了ArkUI的三大特性,同时提到了Ark ...
- npm,registry,镜像源,npm切换源,yarn,cnpm,taobao,nrs
描述 我们在使用 node 的 npm 下载依赖的时候,往往下载速度很慢,那是因为 npm 默认的是 npm 处于国外的官方镜像源.所以需要切换到国内的镜像源来加速依赖下载.所以本文推荐一款简单好用 ...