## 6: Design Patterns

This homework covers design patterns; only the patterns discussed in lecture are used in these questions.
1. (15 points) For each of the following code snippets, identify the design pattern best represented by that code. Briefly explain your reasoning.
1.
``````public class Employee {
...
}
public class Manager extends Employee {
private List<Employee> directSubordinates;
public Manager(List<Employee> directSubordinates) {
this.directSubordinates = directSubordinates;
}
public List<Employee> getDirectSubordinates() {
return this.directSubordinates;
}
}
``````
2.
``````public class TreeWalker<T> {
private List<TreeNode<T>> remainingNodes;
public TreeWalker(TreeNode<T> root) {
}
public boolean finishedWalking() {
return this.remainingNodes.isEmpty();
}
public T getNextNodeData() {
TreeNode<T> node = this.remainingNodes.remove(0);
if (node.getLeft() != null) {
}
if (node.getRight() != null) {
}
return node.getData();
}
}
``````
3.
``````public interface Combiner<T> {
public T combine(T a, T b);
}
public class Adder<Integer> implements Combiner<Integer> {
public Integer combine(Integer a, Integer b)
{ ... }
}
...
public T accumulate(List<T> items, T baseValue, Combiner<T> combiner) {
T accumulator = baseValue;
for (T t : items) {
accumulator = combiner.combine(accumulator, t);
}
return accumulator;
}
``````
2. (15 points) Examine the following members of the Java API. For each one, identify the design pattern that the member represents and briefly explain why.

1. The `AbstractList.get(int)` method.

2. The `PushbackReader` class.

3. Objects returned by the `Collections.unmodifiableSet` method.

3. (15 points) Variations on the standard design patterns which can be made by overlaying two different patterns (think overlaying their UML diagrams at a high level). This sometimes leads to useful new applications. For the following pairs of design patterns there is an elegant overlay possible, draw a UML diagram of that overlay and briefly describe it. What you don't want to do is to use the two patterns "next to" each other, you want to use them "on top of" each other, combining features of both.
1. Deposite: Decorator and Composite;