:组合模式:Component
#ifndef __COMPONENT_H__
#define __COMPONENT_H__
#include <iostream>
#include <vector>
using namespace std;
class Component
{
public:
virtual ~Component(){}
virtual void add(Component *c) {}
virtual void remove(Component *c) {}
virtual Component* getChild(int i) { return NULL; }
virtual const char* getName() { return ""; }
virtual const char* getDescripthion() { return ""; }
virtual float getPrice() { return 0; }
virtual bool isVegetarian() { return false; }
virtual void print() {}
}; class MenuItem : public virtual Component
{
private:
const char* name;
const char* description;
float price;
bool vegetarian;
public:
MenuItem(const char* n, const char* d, bool v, float p)
{
name = n;
description = d;
price = p;
vegetarian = v;
} virtual~MenuItem(){} virtual const char* getName()
{
return name;
} virtual const char* getDescripthion()
{
return description;
} virtual float getPrice()
{
return price;
} virtual bool isVegetarian()
{
return vegetarian;
} virtual void print()
{
cout <<"Type:MenuItem, Name: "<< getName() << (isVegetarian() ? "isVe" : "NotVe") << getPrice() << getDescripthion() << endl;
} }; class Menu :public virtual Component
{
private:
const char* name;
const char* description;
vector<Component *>menuComponent;
public:
Menu(char* n, const char* d)
{
name = n;
description = d;
} virtual ~Menu()
{ } virtual void add(Component *c)
{
menuComponent.push_back(c);
} virtual void remove(Component *c)
{
// vector<Component *>::iterator it =find(menuComponent.begin(), menuComponent.end(), c);
// if (it!= menuComponent.end())
// {
// menuComponent.erase(remove(menuComponent.begin(), menuComponent.end(), c), menuComponent.end());
// }
} Component* getChild(int i)
{
return menuComponent[i];
} virtual const char* getName()
{
return name;
} virtual const char* getDescripthion()
{
return description;
} virtual void print()
{
cout << "Type:Menu, Name: " << getName() << getDescripthion() << endl;
vector<Component *>::iterator it = menuComponent.begin();
while (it != menuComponent.end())
{
(*it)->print();
it++;
}
}
}; class Waitress
{
private:
Component *menu;
public:
Waitress(Component *c)
{
menu = c;
}
void print()
{
menu->print();
}
}; #endif
#include <iostream>
#include "Component.h"
using namespace std;
int main()
{
Component *pancakeMenu = new Menu("Pancake House Menu", "BreakFast");
Component *dinnerMenu = new Menu("Dinner Menu", "Launch");
Component *caffeMenu = new Menu("Caffe Menu", "Dinner");
Component *dessertMenu = new Menu("Dessert Menu", "Dessert of course"); Component *all = new Menu("All Menu","All Menu Combined");
all->add(pancakeMenu);
all->add(dinnerMenu);
all->add(caffeMenu); dinnerMenu->add(new MenuItem("Pasta","Spaghetti with Marinara sauce, and a slice of sourdough bread", true, 3.89));
dinnerMenu->add(dessertMenu);
dessertMenu->add(new MenuItem("Apple Pie", "Apple Pie With a flaky crust", true, 1.59));
Waitress *wa = new Waitress(all);
wa->print();
return 0;
}
:组合模式:Component的更多相关文章
- JAVA 设计模式 组合模式
用途 组合模式 (Component) 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有唯一性. 组合模式是一种结构型模式. 结构
- Java设计模式——组合模式
JAVA 设计模式 组合模式 用途 组合模式 (Component) 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有唯一性. 组合模式是一种结构型模 ...
- ComponentPattern (组合模式)
import java.util.LinkedList; /** * 组合模式 * * @author TMAC-J 主要用于树状结构,用于部分和整体区别无区别的场景 想象一下,假设有一批连锁的理发店 ...
- 设计模式(十)组合模式(Composite Pattern)
一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象 ...
- 设计模式--组合模式Composite(结构型)
一.概念 组合模式允许你将对象组合成树形结构来表现"整体/部分"层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 二.UML图 1.Component(对象接口),定义 ...
- 组合模式/composite模式/对象结构型模式
组合模式/composite模式/对象结构型 意图 将对象组合成树形结构以表示"整体--部分"的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 动机 C ...
- c#设计模式-组合模式
在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象和复合对象 ...
- C#设计模式系列:组合模式(Composite)
1.组合模式简介 1.1>.定义 组合模式主要用来处理一类具有“容器特征”的对象——即它们在充当对象的同时,又可以作为容器包含其他多个对象. 1.2>.使用频率 中高 2.组合模式结构图 ...
- php实现设计模式之 组合模式
<?php /** * 组合模式 * * 将对象组合成树形结构以表示"部分-整体"的层次结构,使得客户对单个对象和复合对象的使用具有一致性 * * * 1) 抽象构件角色Co ...
- java设计模式之组合模式
组合模式 组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性.掌握组合模式的重点是要理解清楚 “部分/整体” 还有 ”单个对象“ 与 & ...
随机推荐
- 【转载】LINUX下安装wget命令(SFTP实现法)
如何安装wget命令. 方法一:通过yum 命令行为:yum install wget 完成.此操作很简单,但是我安装的linux是centos的最小版本,运行上述命令时会出现无法连接到源网站(大概是 ...
- python中configparser模块的使用
configparser模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 首先要写一个如下所示的配置文件: [DEFAULT] serv ...
- 【文献04】无人驾驶高速AWID-AWIS车辆运动控制研究
参考:阮久宏, 李贻斌, 荣学文, et al. 无人驾驶高速AWID-AWIS车辆运动控制研究[J]. 农业机械学报, 2009, 40(12):37-42. https://drive.wps.c ...
- 数组<-->变量
/** * *数组与变量之间转换 **/ $name='jb51'; $email='jb51@jb51.net'; $info=compact('name','email'); print_r($i ...
- A strange lift HDU - 1548
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 ...
- http bass
1.http 是超文本传输协议,是从万维网服务器传输超文本到本地浏览器的传输协议 2.http是一个基于tcp/ip通信协议来传输数据(html,图片,查询结果等) 3.一个完整的http请求包含7个 ...
- pyqt5安装命令
第一步:安装qt5 pip install pyqt5==5.10.1 -i https://pypi.doubanio.com/simple pip install pyqt5-tools -i h ...
- iphone手机在网页返回上一页时,部分字体变大问题
最近做一个项目是,发现了一个iphone的兼容性问题,在返回上一页后,部分字体会变大,刷新就会正常. 经过总结,发现都是span标签里面的字体变大.经过查询发现,需要给span添加一个属性:displ ...
- 厉害了WORD大S
REPORT YLYTEST01. ) TYPE C VALUE 'ABC'. WRITE LV_C TO LV_C RIGHT-JUSTIFIED. '. WRITE LV_C. 结果: 另外收藏一 ...
- laravel请求到响应的生命周期
请求到响应的核个执行过程,主要可以归纳为四个阶段,即程序启动准备阶段.请求实例化阶段.请求处理阶段.响应发送和程序终止阶段. public\index.php中有这么一段代码 $app = requi ...