Example for the Chain of Responsibility pattern
This code uses chain of responsibility to get the owner for a TaskItem
.
Then it builds up a list of project details.
import java.util.ArrayList; import java.util.Iterator; import java.util.List; interface Language { public String getFirstName(); } class LanguageImpl implements Language { private String firstName; public LanguageImpl() { }//from w ww. ja v a2 s . co m public LanguageImpl(String newFirstName) { firstName = newFirstName; } public String getFirstName() { return firstName; } public void setFirstName(String newFirstName) { firstName = newFirstName; } public String toString() { return firstName; } } class Project implements TaskItem { private String name; private Language lang; private String details; private List projectItems = new ArrayList(); public Project() { } public Project(String n, Language l) { name = n; lang = l; } public String getName() { return name; } public String getDetails() { return details; } public Language getLanguage() { return lang; } public TaskItem getParent() { return null; } public List getTaskItems() { return projectItems; } public void setName(String newName) { name = newName; } public void setOwner(Language newOwner) { lang = newOwner; } public void setDetails(String newDetails) { details = newDetails; } public void addTaskItem(TaskItem element) { if (!projectItems.contains(element)) { projectItems.add(element); } } public void removeTaskItem(TaskItem element) { projectItems.remove(element); } public String toString() { return name; } } interface TaskItem { public TaskItem getParent(); public Language getLanguage(); public List getTaskItems(); } class Task implements TaskItem { private String name; private List projectItems = new ArrayList(); private Language owner; private TaskItem parent; private boolean primaryTask; public Task(TaskItem newParent) { this(newParent, "", null, false); } public Task(TaskItem newParent, String newName, Language newOwner, boolean newPrimaryTask) { parent = newParent; name = newName; owner = newOwner; primaryTask = newPrimaryTask; } public Language getLanguage() { if (owner == null) { return parent.getLanguage(); } else { return owner; } } public String getName() { return name; } public List getTaskItems() { return projectItems; } public TaskItem getParent() { return parent; } public boolean isPrimaryTask() { return primaryTask; } public void setName(String newName) { name = newName; } public void setOwner(Language newOwner) { owner = newOwner; } public void setParent(TaskItem newParent) { parent = newParent; } public void setPrimaryTask(boolean newPrimaryTask) { primaryTask = newPrimaryTask; } public void addTaskItem(TaskItem element) { if (!projectItems.contains(element)) { projectItems.add(element); } } public void removeTaskItem(TaskItem element) { projectItems.remove(element); } public String toString() { return name; } } public class Main { public static void main(String[] arguments) { Project project = createData(); getItemInfo(project); } static Project createData() { Language contact1 = new LanguageImpl("CSS"); Language contact2 = new LanguageImpl("HTML"); Language contact3 = new LanguageImpl("Java"); Language contact4 = new LanguageImpl("Javascript"); Project project = new Project("Project A", contact2); Task task1 = new Task(project, "Task 1", contact4, true); Task task2 = new Task(project, "Task 2", null, true); Task task3 = new Task(project, "Task 3", contact3, false); project.addTaskItem(task1); project.addTaskItem(task2); project.addTaskItem(task3); Task task4 = new Task(task1, "Task 4", null, false); Task task5 = new Task(task1, "Task 5", contact1, true); Task task6 = new Task(task2, "Task 6", contact1,true); Task task7 = new Task(task2, "Task 7", null, false); Task task8 = new Task(task2, "Task 8", null, false); Task task9 = new Task(task3, "Task 9", null, true); Task task10 = new Task(task3, "Task 10", contact4, false); task1.addTaskItem(task4); task1.addTaskItem(task5); task2.addTaskItem(task6); task2.addTaskItem(task7); task2.addTaskItem(task8); task3.addTaskItem(task9); task3.addTaskItem(task10); return project; } private static void getItemInfo(TaskItem item) { System.out.println("TaskItem: " + item); System.out.println(" Owner: " + item.getLanguage()); System.out.println(); if (item.getTaskItems() != null) { Iterator subElements = item.getTaskItems().iterator(); while (subElements.hasNext()) { getItemInfo((TaskItem) subElements.next()); } } } }