设计模式之装饰器-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)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...
随机推荐
- 英语文档阅读之Zynq-7000 All Programmable SoC
Read "Zynq-7000 All programmable Soc" 1.Table of Contents 首先是目录,可以看到是标准的总分结构.开头介绍了Zynq的各种功 ...
- 【Pavia】遥感图像数据集下载地址和读取数据集代码
[Pavia]遥感图像数据集下载地址和读取数据集代码 目录 [Pavia]遥感图像数据集下载地址和读取数据集代码 前言 Pavia数据集 Pavia数据集地址: Pavia数据集预览 PaviaU.m ...
- KingbaseES Json 系列十一:Json数组操作函数
KingbaseES Json 系列十一--Json数组操作函数(JSONB_ARRAY_ELEMENTS,JSONB_ARRAY_ELEMENTS_TEXT,JSONB_ARRAY_LENGTH,J ...
- java实战字符串+栈5:解码字符
题目: 有形如 (重复字符串)<重复次数n> 的片段,解码后相当于n个重复字符串连续拼接在一起,求展开后的字符串. 求解: public static String zipString( ...
- C++设计模式 - 适配器模式(Adapter)
接口隔离模式 在组件构建过程中,某些接口之间直接的依赖常常会带来很多问题.甚至根本无法实现.采用添加一层间接(稳定)接口,来隔离本来互相紧密关联的接口是一种常见的解决方案. 典型模式 Facade P ...
- OpenHarmony 官网文档有哪些上新?下篇:设备开发文档上新
为了方便社区开发者更易获取 OpenAtom OpenHarmony(以下简称"OpenHarmony")相关文档,暨上篇应用开发文档上新内容,SIG Docs 小组同步准备了设 ...
- centos环境tomcat配置SSL
环境: centos7.9 tomcat9 jdk1.8 一.阿里云申请 免费SSL 按照官网的方法并未成功启动! 443 80端口加入安全组 阿里云申请免费ssl 下载后解压将localhost-r ...
- openGauss基于4路鲲鹏服务器的性能调优
1.概述 本文主要描述了在4路鲲鹏服务器上,通过软硬件协同优化配置达到openGauss数据库的极致性能的方法. 主要包括软硬件要求.BIOS配置.网卡配置.磁盘配置.服务器参数设置.数据库参数配置. ...
- Mybatis总体框架设计
Mybatis架构概览 Mybatis架构整体设计如下: 接口层和数据库交互的方式 Mybatis和数据库的交互方式有两种 使用传统的MyBatis提供的API: 使用Mapper接口 使用传统的My ...
- HDC2021技术分论坛:鸿蒙智联平台——智能硬件伙伴的最佳拍档
作者:chengjie,鸿蒙智联生态服务平台运营经理 您在产品开发和运营过程中是否遇到过以下问题: 产品开发千头万绪,无从下手?产品上市后不能清晰地了解消费者的使用情况?用户的意见无法及时传递和答复, ...