已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
设计模式-Composite(组合)模式GoF:将对象组合合成树形结构以表示“部分-整体”的层次结构。Composite使得用户对单个对象和组合对象的使用具有一致性。组合体内这些对象都有共同接口,当组合体一个对象的方法被调用执行时,Composite将遍历(Iterator)整个树形结构,寻找同样包含这个方法的对象并实现调用执行。可以用牵一动百来形容。Composite模式描述了并实现了树形目录结构,在该模式中要有一个组合体,它是一个抽象类或接口(Component),组合体中定义足够多的方法。当组合体(Component)一个对象的方法被调用执行时,Composite将遍历(Iterator)整个树形结构,寻找同样包含这个方法的对象并实现调用执行。在Composite模式中要用到Iterator(迭代器)模式和Chain of Responsibility(职责链)模式。这两个模式我还没有学到,但是Java中的Iterator我用过。暂时先将Composite模式实现。以组装电脑为例,首先需要一个组合体:package Composite;import java.util.Iterator;public abstract class EquipmentComponent String E_name; public String getName() return E_name; /end getName(.) public void setName(String strName) this.E_name = strName; /end setName(.) public double getPrice(int eNumber, double price) double equipmentPrice = 0; equipmentPrice = eNumber * price; return equipmentPrice; /end getPrice() public abstract double equipmentPrice(int eNumber, double price); public boolean add(EquipmentComponent eq) return false; /end add(.) public boolean remove(EquipmentComponent eq) return false; /end remove(.) public Iterator iter() return null; /end iter()/end abstract class EquipmentComponent接着我们实现一个软盘的类:package Composite;public class FloppyDisk extends EquipmentComponent public FloppyDisk() /do something. /end FloppyDisk() public double equipmentPrice(int eNumber, double price) double equipmentPrice = getPrice(eNumber, price); return equipmentPrice; /end equipmentPrice() /end class FloppyDisk下面实现Composite类:package Composite;import java.util.ArrayList;import java.util.Iterator;public class EquipmentComposite extends EquipmentComponent /* Creates a new instance of EquipmentComposite */ public EquipmentComposite() /do something. /end EquipmentComposite() private ArrayList equipment = new ArrayList(); public double equipmentPrice(int eNumber, double price) double equipmentPrice = 0; Iterator eIter = equipment.iterator(); while(eIter.hasNext() equipmentPrice += (EquipmentComponent)eIter.next().getPrice(eNumber, price); return equipmentPrice; /end equipmentPrice(.) public boolean add(EquipmentComponent eqc) boolean addBool = equipment.add(eqc); if (addBool) return true; else return false; /end add(.) public boolean remove(EquipmentComponent eqc) if(equipment.size() = 0) return false; boolean removeBool = equipment.remove(eqc); if(removeBool) return true; else return false; /end remove(.) /end class EquipmentComposite接着实现一个箱子类:package Composite;public class Cabinet extends EquipmentComposite public Cabinet() /do something. /end Mosue() public double equipmentPrice(int eNumber, double price) double chassisPrice = super.equipmentPrice(eNumber, price); return chassisPrice; /end equipmentPrice(.) /class Cabinet最后看看调用:/* * CompositePattern.java * * Created on 2006年3月30日, 上午11:14 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */package Composite;/* * * author Administrator */public class CompositePattern Cabinet chassis = new Cabinet(); /* * Creates a new instance of CompositePattern */ public CompositePattern() /do something. /end CompositePattern() public void showCompositePattern() chassis.setName(Power); String chassisName = chassis.getName(); chassis.add(new FloppyDisk(); double chassisPrice = chassis.equipmentPrice(1, 32.5); System.out.println(The chassis name is: + chassisName); System.out.println(The chassis price is: + chassisPrice); /end showCompositePattern() public static void main(String args) System.out.println(*-*); System.out.println(The Composite Pattern!); CompositePattern cp = new CompositePattern(); cp.showCompositePattern(); System.out.println(*-*); /end main(.) /end class CompositePattern下面是Composite模式的UML图:其中:EquipmentComponent,为组合中的对象声明接口;在适当情况下实现所有类共有接口的缺省行为。FloppyDisk,在组合中表示叶结点对象,叶结点没有子结点。EquipmentComposite,定义有子部件的那些行为;存储子部件。Chassis,子部件,用于存储叶结点。在上例的代
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025智能道路交通信号灯绿色长明实施节能效益分析
- 2025智能船舶行业市场运行分析及发展趋势与投资战略研究报告
- 2025智能窗帘行业市场现状分析投资评估规划发展研究报告
- 仪器采购合同协议
- 企业产品合同范本
- 企业劳动合同协议
- 企业管理协议合同
- 会员入会合同范本
- 会展承揽合同范本
- 会计培训合同范本
- 全国优质课一等奖初中语文八年级上册第14课《昆明的雨》课件
- 超高分子量聚乙烯复合材料UD布项目环境影响报告表
- GB/T 7901-2018黑胡椒
- GB/T 20241-2021单板层积材
- GB/T 1182-2018产品几何技术规范(GPS)几何公差形状、方向、位置和跳动公差标注
- 项目合作协议-非框架协议版
- 小品《你睡了没》台词剧本手稿
- (完整)辅警考试公安基础知识考试试题库及答案
- 网约车平台服务合作协议范本
- 170位真实有效投资人邮箱
- 中等职业教育专业目录(2022年)
评论
0/150
提交评论