八、组合模式Composite(结构型)
来源:互联网 发布:免费淘宝号和密码大全 编辑:程序博客网 时间:2024/05/22 17:07
将对象组合成树形结构以表示“部分-整体”的层次结构,Composite模式使得用户对单个对象和组合对象的使用具有一致性。例如,在office中绘图,可以将若干图形组合成为组,当对组操作时会影响所有的组内元素,这里就是把组合图形作为单个元素来看待。
使用Composite模式时可以表示对象的部分-整体层次结构,可以让你忽略组合对象和单个对象的不同,它还可以简化客户代码,使得更容易增加新类型的组件,使你的设计更加一般化。
public interface Component {public String getName();public void setGroup(String group);//public String operation();public void add(Component component);public void remove(Component component);public Component getChild(String name);}public class Composite implements Component {private String name = null;private String group = null;public Composite(String name) {this.name = name;}public String getName() {return name;}public void setGroup(String group) {this.group = group;}public String toString() {return group + "-" + name;}//private Map<String, Component> childs = new HashMap<>();@Overridepublic String operation() {StringBuilder ss = new StringBuilder();ss.append(toString()+":[");for (String name : childs.keySet()) {ss.append(childs.get(name).operation()+",");}ss.append("]");return ss.toString();}@Overridepublic void add(Component component) {if (component != null) {component.setGroup(name);childs.put(component.getName(), component);}}@Overridepublic void remove(Component component) {childs.remove(component);}@Overridepublic Component getChild(String name) {return childs.get(name);}}public class Leaf implements Component {private String name = null;private String group = null;public Leaf(String name) {this.name = name;}public String getName() {return name;}public void setGroup(String group) {this.group = group;}public String toString() {return group + "-" + name;}//@Overridepublic String operation() {return toString();}@Overridepublic void add(Component component) {// do nothing}@Overridepublic void remove(Component component) {// do nothing}@Overridepublic Component getChild(String name) {// do nothingreturn null;}}public class Client {/**构成结构为 * <pre> * root * ____|____ * | | * node leaf1 * __|__ * | | * leaf2 leaf3 * </pre> * */@Testpublic void test() {Component leaf1 = new Leaf("leaf1");Component leaf2 = new Leaf("leaf2");Component leaf3 = new Leaf("leaf3");Component root = new Composite("root");Component node = new Composite("node");root.add(leaf1);root.add(node);node.add(leaf2);node.add(leaf3);System.out.println(root.operation());}}
组合模式的最终效果看起来就像一棵树,当调用最上层root的operation时,下层的child的operation方法就会被级联调用,故可以将它们每个组都看成一个整体。这里就是在Composite类中存储了一个Map用于存储所有的子Component,当遍历时,就直接遍历这个Map。在使用组合模式时也可以在类中加入父组件的引用,方便父组件对子组件的管理,而且需要最大化父组件的接口,以方便各种对组件的操作。
0 0
- 八、组合模式Composite(结构型)
- 八、结构模式之组合(Composite)模式
- 结构型模式-----组合模式(Composite)
- Composite模式(组合模式 结构型)
- 设计模式(八):组合模式Composite(结构型模式)
- Composite 组合(结构型模式)
- 结构型模式: Composite(组合)
- 组合模式Composite(结构型)
- Composite组合(结构型模式)
- 【结构型模式】composite(组合)
- 结构型模式-组合(composite)
- 结构型-组合模式(composite)
- 设计模式(八)之 Composite(组合)
- 设计模式(七)组合模式Composite(结构型)
- 设计模式(七)组合模式Composite(结构型)
- 设计模式(七)组合模式Composite(结构型)
- 设计模式(九)组合模式(Composite)-结构型
- 设计模式(七)组合模式Composite(结构型)
- 拥抱JPA规范(转载)
- shiro-realm认证
- 132. Palindrome Partitioning II
- AsyncTask_HttpURLConnection_一键退出案例
- POJ-2624 4th Point(计算几何)
- 八、组合模式Composite(结构型)
- Codeforces 115A Party
- 打字
- Unbuntu下安装jdk
- 看了简书的感想
- listview多条目展示
- 集合思维导图
- 侧滑的使用
- 【Android】使用Picasso加载图片的内存优化实践