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();
}
}
分享到:
相关推荐
设计模式--组合模式java例子
c++设计模式-结构型模式-组合模式;qt工程;c++简单源码; 组合(Composite Pattern)模式的定义:有时又叫作整体-部分(Part-Whole)模式,它是一种将对象组合成树状的层次结构的模式,用来表示“整体-部分”的关系...
java常用设计模式-组合模式
JAVA-设计模式-结构型模式-组合模式
设计模式-组合模式(讲解及其实现代码)
设计模式专题之(九)组合模式---设计模式组合模式示例代码(python--c++)
自己最近才写的一个设计模式----组合模式小例子,请大家评价
本资源包含一个由Java设计模式中的“组合模式”实现的“空军指挥系统”的全部源代码。注意:本资源含有的代码量比较大,对于急需“项目实战”的朋友很有帮助!需要的朋友可以下载哦!!!!!!!!!!
ios平台中通过最简单的代码讲解组合模式,可在博客http://blog.sina.com.cn/s/blog_161d504630102wxis.html中查看简单解释
设计模式06-适配器、桥接、组合模式 设计模式07-组合模式、装饰模式 设计模式09-外观模式、享元模式 设计模式10-代理模式、结构型模式大复习 设计模式11-行为模式-责任链、命令模式 设计模式12-解释器模式 设计模式...
ios平台中通过最简单的代码讲解组合实体模式,可在博客http://blog.sina.com.cn/s/blog_161d504630102wxis.html中查看简单解释
设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段...
结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。 行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、...
c++设计模式-结构型模式-桥接模式;qt工程,c++简单源码; 桥接(Bridge)模式的定义如下:将抽象与实现分离,使它们可以独立变化。它是用组合关系代替继承关系来实现,从而降低了抽象和实现这两个可变维度的耦合度...
2.2.3 组合模式 272.3 格式化 27 2.3.1 封装格式化算法 27 2.3.2 Compositor 和 Composition 27 2.3.3 策略模式 29 2.4 修饰用户界面 29 2.4.1 透明围栏 29 2.4.2 Monoglyph 30 2.4.3 Decorator 模式 32 2.5 支持...
"设计模式-UML类图" 设计模式是软件开发中的一种解决方案,它提供了一种通用的解决方案来解决软件设计中常见的问题。UML(Unified Modeling Language)是一种标准化的建模语言,用于描述软件系统的结构和行为。下面...
自定义控件 大量 使用 设计模式原则:组合、委托
【例3.2】组合模式-五子棋代码 【例3.3】组合模式-空军指挥系统 【例3.4】组合模式-世界问候语 【例3.7】类适配器模式-客户信息验证 【例3.8】对象适配器模式-字符串排序 【例3.10】外观模式-安全...
结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。 行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、...
《C++20设计模式》学习笔记-第8章组合模式-配套学习代码