设计模式-Composite(结构型模式) 用于 递归构建 树 状 的组合结构,与Decorator的区别是 Composite旨在通过构造子类而添加新操作,而Decorator直接添加新操作。
以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码
//Component.h
- #pragma once
- class Component
- {
- public:
- Component();
- virtual ~Component();
- virtual void Operation() = ;
- virtual void Add(const Component&);
- virtual void Remove(const Component&);
- virtual Component* getChild(int);
- protected:
- private:
- };
//Component.cpp
- #include"Component.h"
- Component::Component(){}
- Component::~Component(){}
- void Component::Add(const Component& com){}
- void Component::Remove(const Component& com){}
- Component* Component::getChild(int index)
- {
- return ;
- }
//composite.h
- #include"Component.h"
- #include<vector>
- class Composite :public Component
- {
- public:
- Composite();
- virtual ~Composite();
- void Add(Component* com);
- void Remove(Component* com);
- void Operation();
- Component* Getchild(int index);
- protected:
- private:
- std::vector<Component*>comVec;
- };
//Composite.cpp
- #include"Component.h"
- #include"composite.h"
- const int null = ;
- Composite::Composite(){}
- Composite::~Composite(){}
- void Composite::Operation(){
- for (std::vector<Component*>::iterator comIter = comVec.begin(); comIter != comVec.end(); ++comIter)
- {
- (*comIter)->Operation();
- }
- }
- void Composite::Add(Component* com)
- {
- comVec.push_back(com);
- }
- void Composite::Remove(Component* com)
- {
- //comVec.erase(&com);//此处有问题,求解释!!!
- }
- Component* Composite::Getchild(int index)
- {
- return comVec[index];
- }
//Leaf.h
- #include"Component.h"
- class Leaf :public Component
- {
- public:
- Leaf();
- virtual ~Leaf();
- void Operation();
- protected:
- private:
- };
//Leaf.cpp
- #include"Leaf.h"
- #include<iostream>
- Leaf::Leaf(){
- }
- Leaf::~Leaf(){}
- void Leaf::Operation(){
- std::cout << "Leaf Operation..." << std::endl;
- }
//main.cpp
- #include"Component.h"
- #include"composite.h"
- #include"Leaf.h"
- #include<iostream>
- #include<string>
- int main(int args, char* argv)
- {
- Leaf* I = new Leaf();
- I->Operation();
- Composite* com = new Composite();
- com->Add(I);
- com->Operation();
- Component* II = com->Getchild();
- getchar();
- return ;
- }
设计模式-Composite(结构型模式) 用于 递归构建 树 状 的组合结构,与Decorator的区别是 Composite旨在通过构造子类而添加新操作,而Decorator直接添加新操作。的更多相关文章
- GoF的23种设计模式之结构型模式的特点和分类
结构型模式描述如何将类或对象按某种布局组成更大的结构.它分为类结构型模式和对象结构型模式,前者采用继承机制来组织接口和类,后者釆用组合或聚合来组合对象. 由于组合关系或聚合关系比继承关系耦合度低,满足 ...
- Go语言实现的23种设计模式之结构型模式
摘要:本文主要聚焦在结构型模式(Structural Pattern)上,其主要思想是将多个对象组装成较大的结构,并同时保持结构的灵活和高效,从程序的结构上解决模块之间的耦合问题. 本文分享自华为云社 ...
- Java设计模式之结构型模式
结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接模式.组合模式.享元模式. 一.适配器模式: 意图: 将一个类的接口转换成客户希望的另外一个接口.Adapter 模式使得原本由于接 ...
- Java经典23种设计模式之结构型模式(一)
结构型模式包含7种:适配器模式.桥接模式.组合模式.装饰模式.外观模式.享元模式.代理模式. 本文主要介绍适配器模式和桥接模式. 一.适配器模式(Adapter) 适配器模式事实上非常easy.就像手 ...
- 设计模式-Decorator(结构型模式) 用于通过 组合 的方式 给定义的类 添加新的操作,这里不用 继承 的原因是 增加了系统的复杂性,继承使深度加深。
以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //Decorator.h #pragma once class Component { public: virtual ~C ...
- GoF23种设计模式之结构型模式之组合模式
一.概述 将对象组合成树型结构以表示“部分--整体”的层次关系.组合模式使得用户对单个对象和组合对象的使用具有一致性. 二.适用性 1.你想表示对象的部分--整体层次结构的时候. 2.你希望用户忽略组 ...
- GoF23种设计模式之结构型模式之外观模式
一.概述 为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 二.适用性 1.当你要为一个复杂子系统提供一个简单接口的时候.子系统 ...
- GoF23种设计模式之结构型模式之桥接模式
一.概述 将类的抽象部分与实现分部分离开来,使它们都可以独立地变化. 二.适用性 1.你不希望在抽象和实现之间有一个固定的绑定关系的时候.例如:在程序运行时实现部分应可以被选择或切换. ...
- GoF23种设计模式之结构型模式之享元模式
一.概述 运用共享技术有效地支持大量细粒度的对象. 二.适用性 1.当一个应用程序使用了大量的对象的时候. 2.由于使用大量的独享而造成很大的存储开销的时候. 3.对象的大多数状态都可变为外部状态的 ...
随机推荐
- 设置Redis的LRU策略
概念 LRU(Least Recently Used)最近最少使用算法是众多置换算法中的一种. maxmemory Redis中有一个maxmemory概念,主要是为了将使用的内存限定在一个固定的大小 ...
- How to: Use the Entity Framework Code First in XAF 如何:在 XAF 中使用EF CodeFirst
This topic demonstrates how to create a simple XAF application with a business model in a DbContext ...
- JavaScript 标准内置对象Promise使用学习总结
Javascript标准内置对象Promise使用学习总结 by:授客 QQ:1033553122 1. 基础用法 var condition = true; let p = new Prom ...
- C lang: Compound literal
Xx_Introduction C99 stantard. Upate array and struct a compound literal. Literal is date type value. ...
- dex方法隐藏后的反编译和运行时 效果
隐藏smali方法后 java源码: int b = fun2(); baksmali解释为: invoke-virtual {v1}, <int MainAc ...
- 表单生成器(Form Builder)之mongodb表单数据查询——返回分页数据和总条数
上一篇笔记将开始定义的存储结构处理了一下,将FormItems数组中的表单项都拿到mongodb document的最外层,和以前的关系型数据类似,之不过好多列都是动态的,不固定,不过这并没有什么影响 ...
- CG-CTF SQL注入
SQL注入1 题目 访问题目网址 先查看一下源码 仔细分析一下核心源码 <?php if($_POST[user] && $_POST[pass]) { //判断user和pas ...
- React: React组件的生命周期
一.简介 在前面的第二篇博文中对组件的生命周期虽然做了一个大略介绍,但总感觉说的过于简单,毕竟生命周期是React组件的核心部分.在我们熟练使用React挂载和合成组件来创建应用表现层的过程中,针对数 ...
- sublime插件开发教程3
今天就看下api的使用方法 中文的api文档 https://mux.alimama.com/posts/549.html#sublime.View sublime模块 方法 返回值 描述 set_t ...
- requeests模块响应体属性和方法重新整理
下面的属性方法都是基于response对象` import requests response = requests.get('url') 一.url 返回值的url 二. text 获得响应体文本信 ...