📄 compositestructure.java
字号:
//: composite:CompositeStructure.java
package composite;
import java.util.*;
import junit.framework.*;
interface Component {
void operation();
}
class Leaf implements Component {
private String name;
public Leaf(String name) { this.name = name; }
public String toString() { return name; }
public void operation() {
System.out.println(this);
}
}
class Node extends ArrayList implements Component {
private String name;
public Node(String name) { this.name = name; }
public String toString() { return name; }
public void operation() {
System.out.println(this);
for(Iterator it = iterator(); it.hasNext(); )
((Component)it.next()).operation();
}
}
public class CompositeStructure extends TestCase {
public void test() {
Node root = new Node("root");
root.add(new Leaf("Leaf1"));
Node c2 = new Node("Node1");
c2.add(new Leaf("Leaf2"));
c2.add(new Leaf("Leaf3"));
root.add(c2);
c2 = new Node("Node2");
c2.add(new Leaf("Leaf4"));
c2.add(new Leaf("Leaf5"));
root.add(c2);
root.operation();
}
public static void main(String args[]) {
junit.textui.TestRunner.run(CompositeStructure.class);
}
} ///:~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -