首页 / 设计模式 / 设计模式(十四)——组合模式
设计模式(十四)——组合模式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了设计模式(十四)——组合模式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3131字,纯文字阅读大概需要5分钟。
内容图文
1.描述
将对象组合成树形结构一以表示“部分——整体”的层次结构。组合模式使用户对单个对象和组合对象的使用具有一致性。
2.模式的使用
·抽象组件(Component):是一个接口或抽象类,该接口定义了个体对象和组合对象需要实现的关于操作器子节点的方法,比如添加add()、删除remove()、等方法。
·Composite节点(Composite Node):实现Component接口类的实例,节点不但实现Component接口,也可以包含其他Composite节点或Leaf节点的引用
·Leaf节点(Leaf Node):实现Component接口的类的实例,Leaf节点实现Component节点,不可以包含其他Composite节点或Leaf节点的引用。
3.使用情景
·当想表示对象的部分——整体层次结构。
·希望用户用一致的处理方式处理个体对象和组合对象。
4.优点
·组合模式包含个体对象和组合对象,并形成树形结构,使用户可以方便的处理个体对象和组合对象。
·组合对象和个体对象吧实现了统一接口,用户一般无需区分个体对象和组合对象。
·当增加新的Conposite节点或时,用户的重要代码不需要修改。
5.UML图
6案例
某军队的编制中,连长下面两个排长,排长下面三个班长,班长下面10个士兵。使用组合模式将这些对象构造成属性结构(一些姓名、工资属性不是重点)。
1 package 组合模式; 2 3 import java.util.Iterator; 4 import java.util.LinkedList; 5 6 public class test1 { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 11 } 12 13 public static double computeSalary(MilitaryPerson person){//计算该单位下所有成员工资总和 14//使用的递归 15double sum = 0; 16if(person.isLeaf()) 17 sum += person.getSalary(); 18 19if(!person.isLeaf()){ 20 sum += person.getSalary(); 21 Iterator<MilitaryPerson> iterator = person.getAllChildren(); 22while(iterator.hasNext()){ 23 MilitaryPerson p = iterator.next(); 24 sum = sum + computeSalary(p); 25 } 26 } 27return sum; 28 } 29} 30 31/* 32 * 抽象组件 33*/ 34interface MilitaryPerson{ 35publicvoid add(MilitaryPerson person);//添加成员 36publicvoid remove(MilitaryPerson person);//删除成员 37public MilitaryPerson getChild(int index);//按下表获取成员 38public Iterator<MilitaryPerson> getAllChildren();//获取该单位下面所有成员的遍历迭代器 39publicboolean isLeaf();//判断是否为Leaf节点 40publicdouble getSalary(); 41publicvoid setSalary(double salary); 42} 43 44/* 45 * composite节点——班长、排长、连长 46*/ 47class MilitaryOfficer implements MilitaryPerson{ 48 LinkedList<MilitaryPerson> list; 49 String name; 50double salary; 51 MilitaryOfficer(String name, double salary){ 52this.name = name; 53this.salary = salary; 54 list = new LinkedList<MilitaryPerson>(); 55 } 56publicvoid add(MilitaryPerson person) { 57 list.add(person); 58 } 59 60publicvoid remove(MilitaryPerson person) { 61 list.remove(person); 62 } 63 64public MilitaryPerson getChild(int index) { 65return list.get(index); 66 } 67 68public Iterator<MilitaryPerson> getAllChildren() { 69return list.iterator(); 70 } 71 72publicboolean isLeaf() { 73returnfalse; 74 } 75 76publicdouble getSalary() { 77return salary; 78 } 79 80publicvoid setSalary(double salary) { 81this.salary = salary; 82 } 83 84} 85 86/* 87 * Leaf节点——士兵 88*/ 89class MilitarySoldier implements MilitaryPerson{ 90double salary; 91 String name; 92 MilitarySoldier(double salary, String name){ 93this.salary = salary; 94this.name = name; 95 } 96publicvoid add(MilitaryPerson person) {} 97 98publicvoid remove(MilitaryPerson person) {} 99100public MilitaryPerson getChild(int index) {returnnull;} 101102public Iterator<MilitaryPerson> getAllChildren() {returnnull;} 103104publicboolean isLeaf() {returntrue;} 105106publicdouble getSalary() { 107return salary; 108 } 109110publicvoid setSalary(double salary) { 111this.salary = salary; 112 } 113114 }
大概结构就是这样,如果演示的话需要创建一堆士兵和长官的对象,太麻烦。
原文:http://www.cnblogs.com/cxy2016/p/7670296.html
内容总结
以上是互联网集市为您收集整理的设计模式(十四)——组合模式全部内容,希望文章能够帮你解决设计模式(十四)——组合模式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。