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. 使用Go语言开发一个短链接服务:五、添加和获取短链接

    章节  使用Go语言开发一个短链接服务:一.基本原理  使用Go语言开发一个短链接服务:二.架构设计  使用Go语言开发一个短链接服务:三.项目目录结构设计  使用Go语言开发一个短链接服务:四.生成 ...

  2. Lambda表达式编写递归函数

    class Program { //Fix求出的是函数f的不动点,它就是我们所需要的递归函数: static Func<T, TResult> Fix<T, TResult>( ...

  3. arch xfce启用自动挂载usb设备,自动访问usb设备,自动连接usb设备

    1.安装gvfs sudo pacman -S gvfs GVFS(Gnome Virtual File System)是一个用于 GNOME 桌面环境的虚拟文件系统,它提供了一种统一的方式来访问和管 ...

  4. 第十三届蓝桥杯大赛软件赛省赛【Java 大学B 组】试题D: 最少刷题数

    1 import java.util.ArrayList; 2 import java.util.Scanner; 3 4 public class Main { 5 public static vo ...

  5. 链表队列(LinkedListQueue)

    栈操作 入队 template<typename T> void LinkedListQueue<T>::enqueue(T e) { if (tail == nullptr) ...

  6. 使用OHOS SDK构建box2d

    参照OHOS IDE和SDK的安装方法配置好开发环境. 从github下载源码,当前最新的提交记录ID为411acc32eb6d4f2e96fc70ddbdf01fe5f9b16230. 执行如下命令 ...

  7. vc修改ocx控件classID

    背景: 客户希望,classID 为客户自已定义好的一个 ID ,由于我们自动生成的 classID 和客户定义的 ID 不同,所以我们需要修改 classID 为客户指定的 ID 解决方法: 1.打 ...

  8. 鼠标移动出现雪花-js实现

    // 鼠标移动出现雪花.html <!DOCTYPE html> <html> <head> <title></title> <scr ...

  9. openGauss3.1.0 版本的gs_stack功能解密

    openGauss3.1.0 版本的 gs_stack 功能解密 不管是测试还是研发,工作中总有遇到各种各样的问题.比如,你有没有遇到过在数据库中执行某个 SQL,却一直不返回结果,这时候的你是不是非 ...

  10. HarmonyOS—使用Web组件加载页面

      页面加载是Web组件的基本功能.根据页面加载数据来源可以分为三种常用场景,包括加载网络页面.加载本地页面.加载HTML格式的富文本数据. 页面加载过程中,若涉及网络资源获取,需要配置ohos.pe ...