samedi 9 juin 2018

Is this a decorator pattern?

The other day I got asked what is the design pattern below (it's roughly what I remembered):

interface Canvas
{
    void Paint();
}

class SimpleCanvas
 : Canvas
{
    public void Paint()
    {
    }
}

abstract class CanvasImprovement : Canvas
{
    protected Canvas canvas;

    public CanvasImprovement(Canvas canvas)
    {
        this.canvas = canvas;
    }

    public abstract void Paint();
}

class ScrollBarImprovement : CanvasImprovement
{
    public ScrollBarImprovement
     : base(Canvas canvas)
    {
    }

    public override void Paint()
    {
        PaintScrollBars();
        canvas.Paint();
    }

    private void PaintScrollBars()
    {
    }
}

class CanvasTesting
{
    public static void Main()
    {
        var sbe = new ScrollBarImprovement (new SimpleCanvas());
        sbe.Paint();
    }
}

I passed the test, back then I wrote it was a Decorator, but wondering whether there was a trick / trap. I mean it looks pretty much like this one: http://www.dofactory.com/net/decorator-design-pattern

The code decorates the existing behaviour in the canvas, just want to double confirm or if there is something else on which I should pay more attention.

Aucun commentaire:

Enregistrer un commentaire