Definition
Provide a way to move through a list of collection or aggregated objects without knowing its internal representations.Where to use & benefits
- Use a standard interface to represent data objects.
- Use s standard iterator built in each standard collection, like List, Sort, or Map.
- Need to distinguish variations in the traversal of an aggregate.
- Similar to Enumeration class, but more effective.
- Need to filter out some info from an aggregated collection.
Example
Employee is an interface, Manager, PieceWorker, HourlyWorker and CommissionWorker are implementation classes of interface Employee. EmployeeTest class will create a list and use a built-in iterator of ArrayList class to traverse the members of the list.import java.util.*;
interface Employee {
public abstract double earnings();
}
class Manager implements Employee {
private double weeklySalary;
private String name;
public Manager(String name, double s) {
this.name = name;
setWeeklySalary(s);
}
void setWeeklySalary(double s) {
if (s > 0) {
weeklySalary = s;
} else
weeklySalary = 0;
}
public double earnings() {
return weeklySalary;
}
public String getName() {
return name;
}
public String toString() {
return "Manager: " + getName();
}
}
class PieceWorker implements Employee {
private double wagePerPiece;
private int quantity;
private String name;
public PieceWorker(String name, double w, int q) {
this.name = name;
setWagePerPiece(w);
setQuantity(q);
}
void setWagePerPiece(double w) {
if (w > 0)
wagePerPiece = w;
else
wagePerPiece = 0;
}
void setQuantity(int q) {
if ( q > 0)
quantity = q;
else
quantity = 0;
}
public String getName() {
return name;
}
public double earnings() {
return quantity * wagePerPiece;
}
public String toString() {
return "Piece worker: " + getName();
}
}
class HourlyWorker implements Employee {
private double hourlyWage;
private double hours;
private String name;
public HourlyWorker(String name, double w, double h) {
this.name = name;
setHourlyWage(w);
setHours(h);
}
void setHourlyWage(double w) {
if (w > 0)
hourlyWage = w;
else
hourlyWage = 0;
}
void setHours(double h) {
if ( 0 <= h && h < 168)
hours = h;
else
hours = 0;
}
public String getName() {
return name;
}
public double earnings() {
return hourlyWage * hours;
}
public String toString() {
return "Hourly worker: " + getName();
}
}
class CommissionWorker implements Employee {
private double salary;
private double commission;
private double totalSales;
private String name;
public CommissionWorker(String name,
double salary, double commission, double totalSales) {
this.name = name;
setSalary(salary);
setCommission(commission);
setTotalSales(totalSales);
}
void setSalary(double s) {
if( s > 0)
salary = s;
else
salary = 0;
}
void setCommission(double c) {
if ( c > 0)
commission = c;
else
commission = 0;
}
void setTotalSales(double ts) {
if (ts > 0 )
totalSales = ts;
else
totalSales = 0;
}
public String getName() {
return name;
}
public double earnings() {
return salary + commission/100*totalSales;
}
public String toString() {
return "Commission worker:"
+ getName();
}
}
class EmployeeTest {
public static void main(String[] args) {
java.util.List list = new ArrayList();
list.add(new Manager("Bill", 800.00));
list.add(new CommissionWorker("Newt", 400.0, 3.75, 159.99));
list.add(new PieceWorker("Al", 2.5, 200));
list.add(new HourlyWorker("Babara", 13.75, 40));
list.add(new Manager("Peter", 1200.00));
list.add(new CommissionWorker("Margret", 600.0,5.5, 200.25));
list.add(new PieceWorker("Mark", 4.5, 333));
list.add(new HourlyWorker("William", 31.25, 50));
System.out.println("Use built-in iterator:");
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
Employee em = (Employee)iterator.next();
System.out.print(em + " earns $");
System.out.println(em.earnings());
}
}
}
%java EmployeeTest
Use built-in iterator:
Manager: Bill earns $800.0
Commission worker:Newt earns $405.999625
Piece worker: Al earns $500.0
Hourly worker: Babara earns $550.0
Manager: Peter earns $1200.0
Commission worker:Margret earns $611.01375
Piece worker: Mark earns $1498.5
Hourly worker: William earns $1562.5The above example also shows a dynamic binding feature which is popular in Object-Oriented realm. If you want to pick up a specific object from the aggregated list, you may use the following code.
while(iterator.hasNext()) {
Employee em = (Employee)iterator.next();
if (em instanceof Manager) {
System.out.print(em + " earns $");
System.out.println(em.earnings());
}
}The above list can also be replaced by an array and achieve the same result.

No comments:
Post a Comment