标签:static ima ram highlight 需要 ace 技术 方法 something
7种结构型模式:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
装饰器模式:动态的给一个对象添加额外的功能,被装饰对象和装饰对象必须实现同一个接口,装饰对象持有被装饰对象的实例。

//被装饰类和装饰类都要实现的接口
public interface ISource {
void DoSomething();
}
//被装饰类
public class Source : ISource
{
public void DoSomething()
{
Console.WriteLine("原始方法");
}
}
//装饰类
public class Decorator : ISource
{
ISource source;
//通过构造函数注入需要装饰的对象,使装饰对象持有被砖石对象
public Decorator(ISource source) {
this.source = source;
}
public void DoSomething()
{
//在这里动态的增加新功能
Console.WriteLine("新增的功能");
source.DoSomething();
Console.WriteLine("也是新增的功能");
}
}
class Program
{
static void Main(string[] args)
{
ISource source = new Source();
ISource decorator = new Decorator(source);
decorator.DoSomething();
Console.Read();
}
}
标签:static ima ram highlight 需要 ace 技术 方法 something
原文地址:http://www.cnblogs.com/mojo/p/7305065.html