Note: Download the source code from https://github.com/sommukhopadhyay/SalesTax
I am yet to study the book Pattern Hatching. However, i tried to accumulate all my knowledge on Design Pattern and solved a problem of Accountancy. i would like to share it with you. Let me, first of all, state the problem.
The Problem :
Basic sales tax is applicable at a rate of 10% on all goods, except books, food, and medical products that are exempt. Import duty is an additional sales tax applicable on all imported goods at a rate of 5%, with no exemptions. Also the Sales Tax should be rounded off to the nearest 0.05.
This being the problem, we need to come out with a solution for this problem.
Now let me tell you how my thought process went into it to think it at an abstract level.
First of all, i thought to make two top level classes. One that will define the Items having different benchmarks for Sales Tax and Import Duty; i.e.
1. Food, Medical and Book items which are not imported and hence exempted from both sales tax and import duty
2. Non Food Book and Medical items which are not imported and hence will have just the Sales Tax
3. Food Book and Medical items which are imported and hence will have only import duty
4. Non food book and medical items which are imported and hence will have both Sales Tax and Import Duty
And the other for the Tax Calculation algorithm for different items.
Then i thought this is a perfect match for Strategy Pattern that i had studied in the GoF book. I thought about that pattern keeping in mind for the future expansion of the Tax Calculation Algorithm. What i mean is that for the present problem, the calculation is simple. And hence it does not need any other Strategy. However, for the future purpose if the Tax Calculation Algorithm is changed to some complicated one, then we can just Subclass the Tax Calculation class and attach that strategy to the Item Class.
Next i thought, won't it be nice to get a Factory Class through which the client can create different Items on the fly. Hence i have decided to create an ItemCreator class which is nothing but a parameterized factory class for creating different Items on the fly.
And i came out with the following solution.
The Item class Hierarchy:
File: Item.h
//This class represents the Items which have only Import Duty
//This class represents the Items which have only Sales Tax but no Import Duty
//This class represents the Items which have got both Import Duty as well as sales Tax
As you can see the four classes solve the hierarchy for different items having different benchmark for Sales Tax and import Duty.
The Item.cpp file looks like the following:
File: Item.cpp
Now let us concentrate on the Sales Tax class
file: SalesTax.h
And the implementation of the Sales tax is as follow:
file: SalesTax.cpp
As you can see the the data for calculation are being passed from the Item class.
Hence we can say this is in abstract form an implementation of the Strategy Pattern where the Item class is working as the Context and the Sales Tax class is acting as the sole Strategy interface.
Now lets concentrate on the creation of the Items.
As i have already mentioned, this is done through a Parameterized factory class called ItemCreator.
This class looks like the following:
File: ItemCreator.h
And the implementation of this ItemCreator is as follow:
file: ItemCreator.cpp
And the client program will look like the following:

Thus the problem is solved using two common design pattern concepts  Strategy Pattern and Parameterized Factory Pattern.
This is the way i am trying to move from the problem domain to the solution domain using design pattern concepts.
Hope this helps others who are studying Design Pattern.
I am yet to study the book Pattern Hatching. However, i tried to accumulate all my knowledge on Design Pattern and solved a problem of Accountancy. i would like to share it with you. Let me, first of all, state the problem.
The Problem :
Basic sales tax is applicable at a rate of 10% on all goods, except books, food, and medical products that are exempt. Import duty is an additional sales tax applicable on all imported goods at a rate of 5%, with no exemptions. Also the Sales Tax should be rounded off to the nearest 0.05.
This being the problem, we need to come out with a solution for this problem.
Now let me tell you how my thought process went into it to think it at an abstract level.
First of all, i thought to make two top level classes. One that will define the Items having different benchmarks for Sales Tax and Import Duty; i.e.
1. Food, Medical and Book items which are not imported and hence exempted from both sales tax and import duty
2. Non Food Book and Medical items which are not imported and hence will have just the Sales Tax
3. Food Book and Medical items which are imported and hence will have only import duty
4. Non food book and medical items which are imported and hence will have both Sales Tax and Import Duty
And the other for the Tax Calculation algorithm for different items.
Then i thought this is a perfect match for Strategy Pattern that i had studied in the GoF book. I thought about that pattern keeping in mind for the future expansion of the Tax Calculation Algorithm. What i mean is that for the present problem, the calculation is simple. And hence it does not need any other Strategy. However, for the future purpose if the Tax Calculation Algorithm is changed to some complicated one, then we can just Subclass the Tax Calculation class and attach that strategy to the Item Class.
Next i thought, won't it be nice to get a Factory Class through which the client can create different Items on the fly. Hence i have decided to create an ItemCreator class which is nothing but a parameterized factory class for creating different Items on the fly.
And i came out with the following solution.
The Item class Hierarchy:
File: Item.h
//This class represents the Items which have only Import Duty
//This class represents the Items which have only Sales Tax but no Import Duty
//This class represents the Items which have got both Import Duty as well as sales Tax
As you can see the four classes solve the hierarchy for different items having different benchmark for Sales Tax and import Duty.
The Item.cpp file looks like the following:
File: Item.cpp
Now let us concentrate on the Sales Tax class
file: SalesTax.h
And the implementation of the Sales tax is as follow:
file: SalesTax.cpp
As you can see the the data for calculation are being passed from the Item class.
Hence we can say this is in abstract form an implementation of the Strategy Pattern where the Item class is working as the Context and the Sales Tax class is acting as the sole Strategy interface.
Now lets concentrate on the creation of the Items.
As i have already mentioned, this is done through a Parameterized factory class called ItemCreator.
This class looks like the following:
File: ItemCreator.h
And the implementation of this ItemCreator is as follow:
file: ItemCreator.cpp
And the client program will look like the following:
Fig : The Class Diagram
This is the way i am trying to move from the problem domain to the solution domain using design pattern concepts.
Hope this helps others who are studying Design Pattern.