组合模式又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解藕.
组合模式可以优化处理递归或分级数据结构.有许多关于分级数据结构的例子,使得组合模式非常有用武之地.
类图:
组成部分:
Component: 为参加组合的对象声明一个公共接口, 不管是组合还是叶结点.
Leaf: 在组合中表示叶子结点对象,叶子结点没有子结点.
Composite: 表示参加组合的有子对象的对象, 并给出树枝购件的行为.
实例:
public abstract class FolderComponent
{
private String name;
public String getName()
{
return name;
}
public void setName(final String name)
{
this.name = name;
}
public FolderComponent()
{
}
public FolderComponent(final String name)
{
this.name = name;
}
public abstract void add(FolderComponent component);
public abstract void remove(FolderComponent component);
public abstract void display();
}
public class FileLeaf extends FolderComponent
{
public FileLeaf(final String name)
{
super(name);
}
@Override
public void add(final FolderComponent component)
{
//...
}
@Override
public void remove(final FolderComponent component)
{
//...
}
@Override
public void display()
{
System.out.println("FileLeaf:" + this.getName());
}
}
public class FolderComposite extends FolderComponent
{
private final List<FolderComponent> components;
public FolderComposite(final String name)
{
super(name);
this.components = new ArrayList<FolderComponent>();
}
public FolderComposite()
{
this.components = new ArrayList<FolderComponent>();
}
@Override
public void add(final FolderComponent component)
{
this.components.add(component);
}
@Override
public void remove(final FolderComponent component)
{
this.components.remove(component);
}
@Override
public void display()
{
System.out.println("FolderComposite---name:" + this.getName());
for (final FolderComponent component : components)
{
System.out.println("FolderComposite---component-name:" + component.getName());
}
}
}
public class Client
{
public static void main(final String[] args)
{
final FolderComponent leaf = new FileLeaf("runnable file");
leaf.display();
final FolderComponent folder = new FolderComposite("new folder");
folder.add(new FileLeaf("content1 in new folder"));
folder.add(new FileLeaf("content2 in new folder"));
folder.display();
}
}
结果:
FileLeaf:runnable file
FolderComposite---name:new folder
FolderComposite---component-name:content1 in new folder
FolderComposite---component-name:content2 in new folder