Field Notes: Using the Strategy Pattern

I had been using the e-tail example in my pattern classes when someone asked, “Are you aware that in the UK people over a certain age don't get taxed on food?” I wasn't aware of this and the interface for the CalcTax object did not handle this case. I could handle this in at least one of three ways:

  1. Pass the age of the Customer into the CalcTax object and use it if needed.

  2. Be more general by passing in the Customer object itself and querying it if needed.

  3. Be more general still by passing a reference to the SalesOrder object (that is, this) and letting the CalcTax object query it.

While it is true I have to modify the SalesOrder and CalcTax classes to handle this case, it is clear how to do this. I am not ...

Get Design Patterns Explained: A New Perspective on Object-Oriented Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.