Design Pattern Basic Tutorial

adplus-dvertising
Interpreter Pattern
Previous Home Next

The Interpreter is used to move the object by the help of the list of the collection or aggregate object. this feature hide the internal representation of the moving object.

Benefit and use of Interpreter Pattern
  1. Used the distinguish variations in the traversal of aggregation object.
  2. For representation of the object Use a standard interface.
  3. It is used list, sort and map in build each standard collection.
  4. It is similar to Enumeration class, but more effective.

Example

Teacher is an interface, principle, Voiceprinciple , Worker and broker are implementation classes of interface teacher. Teachertest class is the main class which will create a list of ArrayList and traverse the element of this list.

package r4r;
import java.util.*;
interface Teacher {   
public abstract double earn();
}
class Principle implements Teacher {
private double weeklySalary;
private String name;
public Principle(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 "Principle: " + getName();
}

@Override
public double earn() {
// TODO Auto-generated method stub
return 0;
}
}

class Voiceprinciple implements Teacher{
private double wagePerPiece;
private int quantity;
private String name;
public Voiceprinciple(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 "Voice Principle: " + getName();
}

@Override
public double earn() {
// TODO Auto-generated method stub
return 0;
}
}

class Worker implements Teacher {
private double hourlyWage;
private double hours;
private String name;
public Worker(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 "Worker: " + getName();
}

@Override
public double earn() {
// TODO Auto-generated method stub
return 0;
}
}

class Broker implements Teacher {
private double salary;
private double commission;
private double totalSales;
private String name;
public Broker(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 "Broker name:"+ getName();
}
@Override
public double earn() {
// TODO Auto-generated method stub
return 0;
}
}
public class interpretertest {
public static void main(String[] args) {
java.util.List list = new ArrayList();
list.add(new Principle("Bill",800.00));
list.add(new Broker("Newt", 400.0, 3.75, 159.99));
list.add(new Voiceprinciple("Al", 2.5, 200));
list.add(new Worker("Babara", 13.75, 40));
list.add(new Principle("Peter", 1200.00));
list.add(new Broker("Margret", 600.0,5.5, 200.25));
list.add(new Voiceprinciple("Mark", 4.5, 333));
list.add(new Worker("William", 31.25, 50));

System.out.println("Use built-in iterator:");
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
Teacher em = (Teacher)iterator.next();
if (em instanceof Principle) {
System.out.print(em + " earns $");
System.out.println(em.earn());
}
}
}
}
Previous Home Next