Design Patterns – Applying Different Taxes Based on Various Factors

designdesign-patternsobject-oriented-designuml

I've been given the task to design a system that has a product's price have different amounts of different taxes based on different factors.

For example in the US you would get a few – one based on region and another on state and such, while in the EU you get VAT. I'm trying to figure out a non-hardcoded way of different taxes being applied based on different factors for the customer and the product and the location or store.

Does anyone have an idea as to an exiting solution that would easily apply to this problem?

I also would accept general answers on how to learn to solve such design questions in general (like a book that guides you through creating a structured approach)

Best Answer

Craig Larman's Applying UML and Patterns tackles the problem with an external tax service (real services do exist if you Google them) and a Gang-Of-Four object Adapter that converts the results into the format expected by the system.

I think Strategy might be OK, but it's ambitious to think that you're going to be able to maintain the code for all the tax algorithms that exist. At every election there's a chance your code will have to change and maybe even have to be validated with local government authorities (?).

As you noted, taxes are complex. You could have multiple lines in a sale (in Quebec there is TVQ and TPS, and at one time you were taxed on the tax!). In some states in the US there is no tax on clothing, or food, etc. To abstract these aspects, Larman's conceptual design used (see Section 26.3) a TaxLineItem in the domain model. The external service takes care of creating them (the logic is in the external service).

Here's a rough UML diagram of that idea:

enter image description here

For the Adapter, here's how it looks in 26.1:

enter image description here

The use of an adapter:

enter image description here

Related Topic