using
System;
using System.Collections.Generic;
using System.Text;
namespace SharpDevelop设计模式.类库
{
/// <summary>
/// Decorator模式在运行时为对象添加功能,它从一个接口继承而来,并扩展
/// 和实现了该接口的全部实现和方法,它接收在构造函数中实现该接口 一个对象
/// 并将原始接口提供的所有调用委托给通过该构造函数接收的对象
/// decorator可以添加许多原始接口不包含的函数,从而就可以快速地添加功能
///
/// 如下代码是以Factory模式下来示例Decorator模式
/// </summary>
public interface IHelloPrinterDecorator :IHelloPrinter
{
// 扩展功能
void PrintGoodbye();
}
public abstract class AbstractHelloPrinterDecorator : IHelloPrinterDecorator
{
IHelloPrinter helloPrinter;
public AbstractHelloPrinterDecorator(IHelloPrinter helloPrinter)
{
this .helloPrinter = helloPrinter;
}
public void PrintHello()
{
helloPrinter.PrintHello();
}
public abstract void PrintGoodbye();
}
public class EnlishHelloPrinterDecorator : AbstractHelloPrinterDecorator
{
public EnlishHelloPrinterDecorator(IHelloPrinter helloPrinter)
: base (helloPrinter)
{
}
public override void PrintGoodbye()
{
System.Console.WriteLine( " Good bye " );
}
}
public class ChineseHelloPrinterDecorator : AbstractHelloPrinterDecorator
{
public ChineseHelloPrinterDecorator(IHelloPrinter helloPrinter)
: base (helloPrinter)
{
}
public override void PrintGoodbye()
{
System.Console.WriteLine( " 再见 " );
}
}
// 这里也使Decorator从抽象基类派生,但Decorator代码比HelloPrinter类更稳定
}
using System.Collections.Generic;
using System.Text;
namespace SharpDevelop设计模式.类库
{
/// <summary>
/// Decorator模式在运行时为对象添加功能,它从一个接口继承而来,并扩展
/// 和实现了该接口的全部实现和方法,它接收在构造函数中实现该接口 一个对象
/// 并将原始接口提供的所有调用委托给通过该构造函数接收的对象
/// decorator可以添加许多原始接口不包含的函数,从而就可以快速地添加功能
///
/// 如下代码是以Factory模式下来示例Decorator模式
/// </summary>
public interface IHelloPrinterDecorator :IHelloPrinter
{
// 扩展功能
void PrintGoodbye();
}
public abstract class AbstractHelloPrinterDecorator : IHelloPrinterDecorator
{
IHelloPrinter helloPrinter;
public AbstractHelloPrinterDecorator(IHelloPrinter helloPrinter)
{
this .helloPrinter = helloPrinter;
}
public void PrintHello()
{
helloPrinter.PrintHello();
}
public abstract void PrintGoodbye();
}
public class EnlishHelloPrinterDecorator : AbstractHelloPrinterDecorator
{
public EnlishHelloPrinterDecorator(IHelloPrinter helloPrinter)
: base (helloPrinter)
{
}
public override void PrintGoodbye()
{
System.Console.WriteLine( " Good bye " );
}
}
public class ChineseHelloPrinterDecorator : AbstractHelloPrinterDecorator
{
public ChineseHelloPrinterDecorator(IHelloPrinter helloPrinter)
: base (helloPrinter)
{
}
public override void PrintGoodbye()
{
System.Console.WriteLine( " 再见 " );
}
}
// 这里也使Decorator从抽象基类派生,但Decorator代码比HelloPrinter类更稳定
}