组合模式通常用来描述一个类似树状的结构。
一个“根”节点
几个“枝”节点
几个“叶”节点
抽象组件层(视为任何一种类型节点的抽象)
public abstract class Component {
private String name = "";
private int position = 0;
private String content = "";
private Component parent = null;
public Component(String name, int position, String content) {
this.name = name;
this.position = position;
this.content = content;
}
public void print() {
System.out.println("------------------");
System.out.println("Name: " + this.name + "\nPosition: " + this.position + "\nContent: " + this.content);
}
public Component getParent() {
return this.parent;
}
protected void setParent(Component parent) {
this.parent = parent;
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class CompositeRoot extends Component {
private String name = "";
private int position = 0;
private String content = "";
private List subordinaryList = new ArrayList();
private Component root = null;
public CompositeRoot(String name, int position, String content) {
super(name, position, content);
this.name = name;
this.position = position;
this.content = content;
}
protected void setRoot(Component root) {
this.root = root;
}
public Component getRoot() {
return this.root;
}
public List getSubordinary() {
return this.subordinaryList;
}
public void add(Component component) {
component.setParent(this);
subordinaryList.add(component);
}
@Override
public void print() {
System.out.println("--------------");
System.out.println("Name: " + this.name + "\nPosition: "
+ this.position + "\nContent: " + this.content + " (Root)");
Iterator it = subordinaryList.iterator();
while (it.hasNext()) {
Component comp = it.next();
comp.print();
}
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class CompositeBranch extends Component {
private String name = "";
private int position = 0;
private String content = "";
private List subordinaryList = new ArrayList();
public CompositeBranch(String name, int position, String content) {
super(name, position, content);
this.name = name;
this.position = position;
this.content = content;
}
public List getSubordinary() {
return this.subordinaryList;
}
public void add(Component component) {
component.setParent(this);
subordinaryList.add(component);
}
@Override
public void print() {
System.out.println("------------------");
System.out.println("Name: " + this.name + "\nPosition: "
+ this.position + "\nContent: " + this.content + " (Branch)");
Iterator it = subordinaryList.iterator();
while (it.hasNext()) {
Component comp = it.next();
comp.print();
}
}
}
public class CompositeLeaf extends Component {
private String name = "";
private int position = 0;
private String content = "";
public CompositeLeaf(String name, int position, String content) {
super(name, position, content);
this.name = name;
this.position = position;
this.content = content;
}
@Override
public void print() {
System.out.println("--------------");
System.out.println("Name: " + this.name + "\nPosition: "
+ this.position + "\nContent: " + this.content + " (Leaf)");
}
}
public class Invoker {
private Component node;
public Invoker(Component node) {
this.node = node;
}
public void printNode() {
node.print();
}
}
public class CompositeCaller {
public static void main(String[] args) {
CompositeRoot root = new CompositeRoot("Animal", 0, "Animal");
CompositeBranch cat = new CompositeBranch("Cat", 1, "Cat");
CompositeBranch dog = new CompositeBranch("Dog", 1, "Dog");
CompositeBranch duck = new CompositeBranch("Duck", 1, "Duck");
CompositeLeaf peikingDuck = new CompositeLeaf("PeikingDuck", 2, "PeikingDuck");
CompositeLeaf blackDuck = new CompositeLeaf("BlackDuck", 2, "BlackDuck");
root.add(cat);
root.add(dog);
root.add(duck);
duck.add(peikingDuck);
duck.add(blackDuck);
Invoker invoker = new Invoker(root);
invoker.printNode();
}
}
那么,如何泛型化呢?
其实,在这个结构中,重要的部分就是”树枝“结构或者”根“结构。因为,只有这两个部分才是对整个”树“的结构进行维护的。其中,”树枝“结构可以包含”根“结构。
以下是泛型化代码:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class CompositeRoot extends Component {
private String name = "";
private int position = 0;
private String content = "";
private List subordinaryList = new ArrayList();
private Component root = null;
public CompositeRoot(String name, int position, String content) {
super(name, position, content);
this.name = name;
this.position = position;
this.content = content;
}
protected void setRoot(T root) {
this.root = root;
}
public Component getRoot() {
return this.root;
}
public List getSubordinary() {
return this.subordinaryList;
}
public void add(T component) {
component.setParent(this);
subordinaryList.add(component);
}
@Override
public void print() {
System.out.println("--------------");
System.out.println("Name: " + this.name + "\nPosition: "
+ this.position + "\nContent: " + this.content + " (Root)");
Iterator it = subordinaryList.iterator();
while (it.hasNext()) {
Component comp = it.next();
comp.print();
}
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class CompositeBranch extends Component {
private String name = "";
private int position = 0;
private String content = "";
private List subordinaryList = new ArrayList();
public CompositeBranch(String name, int position, String content) {
super(name, position, content);
this.name = name;
this.position = position;
this.content = content;
}
public List getSubordinary() {
return this.subordinaryList;
}
public void add(T component) {
component.setParent(this);
subordinaryList.add(component);
}
@Override
public void print() {
System.out.println("------------------");
System.out.println("Name: " + this.name + "\nPosition: "
+ this.position + "\nContent: " + this.content + " (Branch)");
Iterator it = subordinaryList.iterator();
while (it.hasNext()) {
Component comp = it.next();
comp.print();
}
}
}
public class CompositeCaller {
public static void main(String[] args) {
CompositeRoot root = new CompositeRoot("Animal", 0, "Animal");
CompositeBranch cat = new CompositeBranch("Cat", 1, "Cat");
CompositeBranch dog = new CompositeBranch("Dog", 1, "Dog");
CompositeBranch duck = new CompositeBranch("Duck", 1, "Duck");
CompositeLeaf peikingDuck = new CompositeLeaf("PeikingDuck", 2, "PeikingDuck");
CompositeLeaf blackDuck = new CompositeLeaf("BlackDuck", 2, "BlackDuck");
root.add(cat);
root.add(dog);
root.add(duck);
duck.add(peikingDuck);
duck.add(blackDuck);
Invoker invoker = new Invoker(root);
invoker.printNode();
}
}