Search code examples
ooad

OO Design question


I have a two types of products - Discounted (10% Disc) and NonDiscounted (0%) Each of these can be either LocalProduct / ExportableProduct with export one attracting a 15% sales tax.

What is the best way to model this scenario. Me being absolute newbie to S/W design, I have very limited ideas 1. To have 4 different Product sub types 2. Use Strategy pattern and have 4 different strategies.

Can some one please suggest how can I model this effectively using the above options or other ones.


Solution

  • Classes distinguish sets of behavior. So let's take a look at your divisions in those terms:

    • While an argument could be made that discounted/non-discounted is a variation in behavior, it's trivial to reduce this to a single behavior: All products have a discount, but the amount of the discount happens to be 0% on non-discounted products. This is just an attribute of your products (discount_amount), not a separate class.

    • Local/exportable may or may not have distinct behaviors. If the only difference is whether the product is allowed to be shipped internationally or not, then a simple boolean flag should handle this distinction more than adequately. If, on the other hand, exportable products require behaviors not supported by local products (e.g., recording of customs requirements and procedures), then it would be appropriate to either make ExportableProduct a subclass of LocalProduct (if exportable product behaviors are a superset of local product behaviors) or make an abstract Product class with LocalProduct and ExportableProduct subclasses (if local products also have behaviors which are not supported by exportable products).