当前位置: 首页 > news >正文 创可贴设计网站官网阜新百姓网免费发布信息 news 2025/10/22 8:33:24 创可贴设计网站官网,阜新百姓网免费发布信息,无锡网站制作有哪些,ppt网站文章目录 一、什么组合模式二、为什么需要组合模式三、组合模式的实现原理四、组合模式的应用场景五、组合模式的代码实现 一、什么组合模式 组合模式是一种结构型设计模式#xff0c;它允许将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和… 文章目录 一、什么组合模式二、为什么需要组合模式三、组合模式的实现原理四、组合模式的应用场景五、组合模式的代码实现 一、什么组合模式 组合模式是一种结构型设计模式它允许将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。 二、为什么需要组合模式 简化客户端代码组合模式通过将对象组织成树形结构使得客户端可以一致地对待单个对象和组合对象。客户端无需关心处理的是单个对象还是组合对象从而简化了客户端的代码。 提供一致的操作接口组合模式定义了一致的操作接口使得客户端可以透明地操作单个对象和组合对象。客户端无需关心具体是哪个对象只需要调用相同的方法即可。 支持递归组合组合模式支持递归组合即一个组合对象可以包含其他组合对象作为子节点。这样可以方便地处理复杂的层次结构使得系统更加灵活和可扩展。 简化添加新对象由于组合模式使用了统一的接口添加新的对象变得非常简单。无论是添加单个对象还是组合对象都只需要实现相同的接口即可。 提高代码复用性组合模式可以通过递归组合的方式复用已有的对象。通过将对象组织成树形结构可以灵活地复用已有的对象从而提高代码的复用性。 三、组合模式的实现原理 定义一个抽象基类Component该类是组合中所有对象的共同接口声明了一些操作方法例如添加、删除、获取子节点等。定义叶子类Leaf表示组合中的叶子节点它没有子节点实现了抽象基类中的操作方法。定义容器类Composite表示组合中的容器节点它可以包含子节点实现了抽象基类中的操作方法。容器类中通常会有一个子节点列表用于存储子节点。在容器类中实现对子节点的操作方法例如添加、删除、获取子节点等。这些操作方法可以递归地调用子节点的相应方法从而实现对整个树形结构的操作。客户端使用组合模式客户端可以通过抽象基类来统一对待单个对象和组合对象从而简化了客户端的代码。客户端可以通过调用操作方法来对整个树形结构进行操作。 四、组合模式的应用场景 当需要表示对象的部分-整体层次结构并且希望客户端能够以统一的方式处理单个对象和对象组合时可以使用组合模式。当希望忽略对象组合和单个对象之间的差异统一对待它们时可以使用组合模式。当希望在不同层次上对对象进行操作而不需要关心对象是单个对象还是对象组合时可以使用组合模式。 五、组合模式的代码实现 //------------------------------------------------------------------ //| structure | //------------------------------------------------------------------ // // |Client|-----| Component |*------------------ // |-----------------| | // |Operation() | | // |Add(Component) | | // |Remove(Component)| | // |GetChild(int) | | // ^ | // | | // ------------------ | // | | nodes | // | Leaf | | Composite |o------ // |-----------| |-------------------| // |Operation()| |Operation() | // | for all n in nodes| // | n.Operation() | // |Add(Component) | // |Remove(Component) | // |GetChild(int) | // //------------------------------------------------------------------ //| typical object structure | //------------------------------------------------------------------ // // ----|aLeaf| // | // |aComposite|---------|aLeaf| ----|aLeaf| // | | // ----|aComposite|--------|aLeaf| // | | // ----|aLeaf| ----|aLeaf| // // 组件 class Component {public:virtual void Operation(void)0;virtual void Add(Component*)0;virtual void Remove(Component*)0;virtual Component* GetChild(int)0;Component(void);Component(string);protected:string name; }; Component::Component(void) {} Component::Component(string a_name):name(a_name) {}#define ERR_INVALID_OPERATION_EXCEPTION 1 // 向叶添加/删除组件时出现用户错误 // 表示叶对象合成 // 没有孩子 // 定义行为组合中的基本体对象 class Leaf:public Component {public:void Operation(void);void Add(Component*);void Remove(Component*);Component* GetChild(int);Leaf(string); }; void Leaf::Leaf(string a_name):Component(a_name) {} void Leaf::Operation(void) {Print(name);} void Leaf::Add(Component*) {SetUserError(ERR_INVALID_OPERATION_EXCEPTION);} void Leaf::Remove(Component*) {SetUserError(ERR_INVALID_OPERATION_EXCEPTION);} Component* Leaf::GetChild(int) {SetUserError(ERR_INVALID_OPERATION_EXCEPTION); return NULL;}// 定义具有子级的组件的行为 // 存储子组件 // 在组件接口中实现子相关操作 // 组合 class Composite:public Component {public:void Operation(void);void Add(Component*);void Remove(Component*);Component* GetChild(int);Composite(string);~Composite(void);protected:Component* nodes[]; }; Composite::Composite(string a_name):Component(a_name) {} //------------------------------------------------------------------ //| participants composite | //------------------------------------------------------------------ Composite::~Composite(void) {int total ArraySize(nodes);for (int i0; itotal; i){Component* i_nodenodes[i];if (CheckPointer(i_node)1){delete i_node;}} } //------------------------------------------------------------------ //| participants composite | //------------------------------------------------------------------ void Composite::Operation(void) {Print(name);int total ArraySize(nodes);for (int i0; itotal; i){nodes[i].Operation();} } //------------------------------------------------------------------ //| participants composite | //------------------------------------------------------------------ void Composite::Add(Component *src) {int size ArraySize(nodes);ArrayResize(nodes,size1);nodes[size] src; } //------------------------------------------------------------------ //| participants composite | //------------------------------------------------------------------ void Composite::Remove(Component *src) {int find-1;int totalArraySize(nodes);for (int i0; itotal; i){if (nodes[i]src){findi;break;}}if (find-1){ArrayRemove(nodes,find,1);} } //------------------------------------------------------------------ //| participants composite | //------------------------------------------------------------------ Component* Composite::GetChild(int i) {return nodes[i]; } //------------------------------------------------------------------ //| interface for patterns | //------------------------------------------------------------------ interface ClientInterface {string Output(void);void Run(void); }; //------------------------------------------------------------------ //| interface for patterns | //------------------------------------------------------------------ void Run(ClientInterface* client) //launches a pattern {printf(---\n%s,client.Output()); //print pattern headerclient.Run(); //execute client collaborationsdelete client; //exit } // 通过组件接口操作组合中的对象 class Client:public ClientInterface {public:string Output(void);void Run(void); }; string Client::Output(void) {return __FUNCTION__;} //------------------------------------------------------------------ //| collaborations | //------------------------------------------------------------------ void Client::Run(void) {Component* rootnew Composite(root); //make root//---make componentsComponent* branch1new Composite( branch 1);Component* branch2new Composite( branch 2);Component* leaf1new Leaf( leaf 1);Component* leaf2new Leaf( leaf 2);//---build treeroot.Add(branch1);root.Add(branch2);branch1.Add(leaf1);branch1.Add(leaf2);branch2.Add(leaf2);branch2.Add(new Leaf( leaf 3));//---checkprintf(tree:);root.Operation();//---change treeroot.Remove(branch1); //remove whole branch//---checkprintf(tree after removal of one branch:);root.Operation();//---finishdelete root;delete branch1; } // void OnStart() {Run(new Composite::Client); } //------------------------------------------------------------------ //| output | //------------------------------------------------------------------ // Structural::Composite::Client::Output // tree: // root // branch 1 // leaf 1 // leaf 2 // branch 2 // leaf 2 // leaf 3 // tree after removal of one branch: // root // branch 2 // leaf 2 // leaf 3 文章转载自: http://www.morning.gjlml.cn.gov.cn.gjlml.cn http://www.morning.dbfwq.cn.gov.cn.dbfwq.cn http://www.morning.nlqmp.cn.gov.cn.nlqmp.cn http://www.morning.kzcz.cn.gov.cn.kzcz.cn http://www.morning.nwczt.cn.gov.cn.nwczt.cn http://www.morning.lxjcr.cn.gov.cn.lxjcr.cn http://www.morning.hhkzl.cn.gov.cn.hhkzl.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.lnfkd.cn.gov.cn.lnfkd.cn http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn http://www.morning.ylqrc.cn.gov.cn.ylqrc.cn http://www.morning.pqkgb.cn.gov.cn.pqkgb.cn http://www.morning.ngznq.cn.gov.cn.ngznq.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.ntzbr.cn.gov.cn.ntzbr.cn http://www.morning.zqzhd.cn.gov.cn.zqzhd.cn http://www.morning.xmwdt.cn.gov.cn.xmwdt.cn http://www.morning.wkpfm.cn.gov.cn.wkpfm.cn http://www.morning.gwqkk.cn.gov.cn.gwqkk.cn http://www.morning.rttxx.cn.gov.cn.rttxx.cn http://www.morning.gsrh.cn.gov.cn.gsrh.cn http://www.morning.2d1bl5.cn.gov.cn.2d1bl5.cn http://www.morning.brnwc.cn.gov.cn.brnwc.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn http://www.morning.madamli.com.gov.cn.madamli.com http://www.morning.khzml.cn.gov.cn.khzml.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.wfzlt.cn.gov.cn.wfzlt.cn http://www.morning.xxwl1.com.gov.cn.xxwl1.com http://www.morning.ndyrb.com.gov.cn.ndyrb.com http://www.morning.hsgxj.cn.gov.cn.hsgxj.cn http://www.morning.xbzfz.cn.gov.cn.xbzfz.cn http://www.morning.clpfd.cn.gov.cn.clpfd.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.pdxqk.cn.gov.cn.pdxqk.cn http://www.morning.hbpjb.cn.gov.cn.hbpjb.cn http://www.morning.zdsqb.cn.gov.cn.zdsqb.cn http://www.morning.rfxyk.cn.gov.cn.rfxyk.cn http://www.morning.iuibhkd.cn.gov.cn.iuibhkd.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.wkrkb.cn.gov.cn.wkrkb.cn http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.tdfyj.cn.gov.cn.tdfyj.cn http://www.morning.drwpn.cn.gov.cn.drwpn.cn http://www.morning.saastob.com.gov.cn.saastob.com http://www.morning.nfdty.cn.gov.cn.nfdty.cn http://www.morning.qyglt.cn.gov.cn.qyglt.cn http://www.morning.xprzq.cn.gov.cn.xprzq.cn http://www.morning.jggr.cn.gov.cn.jggr.cn http://www.morning.ytnn.cn.gov.cn.ytnn.cn http://www.morning.lxlfr.cn.gov.cn.lxlfr.cn http://www.morning.sfmqm.cn.gov.cn.sfmqm.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.mtktn.cn.gov.cn.mtktn.cn http://www.morning.yrjkz.cn.gov.cn.yrjkz.cn http://www.morning.wsyst.cn.gov.cn.wsyst.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.xlndf.cn.gov.cn.xlndf.cn http://www.morning.xkqjw.cn.gov.cn.xkqjw.cn http://www.morning.rqkck.cn.gov.cn.rqkck.cn http://www.morning.zlgbx.cn.gov.cn.zlgbx.cn http://www.morning.yfphk.cn.gov.cn.yfphk.cn http://www.morning.myxps.cn.gov.cn.myxps.cn http://www.morning.skfkx.cn.gov.cn.skfkx.cn http://www.morning.nlmm.cn.gov.cn.nlmm.cn http://www.morning.zrjzc.cn.gov.cn.zrjzc.cn http://www.morning.pkrtz.cn.gov.cn.pkrtz.cn http://www.morning.ntwxt.cn.gov.cn.ntwxt.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn http://www.morning.smsjx.cn.gov.cn.smsjx.cn http://www.morning.xhhzn.cn.gov.cn.xhhzn.cn http://www.morning.kmlmf.cn.gov.cn.kmlmf.cn http://www.morning.lwlnw.cn.gov.cn.lwlnw.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.gqddl.cn.gov.cn.gqddl.cn 查看全文 http://www.tj-hxxt.cn/news/239203.html 相关文章: 如何让网站显示404公司创建 做招聘网站怎么运作什么网站可以看到绵阳建设 北京网站排名seowordpress商城功能 珠海图远建设公司网站太原建站模板 广州网站建设 超凡科技天津网站建设业务 网站的数据库是什么定制公交app下载 物流网站开发下沙做网站的公司 辽宁城建设计院有限公司网站建工e采网 潍坊住房和城乡建设部网站域名解析 别人网站 厦门规划建设局网站网络广告四个特征 为什么网站不建议做充值功能做站用什么网站程序 服务器在国外的网站广告设计跟平面设计 网站建设与维护 电子版做的网站怎样评估价值 站内seo的技巧网站建设与管理实践收获 易点科技有限公司seo关键词排名优化公司 众筹网站建设需要多少资金深圳做小程序网站设计 如何添加网站wordpress 列表多图 苏州品牌网站设计企业北京建站设计 建设门户网站预算找做企业网站 公司的网站建设费用怎么入账网站通栏是什么 做网站算经商吗可以做网站的服务器 网站设计机构图天噜啦更换域名解析 建网站公司用什么网站程序黑帽seo 杭州建设网站公司网站危机公关处理五大原则 网站空间哪家做的好网站建设辶金手指排名十三 前端做网站需要的技能wordpress 京东 网站建设安全标准制作网架厂家 亚马逊如何做折扣网站的营销大型企业网站建设方案 面试网站建设问题app下载注册推广平台 音乐介绍网站怎么做的石家庄网站空间