#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的更多相关文章

  1. JAVA 设计模式 组合模式

    用途 组合模式 (Component) 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有唯一性. 组合模式是一种结构型模式. 结构

  2. Java设计模式——组合模式

    JAVA 设计模式 组合模式 用途 组合模式 (Component) 将对象组合成树形结构以表示“部分-整体”的层次结构.组合模式使得用户对单个对象和组合对象的使用具有唯一性. 组合模式是一种结构型模 ...

  3. ComponentPattern (组合模式)

    import java.util.LinkedList; /** * 组合模式 * * @author TMAC-J 主要用于树状结构,用于部分和整体区别无区别的场景 想象一下,假设有一批连锁的理发店 ...

  4. 设计模式(十)组合模式(Composite Pattern)

    一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象 ...

  5. 设计模式--组合模式Composite(结构型)

    一.概念 组合模式允许你将对象组合成树形结构来表现"整体/部分"层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 二.UML图 1.Component(对象接口),定义 ...

  6. 组合模式/composite模式/对象结构型模式

    组合模式/composite模式/对象结构型 意图 将对象组合成树形结构以表示"整体--部分"的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 动机 C ...

  7. c#设计模式-组合模式

    在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象和复合对象 ...

  8. C#设计模式系列:组合模式(Composite)

    1.组合模式简介 1.1>.定义 组合模式主要用来处理一类具有“容器特征”的对象——即它们在充当对象的同时,又可以作为容器包含其他多个对象. 1.2>.使用频率 中高 2.组合模式结构图 ...

  9. php实现设计模式之 组合模式

    <?php /** * 组合模式 * * 将对象组合成树形结构以表示"部分-整体"的层次结构,使得客户对单个对象和复合对象的使用具有一致性 * * * 1) 抽象构件角色Co ...

  10. java设计模式之组合模式

    组合模式 组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性.掌握组合模式的重点是要理解清楚 “部分/整体” 还有 ”单个对象“ 与 & ...

随机推荐

  1. JS 日期比较方法

    1.日期参数格式:yyyy-mm-dd // a: 日期a, b: 日期b, flag: 返回的结果 function duibi(a, b,flag) { var arr = a.split(&qu ...

  2. SVN图标各种标注

    黄色感叹号(有冲突):--这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许你提交,防止你的提交覆盖了别人 ...

  3. 卡方分布 | t检验 | F检验 | 卡方检验 | 假设检验 | 各种检验持续总结

    Chi-square distribution introduction 这个视频真的好,完美地解释了卡方统计量是怎么来的! 我们有一个标准正态分布的总体,我们从其中抽一次,取该值的平方就是Q1统计量 ...

  4. 20165327 2017-2018-2 《Java程序设计》第一周学习总结

    第1章 Java入门 一.Java 的特点 简单 面向对象 平台无关 多线程:允许同时完成多个任务 动态:Java程序的基本组成单元就是类(有些类是自己编写的,有一些是从类库中引入的,而类又是运行时动 ...

  5. 最长上升子序列 nlogn

    ; LL num[N]; LL dp[N]; LL go(LL l, LL r, LL k) { for (; r >= l; r--) if (dp[r] <= k) return r; ...

  6. PHP引用赋值

    <?php/** * 在PHP 中引用的意思是用不同的名字访问同一个变量内容 * 只有有名字的变量才可以引用赋值,否则会报错 * 引用赋值 不是在内存上同体,只是把各自的值关联起来 * unse ...

  7. JS onclick事件获取空间value

    1. HTML onclick = btnAction(this.value) 2. JS btnAction(v){ alert(v) }

  8. New Roads CodeForces - 746G (树,构造)

    大意:构造n结点树, 高度$i$的结点有$a_i$个, 且叶子有k个. 先确定主链, 然后贪心放其余节点. #include <iostream> #include <algorit ...

  9. Centos7 JDK安装过程中 解决java -version 报错: bash: /home/jdk1.8.0_161/bin/java: Permission denied

    1.执行Linux命令 -----vim /etc/profile  编辑profile  文件,在里面添加: #set java enviroment JAVA_HOME=/opt/JavaHome ...

  10. atom - Emmet插件使用,代码快速填写

    参考转载:http://www.hangge.com/blog/cache/detail_1537.html 用法: 输入:ul>li*6    接着按:tab键 常用语法: 1.后代>: ...