I am designing an inventory system. Each inventory item’s taxes need to be calculated based on many conditions (State, County, etc, etc). The rules of calculations change as well over period of time. So I was thinking of the Strategy Pattern but I do not have an extensive experience in making design descions.
What other questions should I be asing my self?
My Inventory Item can be of a different type, so the wiki page on strategy pattern says:
The behaviours of a class should not be inhereted, instead they should be encapsulated using interfaces.
How does this affect my case?
This means that instead of subclassing your
Inventoryitem for each and every possible tax calculation, you should encapsulate the calculation algorithm behind an interface and then use encapsulation inside yourInventoryclass that points to the correct Strategy object.This saves you a lot of work since you don’t have to declare a new
Inventorysubclass each time you have a new calculation.The following implementation uses inheritance:
But the strategy pattern would look like:
So, strategy is definitely a nice solution for abstracting your tax calculations. If the rules of which calculation apply at a certain time are different and can change I would also use a Factory for creating the correct Strategy object.