I have a decorator chain that looks like this when initially created:
IType calculator = new TypeADecorator(
new TypeBDecorator(
new TypeCDecorator(
new MyCalculator())));
Each of these decorators does a database look up to get a piece of data which is used in a calculation
However, not all of these decorators will be used each time. Therefore, there will potentially be redundant database calls that return nothing
So I am thinking it would better to dynamically create the decorator chain based on which ones do get used
e.g.
If I had a bool representing each one that gets used:
bool useTypeA;
bool useTypeB;
bool useTypeC;
Would I be able to somehow dynamically construct the necessary chain?
Bear in mind that it’s highly likely that other decorators will be added throughout the life of the app so I would like to come up with something that is quite flexable. Also, although there are only three decorators here, in reality there are about 8 used at the moment
I assume the database lookups are done in the constructor of the decarators, would not they be deferred? Would it be possible to design your decorators in a way that they do the database lookup whenever they really need the value from the db?