using System;
![](/Images/OutliningIndicators/None.gif)
namespace Decorator.DecoratorFolder
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 装饰模式:在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
///处理那些可以撤消的职责。
///当不能采用生成子类的方法进行扩充时。
/// 一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。
/// 另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类
/// </summary>
public interface Work
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
void insert();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 装饰模式:动态给一个对象添加一些额外的职责,就象在墙上刷油漆.
/// 使用Decorator模式相比用生成子类方式达到功能的扩充显得更为灵活
/// 目的:
/// 我们通常可以使用继承来实现功能的拓展,如果这些需要拓展的功能的种类很繁多,
/// 那么势必生成很多子类,增加系统的复杂性,同时,使用继承实现功能拓展,我们必须可预见这些拓展功能,
///这些功能是编译时就确定了,是静态的.
///使用Decorator的理由是:这些功能需要由用户动态决定加入的方式和时机.
/// Decorator提供了"即插即用"的方法,在运行期间决定何时增加何种功能
/// </summary>
public class SystemDecorator:Work
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
私有变量#region 私有变量
private Work work;
private ArrayList other = new ArrayList();
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
public SystemDecorator(Work work)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.work = work;
other.Add("挖坑");
other.Add("钉木板");
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Work 成员#region Work 成员
![](/Images/OutliningIndicators/InBlock.gif)
public void insert()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.newMethod();
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Decorator 成员#region Decorator 成员
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private void newMethod()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.otherMethod();
work.insert();
Console.WriteLine();
}
![](/Images/OutliningIndicators/InBlock.gif)
private void otherMethod()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int index =0;index <other.Count; index ++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(other[index].ToString() + "正在进行!");
}
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// SquarePeg 的摘要说明。
/// </summary>
public class SquarePeg :Work
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public SquarePeg()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Work 成员#region Work 成员
![](/Images/OutliningIndicators/InBlock.gif)
public void insert()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Console.WriteLine("方形桩插入");
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
///以下为测试代码
/// 子类化和Decorator模式的主要区别是:
/// 采用子类化,你同一个类打交道;
/// 使用Decorator模式,你可以动态的修改多个对象。
/// </summary>
class DecoratorTest
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: 在此处添加代码以启动应用程序
//
![](/Images/OutliningIndicators/InBlock.gif)
SquarePeg sp = new SquarePeg();
SystemDecorator sd = new SystemDecorator(sp);
sd.insert();
Console.WriteLine();
}
}
}