Design Patterns - Composite Pattern
Key words: Tree like hierarchy
UML
Composite Pattern
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Fourth
<ul>
<li>Fourth1</li>
<li>Fourth2</li>
</ul>
</ul>
</ul>
Pseudo Code
interface TodoList {
String getHtml();
}
class Todo extends TodoList {
String text;
public Todo(String text) {
this.text = text;
}
public String getHtml() {
return this.text;
}
}
class Project extends TodoList {
String title;
List<TodoList> todos;
public Project(String title, List<TodoList> todos) {
this.title = title;
this.todos = todos;
}
public String getHtml() {
String html = "<h1>";
html += this.title;
html += "</h1><ul>";
for(TodoList todo : todos) {
html += "<li>";
html += todo.getHtml();
html += "</li>";
}
html += "</ul>";
return html;
}
}