Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat design pattern can I use for creating objects based on country/state/location for a shopping cart application?
    text
    copied!<p>I am currently working on an application that will help me improve my OO design skills. I have almost completed the assignment but feel that I need to make my application more extensible and need some inputs on how to achieve this. The problem is a little difficult to explain through text so please bare with me. </p> <p>The problem domain can be described in a nutshell as follows : - </p> <p>The problem domain is to design a shopping cart application. There are different categories for an item such as food, books, etc. The rate of tax for which an item is eligible depends upon the category of the item. There are well defined tax policies, e.g basic tax of 10 % is applicable for all items except items belonging to the category food, all imported items are subject to additional import duty tax of 5 %. </p> <p>The area I want to focus on is the module responsible for deciding the eligibility of an item for a given tax policy. I have used ideas from other people and the internet to come up with the following design for the tax module : </p> <p><img src="https://i.stack.imgur.com/R07kC.png" alt="Design for shopping cart tax module "></p> <p><strong>TaxCriteria :</strong> defines the condition that must be satisfied for an item to eligible for a TaxPolicy. Concrete implementers implement the isEligible method and provide the business logic for determining whether an item is eligible for a policy or not eg. isEligible method in ImportedTaxCriteria will check if an item is imported or not. </p> <p><strong>TaxPolicy :</strong> has a TaxCriteria, the rate at which the tax is applicable, and the name for the tax policy. The relation between TaxPolicy and TaxCriteria is based on the specification pattern. </p> <p><strong>SalesPolicy :</strong> is made up of one or more TaxPolicy instances. </p> <p><strong>SalesEngine :</strong> SalesEngine has a SalesPolicy and a TaxCalculator. The applyPolicies method is passed an Order that contains a list of items. applyPolicies will iterate through each item in the order and check its eligibility against each TaxPolicy in the sales policy and calculate the tax using a TaxCalculator based on the applicable policies. </p> <p>The new problem domain : </p> <p>Every country has different sales policies defined. Import duty in the US could be 5 percent whereas import duty in China could be 3 %. To address this requirement, I was thinking of using a simple factory pattern. SalesPolicy will no longer be a class but an interface with a getTaxPolicies method that returns a list of TaxPolicy. USASalesPolicy and ChinaSalesPolicy will implement SalesPolicy and the getTaxPolicies method will return a list of TaxPolicy pertaining to the specific country respectively. Similarly, every country will have its own tax calculation adjustments. USATaxCalculator and ChinaTaxCalculator can take care of indepent implementations. </p> <p>Finally, SalesEngine will be passed a SalesPolicyFactory and a TaxCalculatorFactory in the constructor instead of directly passing a SalesPolicy and a TaxCalcualtor. The applyPolicies method will be passed an additional parameter which will be a String representing the country. The applyPolciies method will pass the country to the factories in SalesEngine and use them to create appropriate SalesPolicy and TaxCalculator instances to be used for calculating the tax. </p> <p>This is what my thought process has been so far. Am I doing justice to the well defined design principles an the factory pattern? How can I use abstract factory pattern such that SalesPolicy and TaxCalculator represent the families of related products? Is there a better way to solve the problem than the one I have used? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload