Decorator pattern : Decorator Pattern « Design Patterns « C# / CSharp Tutorial






using System;
interface Action
{
    string DoIt();
}
class Component : Action
{
    public string DoIt()
    {
        return "Component";
    }
}
class DecoratorA : Action
{
    Action component;

    public DecoratorA(Action c)
    {
        component = c;
    }

    public string DoIt()
    {
        string s = component.DoIt();
        s += " DecoratorA ";
        return s;
    }
}
class DecoratorB : Action
{
    Action component;
    public DecoratorB(Action c)
    {
        component = c;
    }

    public string DoIt()
    {
        string s = component.DoIt();
        s += " DecoratorB ";
        return s;
    }
}
class MainClass
{
    static void Main()
    {
        Action component = new Component();
        Console.WriteLine(new DecoratorA(component).DoIt());
        Console.WriteLine(new DecoratorB(component).DoIt());
        Console.WriteLine(new DecoratorB(new DecoratorA(component)).DoIt());
        DecoratorB b = new DecoratorB(new Component());
        Console.WriteLine(new DecoratorA(b).DoIt());

    }
}








28.12.Decorator Pattern
28.12.1.Decorator pattern