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.");
    }
}

posted on 2005-11-01 11:09  井泉  阅读(200)  评论(0编辑  收藏  举报

导航