结构型模式之四---Decorator
来源:互联网 发布:淘宝活动流程 编辑:程序博客网 时间:2024/05/18 01:27
1、结构图
2、代码
/*****************************************************************************
* 意图:动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator 模式
* 相比生成子类更为灵活。
*
* 适用性:
* 1、在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
* 2、处理那些可以撤消的职责。
* 3、当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,
* 为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。
* 另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。
*
* 协作:Decorator将请求转发给它的Component对象,并有可能在转发请求前后执行一些附加
* 的动作。
*
* 作者:董怀信
* 日期:2009-6-1
*
* ****************************************************************************/
using System;
namespace DesignPattern.StructuralPattern
{
/// <summary>
/// 定义一个对象接口,可以给这些对象动态地添加职责。
/// </summary>
public interface Component
{
void Operation();
}
/// <summary>
/// 定义一个对象,可以给这个对象添加一些职责。
/// </summary>
public class ConcreteComponent : Component
{
private string m_content;
public ConcreteComponent(string content)
{
m_content = content;
}
#region Component Members
public void Operation()
{
Console.WriteLine("ConcreteComponent:Operation Start;");
Console.WriteLine(m_content);
Console.WriteLine("ConcreteComponent:Operation End;");
}
#endregion
}
/// <summary>
/// 维持一个指向Component对象的指针(引用),并定义一个与Component接口一致
/// 的接口。
/// </summary>
public class Decorator:Component
{
protected Component m_decorator;
public Decorator(Component decorator)
{
m_decorator = decorator;
}
#region Component Members
public virtual void Operation()
{
Console.WriteLine("Decorator:Operation Start;");
m_decorator.Operation();
Console.WriteLine("Decorator:Operaton End;");
}
#endregion
}
/// <summary>
/// 向组件添加职责。
/// </summary>
public class ConcreteDecoratorA:Decorator
{
private bool m_addedState;
public ConcreteDecoratorA(Component decorator):base(decorator)
{
}
public override void Operation()
{
Console.WriteLine("ConcreteDecoratorA:Operation Start;");
Console.WriteLine("AddedState:" + m_addedState.ToString());
m_decorator.Operation();
Console.WriteLine("ConcreteDecoratorA:Operation End;");
}
public bool AddedState
{
get
{
return m_addedState;
}
set
{
m_addedState = value;
}
}
}
/// <summary>
/// 向组件添加职责。
/// </summary>
public class ConcreteDecoratorB : Decorator
{
public ConcreteDecoratorB(Component component)
: base(component)
{
}
public override void Operation()
{
Console.WriteLine("ConcreteDecoratorB:Operation Start;");
m_decorator.Operation();
this.AddedBehavior();
Console.WriteLine("ConcreteDecoratorB:Operation End;");
}
private void AddedBehavior()
{
Console.WriteLine("ConcreteDecoratorB:AddedBehabior();");
}
}
/// <summary>
/// 客户端测试。
/// </summary>
public class Client
{
public void Test()
{
Component concreteComponent = new ConcreteComponent("Please Decorate ConcreteComponent;");
Component decorator = new Decorator(concreteComponent);
decorator.Operation();
Component concreteDecoratorA = new ConcreteDecoratorA(concreteComponent);
concreteDecoratorA.Operation();
Component concreteDecoratorB = new ConcreteDecoratorB(decorator);
concreteDecoratorB.Operation();
}
}
}
- 结构型模式之四---Decorator
- 设计模式笔记-结构型模式之四--Decorator
- 结构型模式之Decorator模式
- 设计模式中结构型模式(四)装饰模式(Decorator)
- 结构型模式三之Decorator
- 结构型模式之装饰DECORATOR
- 结构型模式之装饰(Decorator)
- 结构模式之Decorator(油漆工)
- 结构模式之Decorator(油漆工)
- 【设计模式攻略】结构型模式之Decorator模式
- 结构型模式之装饰模式(Decorator Pattern)C++实现
- 结构型模式之装饰者模式(Decorator)
- 结构型模式之装饰模式(Decorator)
- 结构型模式之装饰模式(Decorator)
- 设计模式(结构型)之装饰者模式(Decorator Pattern)
- 【设计模式】结构型模式之装饰器Decorator
- 7种结构型模式之:装饰器模式(Decorator)
- 九、结构模式之装饰(Decorator)模式
- 类型转换
- 从一则小事说起----漫谈软件调试在开发中的应用
- Java中常见图形绘制方式与实现
- asp.net UrlRewrite 技术的实现
- Dozer 使用 xml文件配置1
- 结构型模式之四---Decorator
- Dozer 使用 xml文件配置2
- 已发表论文列表 (更新日期:2009年1月2日)
- Dozer 使用 xml文件配置3
- 用 PHP V5 开发多任务应用程序
- 中草药藤黄中发现肿瘤生长克星
- 首次观测到宇宙“循环”现象
- 从06到09
- release_content.php源代码分析