using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace 装饰者
{
class Program
{
static void Main(string[] args)
{
ConcreteComponent con = new ConcreteComponent();
ConcreteDecoratorA a = new ConcreteDecoratorA();
a.SetComponent(con);
a.Operation();
a.AddBehavior();
Console.WriteLine("============================");
ConcreteDecoratorB B = new ConcreteDecoratorB();
B.SetComponent(a);
a.AddBehavior();
B.Operation();
B.AddBehavior();
}
public abstract class Component
{
public abstract void Operation();
}
abstract class Decorator : Component
{
private Component component;
public void SetComponent(Component component)
{
this.component = component;
}
public override void Operation()
{
if (component != null)
{
component.Operation();
}
}
}
class ConcreteDecoratorA : Decorator
{
public override void Operation()
{
base.Operation();
}
public void AddBehavior()
{
Console.WriteLine("装饰A类的操作!");
}
}
class ConcreteDecoratorB : Decorator
{
public override void Operation()
{
base.Operation();
}
public void AddBehavior()
{
Console.WriteLine("装饰B类的操作!");
}
}
public class ConcreteComponent : Component
{
public override void Operation()
{
Console.WriteLine("具体对象的操作!");
}
}
}
}

C#装饰者模式实例代码的更多相关文章

  1. java 装饰器模式实现代码

    目录 1.实现装饰器模式 1.1.公共接口 1.2.接口实现 1.3.装饰器 1.4.装饰构件 1.5.测试装饰器 上图展示的是io流中的一个装饰者模式的代码结构 1.实现装饰器模式 汽车厂生产汽车实 ...

  2. 设计模式入门,装饰着模式,c++代码实现

    // test03.cpp : Defines the entry point for the console application.////设计模式第3章 装饰者模式#include " ...

  3. php设计模式之桥接模式实例代码

    <?php header("Content-type:text/html;charset=utf-8"); abstract class msg{ protected $se ...

  4. php设计模式之策略模式实例代码

    html <html> <head> <meta charset="UTF-8"> <title>简单计算器</title&g ...

  5. 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)

    在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...

  6. 《javascript设计模式与开发实践》阅读笔记(15)—— 装饰者模式

    装饰者模式 可以动态地给某个对象添加一些额外的职责,而不会影响从这个类中派生的其他对象.在程序开发中,许多时候都并不希望某个类天生就非常庞大,一次性包含许多职责.那么我们就可以使用装饰者模式. 代码例 ...

  7. 23种设计模式之装饰器模式(Decorator Pattern)

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

  8. 【设计模式】Java设计模式 - 装饰者模式

    Java设计模式 - 装饰者模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 原创作品,更多关注我CSDN: 一个有梦有戏的人 准备将博客园.CSDN一起记录分享自 ...

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

    1.装饰器模式的定义(保持接口,扩展功能) Decorate装饰器,顾名思义,就是动态的给一个对象添加一些额外的职责,就好比对房子进行装修一样. 2.装饰器模式的特征 具有一个装饰对象. 必须拥有与被 ...

随机推荐

  1. cordova 使用WKWebView 适配iphoneX及解决不能拨打电话问题

    先安装插件 cordova-plugin-wkwebview-engine 然后修改插件中CDVWKWebViewEngine.m文件,下面是全部代码,修改部分已经进行注释     /* Licens ...

  2. Java String、StringBuilder和StringBuffer

    转载: Java String.StringBuilder和StringBuffer 概览 在Android/Java开发中,用来处理字符串常用的类有3种: String.StringBuilder. ...

  3. 牛客网 牛客小白月赛12 B.华华教月月做数学-A^B mod P-快速幂+快速乘

    链接:https://ac.nowcoder.com/acm/contest/392/B来源:牛客网 华华教月月做数学 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其 ...

  4. COMP COMP-3

    Comp (Computational) Comp (with no suffix) leaves the choice of the data type to the compiler writer ...

  5. FastReport.Net使用:[31]使用带参查询及存储

    带参查询 1.在数据列表中创建一个名为姓名的参数. 然后使用一个对话框,将文本框的ReportParameter(报表参数)选为参数中的姓名. 给童鞋们的一个题目:这里可以改为下拉框,学生列表从数据库 ...

  6. 【atcoder F - Namori】**

    F- Namori http://agc004.contest.atcoder.jp/tasks/agc004_f Time limit : 2sec / Memory limit : 256MB S ...

  7. bzoj 1779

    较水的网络流. /************************************************************** Problem: 1779 User: idy002 L ...

  8. Nginx服务器的启动控制

    写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------主要内容包括: nginx服 ...

  9. python开发_tempfile

    python中的tempfile模块,是为创建临时文件(夹)所提供的 如果你的应用程序需要一个临时文件来存储数据,但不需要同其他程序共享,那么tempfile模块来创建临时文件(夹)是个不错的选择 其 ...

  10. Debounce 和 Throttle 的原理及实现---防止频繁触发某事件

    原文:http://blog.csdn.net/redtopic/article/details/69396722 在处理诸如 resize.scroll.mousemove 和 keydown/ke ...