decorator 装饰者
decorator运行时为对象添加功能
public interface IHelloPrinter
{
void PrintHello();
}
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 EnglishHelloPrinterDecorator : AbstractHelloPrinterDecorator
{
EnglishHelloPrinterDecorator(IHelloPrinter helloPrinter)
: base(helloPrinter)
{
}
public override void PrintGoodbye()
{
throw new Exception("The method or operation is not implemented.");
}
}
public class GermanHelloPrinterDecorator : AbstractHelloPrinterDecorator
{
GermanHelloPrinterDecorator(IHelloPrinter helloPrinter)
: base(helloPrinter)
{
}
public override void PrintGoodbye()
{
throw new Exception("The method or operation is not implemented.");
}
}