I had previously implemented Abstract Factory Pattern on simple problems and it worked. So I tried to use same thing to solve this problem but I was confused. I wrote the bottom level classes but confused how to combine them to a single program. What Should I do and How should it be done?
I am using Java to write a code to calculate tax. I am having base class TaxPayer
. Taxpayer can have multiple incomeSource
. There can be many types of TaxPayer
or IncomeSource
. There can be many income headings for different income sources as its attributes, which will be stored in database. The tax rate will be different for different taxpayer type and amount of taxableIncome
.
Base class Taxpayer is defined as
public abstract class TaxPayer {
private List<IncomeSource> incomeSource;
double taxRate;
Address address;
other attributes here;
public Double getTaxRate(){
return 0.25; //default tax rate
}
}
public abstract class IncomeSource {
private String incomeSourceName;
private Double incomeHeading1, incomeHeading2, incomeHeading3;
private Double totalIncome = incomeHeading1 + incomeHeading2 + incomeHeading3;
}
There can be more levels of IncomeSource
inheritance with different income headings. Similarly tax payer type can be modeled into following inheritance structure
Base Class: Taxpayer
* IndividualPerson
* Male, Female, OldAge
* Business
* Bank, ITIndustry, HydroElectricIndustry
* TaxFree
* SocialOrganization, ReligiousOrganization, PoliticalParty etc.
The subclasses of TaxPayer
generally modifies the taxRate
to be applied to taxableIncome
and sometimes changes taxableIncome
with some logic. For an example:
abstract class IndividualPerson extends TaxPayer{
if (incomeSource.taxableIncome > 250000) taxRate = ratex;
if (incomeSource.taxableIncome > 500000) taxRate = ratey;
@override
public getTaxRate() {
return taxRate;
}
}
class Female extends IndividualPerson {
if (incomeSource.getNumberOfIncomeSource() > 1) taxRate = taxRate + rate1;
else taxRate = taxRate - rate2
if (address.isRural() = true) taxRate = taxRate - rate3;
if (attributeX = true) taxRate = taxRate + rate4;
if ("Some other attribute" = true) taxableIncome = taxableIncome - someAmount;
}
We have to check other attributes of Taxpayer
and IncomeSource
to determine the taxRate
. Mostly, taxRate
is different for different logic, but sometimes, taxableIncome
can be discounted.
I am trying return tax rate according to TaxPayer type and taxableIncome. I am confused how to combine bottom level classes together.
Create Taxpayer
as a parent interface
and the three below in the hierarchy will implement it. This taxpayer
interface will have a getTaxRate()
method which needs to be implemented by all of the child classes.
You can make the business
class as another interface extending the parent taxpayer
interface and make the bank,hydroelectricity
class extend the business
interface.
each of the bank,hydroelectricity
etc will have a final float
with desired tax rate.
Suppose A is a person class who has business in Bank so in that case
A implements Bank
This will provide the taxrate specific to bank at A.
But better option would be to have the bank,hydroelectricity
etc as ENUMS
under business
class that shall implement the Taxpayer
interface.
Better approach
public enum Business {
BANK(10.1), ITINDUSTRY(8.1), HYDROELECTRICITY(1.3);
private float value;
private Business(int value) {
this.value = value;
public float getTaxRate(){
return this.value;
}
};
class A implements TaxPayer{
public String occupation = "BANK";
//implemented from parent taxpayer
public float getTaxRate(){
return Business.BANK.getTaxRate();
}
}
And if the segregation under taxpayer is not important then you can club all the lowest level classes under a single ENUM.
Do something like above. Hope it gives you a clearer idea.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.