lundi 4 février 2019

Replacing decorator pattern to enforce creation order

Trying to learn some new design patterns I came across the following problem.
Let's say we have the following interface : GUI_ITEM.
We have some classes that implement it like : Canvas , TextView , ListView ..
The target is to add some extra behavior to those classes such as :
Scrollbars , Title , Border ..

Now since we need to add a behavior to given objects from the same interface , i'm guessing the right design to be used is a decorator .
This is the UML
UML

(this is from a slide-show i'm learning from)

So usage would be something like this :

GUI_Item gui = new Scrollbars(new Border(new Title(new Canvas() ) ) );

The following question arises ;
What if I want to enforce a creation order ?
Since the border can hide the scroll bar , or we added another feature which is created at the same spot of another feature , thus can hide it .

I'm trying to figure out which design pattern will be the best solution here and how to implement it since I want to remove this responsibility from the client.

I can't seem to think of a proper design pattern here ? maybe I should combine the decorator with another solution ?
I thought about adding a member to each decorator-class marking its hierarchy(such that objects with the same hierarchy cannot be created together) in the displaying order but this seems like a bad solution (Because I feel like its violating the OPEN/CLOSED principle)

Thanks !

Aucun commentaire:

Enregistrer un commentaire