Java Reference
In-Depth Information
public double getSalary() {
return salary;
}
}
public class Manager extends Employee {
public Manager(String name, double salary) {
super(name, salary);
}
}
public class Programmer extends Employee {
public Programmer(String name, double salary) {
super(name, salary);
}
@Override
public void addEmployee(Employee employee) {
// Consider throwing an exception here
}
@Override
public void removeEmployee(Employee employee) {
}
@Override
public Employee getEmployee(int i) {
return null;
}
@Override
public int getNrEmployees() {
return 0;
}
}
Much better! Now every concept has its own class. Note how this immediately gives you a way to
implement the composite pattern. You now have an easy way to treat a group of employees the same
way as a single employee. The composite pattern makes it very easy to model a “has a” relation,
which is particularly helpful when you want to model a tree structure, such as in this example.
To see why this is so helpful, look at the next simple example program, which shows the organiza-
tional structure starting from a particular employee:
public class HRExample {
public static void main(String args[]) {
Employee programmerAimee = new Programmer("Aimee", 16000);
Employee programmerBart = new Programmer("Bart", 15000);
Employee programmerSeppe = new Programmer("Seppe", 14000);
Employee managerJane = new Manager("Jane", 30000);
Employee managerWiley = new Manager("Wiley", 35000);
managerWiley.addEmployee(managerJane);
managerWiley.addEmployee(programmerBart);
Search WWH ::




Custom Search