Question

For each product there are associated cost calculators like: discount, discount by merchant, bonus by merchant, monthly discount etc. In future, more cost calculators would be added.

We have a concrete product class and many decorators for each cost calculation. All products should use all of the calculators, because the calculators decide to apply their calculations by the product's properties like product merchant id, category id, color etc.

And, there are millions of products in our system which needs to be calculated. So, we better cache the decorated calculators. Because, decorating each product entity in runtime would be expensive. But this is hard with decorator pattern. It seems like a smell to use this pattern in our situation.

What do you suggest? Should we use decorators, strategy or chain-of-responsibility pattern? Or no-pattern.

Was it helpful?

Solution

All products should use all of the calculators, because the calculators decide to apply their calculations by the product's properties like product merchant id, category id, color etc.

If you need to have all products use all decorators, then you're not really gaining any benefit from the pattern. Does this mean that if you implement a new decorator, all existing entities must be updated to use that new decorator?

Decorators should be applied to products only when necessary, and only those decorators that are needed should be applied.

I think you should remove the decision from within the decorators; something else should decide whether to apply a decorator, in which case it wraps the product in the appropriate decorators. This way you know that if a product is wrapped in a decorator, that decorator is affecting (effecting?) the product.

OTHER TIPS

In a strategy pattern, the intent of the object changes.

Therefore, I think strategy would be a good choice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top