Visitor (padrão de desenho)/Exercício 1: Cálculo de Impostos

From Wiki**3

< Visitor (padrão de desenho)
Revision as of 18:55, 29 November 2009 by Root (talk | contribs) (New page: {{TOCright}} = Problem = = Solution = == Taxpayer Classes == === Class Taxpayer === <java5> * * Basic taxpayer.: public abstract class Taxpayer { /** * No actual value is retu...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Problem

Solution

Taxpayer Classes

Class Taxpayer

<java5> /**

* Basic taxpayer.
*/

public abstract class Taxpayer { /** * No actual value is returned in this case. * * @param irs * the visitor used to compute the revenue. * @return tax payed by this taxpayer. */ public double accept(FriendlyIRS irs) { throw new UnsupportedOperationException(); } } </java5>

Class Person

<java5> /**

* Individual taxpayer.
* 
* We omitted the initialisation code.
*/

public class Person extends Taxpayer { /** * @see Taxpayer#accept(FriendlyIRS) */ @Override public double accept(FriendlyIRS irs) { return irs.taxPerson(this); } } </java5>

Class Company

<java5> import java.util.ArrayList;

/**

* A company has employees (persons).
* 
* We omitted the initialisation code.
*/

public class Company extends Taxpayer { /** * The employees in this company. */ private ArrayList<Person> _employees = new ArrayList<Person>();

/** * @return size of company (number of employees). */ public int size() { return _employees.size(); }

/** * @param index * @return an employee */ public Person getEmployee(int index) { return _employees.get(index); }

/** * @see Taxpayer#accept(FriendlyIRS) */ @Override public double accept(FriendlyIRS irs) { return irs.taxCompany(this); } } </java5>

Class Region

<java5> import java.util.ArrayList;

/**

* A region has companies.
* 
* We omitted the initialisation code.
*/

public class Region extends Taxpayer { /** * The companies in this region. */ private ArrayList<Company> _companies = new ArrayList<Company>();

/** * @return size of region (number of companies). */ public int size() { return _companies.size(); }

/** * @param index * @return a company */ public Company getCompany(int index) { return _companies.get(index); }

/** * @see Taxpayer#accept(FriendlyIRS) */ @Override public double accept(FriendlyIRS irs) { return irs.taxRegion(this); } } </java5>

Tax Computation (Visitors)

Class FriendlyIRS

The abstract class representing the concept of tax computation.

<java5> /**

* The IRS computing visitor interface.
*/

public abstract class FriendlyIRS { /** * @param person * @return tax payed by this person. */ public abstract double taxPerson(Person person);

/** * @param company * @return tax payed by this company. */ public abstract double taxCompany(Company company);

/** * @param region * @return tax payed by this region. */ public abstract double taxRegion(Region region); } </java5>

Class VanillaTaxes

Simple tax computation.

<java5> /**

* Usual tax calculator.
*/

public class VanillaTaxes extends FriendlyIRS {

/** * @see FriendlyIRS#taxCompany(Company) */ @Override public double taxCompany(Company company) { double tax = 0; for (int index = 0; index < company.size(); index++) tax += company.getEmployee(index).accept(this); return tax; }

/** * @see FriendlyIRS#taxPerson(Person) */ @Override public double taxPerson(Person person) { return 1; }

/** * @see FriendlyIRS#taxRegion(Region) */ @Override public double taxRegion(Region region) { double tax = 0; for (int index = 0; index < region.size(); index++) tax += region.getCompany(index).accept(this); return tax; }

} </java5>

Class BecauseWeCare

Discounted taxes.

<java5> /**

* The discount tax system.
*/

public class BecauseWeCare extends FriendlyIRS {

/** * Low water marker for revenue. */ private final int LOW = 1;

/** * Low water marker for population. */ private final int POP = 1;

/** * @see FriendlyIRS#taxCompany(Company) */ @Override public double taxCompany(Company company) { double tax = 0; for (int index = 0; index < company.size(); index++) tax += company.getEmployee(index).accept(this); if (company.size() < POP || tax < LOW) tax *= .9 * tax; return tax; }

/** * In this case, we chose not to test the LOW level (simply because we know * the value is the same). * * @see FriendlyIRS#taxPerson(Person) */ @Override public double taxPerson(Person person) { return 1; }

/** * @see FriendlyIRS#taxRegion(Region) */ @Override public double taxRegion(Region region) { double tax = 0; for (int index = 0; index < region.size(); index++) tax += region.getCompany(index).accept(this); if (region.size() < POP || tax < LOW) tax *= .9 * tax; return tax; }

} </java5>

Example Application

<java5> </java5>

Compiling and Running