声明:
本文只为方便我个人查阅和理解,详细的分析以及源代码请移步 原作者的博客http://chjavach.iteye.com/
import java.util.ArrayList;
import java.util.List;
abstract class Component {
public abstract void printStruct(String preStr);
/*以下三个方法简单地抛出例外。叶子类Leaf不覆盖这些方法,表示不支持。而Composite则重写这些方法
* 这涉及到组合模式的透明性和安全性,就是说叶子类要不要知道这些方法
* 如果从安全性考虑,那叶子类不要知道这些方法,就不要在父类(Component)中定义
* 个人认为透明性更重要一些,可以将Leaf和Composite的操作统一起来
*/
public void addChild(Component child) {
throw new UnsupportedOperationException("addChild():not allowed.");
}
public void removeChild(Component child) {
throw new UnsupportedOperationException("removeChild():not allowed.");
}
public Component getChild(int index) {
throw new UnsupportedOperationException("getChild():not allowed.");
}
/*书上提到两个扩展:
* 1.父组件引用。主要用在删除上:当子目录删除时,子目录下的元素上移一层(将这些元素的父目录设为子目录的上一层):
private Component parent;
private List<Component> children;
* 2.环状引用。要记录路径,以免重复
*/
}
class Leaf extends Component {
private String name;
public Leaf(String name) {
this.name = name;
}
@Override
public void printStruct(String preStr) {
System.out.println(preStr + "-" + name);
}
}
class Composite extends Component {
private String name;
private List<Component> list;
public Composite(String name) {
this.name = name;
}
public void addChild(Component child) {
if (list == null) {
list = new ArrayList<Component>();
}
list.add(child);
}
public void removeChild(Component child) {
if (list != null) {
list.remove(child);
}
}
@Override
public void printStruct(String preStr) {
System.out.println(preStr + "+" + name);
if (list != null) {
preStr += " ";
for (Component child : list) {
child.printStruct(preStr);
}
}
}
}
public class CompositePattern {
public static void main(String[] args) {
Component root = new Composite("服装");
Component c1 = new Composite("男装");
Component c2 = new Composite("女装");
// 定义所有的叶子对象
Component leaf1 = new Leaf("衬衣");
Component leaf2 = new Leaf("夹克");
Component leaf3 = new Leaf("裙子");
Component leaf4 = new Leaf("套装");
// 按照树的结构来组合组合对象和叶子对象
root.addChild(c1);
root.addChild(c2);
c1.addChild(leaf1);
c1.addChild(leaf2);
c2.addChild(leaf3);
c2.addChild(leaf4);
// 调用根对象的输出功能来输出整棵树
root.printStruct("");
}
}