using System;

namespace Decorator.DecoratorFolder


{

/**//// <summary>
/// 装饰模式:在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
///处理那些可以撤消的职责。
///当不能采用生成子类的方法进行扩充时。
/// 一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。
/// 另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类
/// </summary>
public interface Work

{
void insert();
}

/**//// <summary>
/// 装饰模式:动态给一个对象添加一些额外的职责,就象在墙上刷油漆.
/// 使用Decorator模式相比用生成子类方式达到功能的扩充显得更为灵活
/// 目的:
/// 我们通常可以使用继承来实现功能的拓展,如果这些需要拓展的功能的种类很繁多,
/// 那么势必生成很多子类,增加系统的复杂性,同时,使用继承实现功能拓展,我们必须可预见这些拓展功能,
///这些功能是编译时就确定了,是静态的.
///使用Decorator的理由是:这些功能需要由用户动态决定加入的方式和时机.
/// Decorator提供了"即插即用"的方法,在运行期间决定何时增加何种功能
/// </summary>
public class SystemDecorator:Work

{


私有变量#region 私有变量
private Work work;
private ArrayList other = new ArrayList();
#endregion

public SystemDecorator(Work work)

{
this.work = work;
other.Add("挖坑");
other.Add("钉木板");
}



Work 成员#region Work 成员

public void insert()

{
this.newMethod();
}

#endregion


Decorator 成员#region Decorator 成员



private void newMethod()

{
this.otherMethod();
work.insert();
Console.WriteLine();
}

private void otherMethod()

{
for(int index =0;index <other.Count; index ++)

{
Console.WriteLine(other[index].ToString() + "正在进行!");
}
}

#endregion
}

/**//// <summary>
/// SquarePeg 的摘要说明。
/// </summary>
public class SquarePeg :Work

{
public SquarePeg()

{
//
// TODO: 在此处添加构造函数逻辑
//
}

Work 成员#region Work 成员

public void insert()

{
System.Console.WriteLine("方形桩插入");
}

#endregion
}

/**//// <summary>
///以下为测试代码
/// 子类化和Decorator模式的主要区别是:
/// 采用子类化,你同一个类打交道;
/// 使用Decorator模式,你可以动态的修改多个对象。
/// </summary>
class DecoratorTest

{

/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)

{
//
// TODO: 在此处添加代码以启动应用程序
//

SquarePeg sp = new SquarePeg();
SystemDecorator sd = new SystemDecorator(sp);
sd.insert();
Console.WriteLine();
}
}
}