设计模式--组合模式
Composite模式定义:
将对象以树形结构组织起来,以达成“部分-整体” 的层次结构,使得客户端对单个对象和组合对象的使用具有一致性.
Composite比较容易理解,想到Composite就应该想到树形结构图。组合体内这些对象都有共同接口,当组合体一个对象的方法被调用执行时,Composite将遍历(Iterator)整个树形结构,寻找同样包含这个方法的对象并实现调用执行。可以用牵一动百来形容。
Composite好处:
1.使客户端调用简单,客户端可以一致的使用组合结构或其中单个对象,用户就不必关系自己处理的是单个对象还是整个组合结构,这就简化了客户端代码。
2.更容易在组合体内加入对象部件. 客户端不必因为加入了新的对象部件而更改代码。
package composite;public abstract class Component {public abstract void operation();public abstract void add(Component child) throws AbstractMethodError;public abstract void remove(Component child) throws AbstractMethodError;public abstract Component getChild(int index) throws AbstractMethodError;}
package composite;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class Composite extends Component {private final List<Component> fChildren = new ArrayList<Component>();public void operation() {Iterator<Component> iterator = fChildren.iterator();while (iterator.hasNext()) {((Component) iterator.next()).operation();}}public void add(Component child) throws AbstractMethodError {fChildren.add(child);}public void remove(Component child) throws AbstractMethodError {fChildren.remove(child);}public Component getChild(int index) throws AbstractMethodError {return (Component) fChildren.get(index);}}
package composite;/** * @author ZERO */public class Leaf_A extends Component {private static final String ERROR_MSG = "This method should never be called.";public void operation() {System.out.println("Leaf_A=====TODO");}public void add(Component child) throws AbstractMethodError {throw new AbstractMethodError(ERROR_MSG);}public void remove(Component child) throws AbstractMethodError {throw new AbstractMethodError(ERROR_MSG);}public Component getChild(int index) throws AbstractMethodError {throw new AbstractMethodError(ERROR_MSG);}}
package composite;/** * @author ZERO */public class Leaf_B extends Component {private static final String ERROR_MSG = "This method should never be called.";public void operation() {System.out.println("Leaf_B=====TODO");}public void add(Component child) throws AbstractMethodError {throw new AbstractMethodError(ERROR_MSG);}public void remove(Component child) throws AbstractMethodError {throw new AbstractMethodError(ERROR_MSG);}public Component getChild(int index) throws AbstractMethodError {throw new AbstractMethodError(ERROR_MSG);}}
package composite;/** * @author ZERO */public class Leaf_C extends Component {private static final String ERROR_MSG = "This method should never be called.";public void operation() {System.out.println("Leaf_C=====TODO");}public void add(Component child) throws AbstractMethodError {throw new AbstractMethodError(ERROR_MSG);}public void remove(Component child) throws AbstractMethodError {throw new AbstractMethodError(ERROR_MSG);}public Component getChild(int index) throws AbstractMethodError {throw new AbstractMethodError(ERROR_MSG);}}
package composite;/** * @author ZERO */public class Client {private Component fComponent = null;public Client() {super();}public Client(Component component) {super();fComponent = component;}public void createComposite() {fComponent = new Composite();Composite composite = new Composite();composite.add(new Leaf_A());composite.add(new Leaf_B());fComponent.add(composite);fComponent.add(new Leaf_C());fComponent.getChild(0).add(new Leaf_A());fComponent.getChild(0).add(new Leaf_A());}public void useComposite() {fComponent.operation();}public static void main(String[] args) {Client client = new Client();client.createComposite();client.useComposite();System.out.println("-----------");client.fComponent.operation();System.out.println("-----------");client.fComponent.getChild(0).operation();System.out.println("-----------");client.fComponent.getChild(1).operation();}}