:组合模式: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设计模式之组合模式
组合模式 组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性.掌握组合模式的重点是要理解清楚 “部分/整体” 还有 ”单个对象“ 与 & ...
随机推荐
- MySql安装成功后无法远程登录
MySql安装成功之后在服务器能够登录,但是在客户端却不能登录.只需要按照如下操作即可 请使用mysql管理工具,如:SQLyog Enterprise 可能是你的帐号不允许从远程登陆,只能在loca ...
- 目前用到的一些os.path方法
这里主要记录下os.path.join()的用法 目录结构如下 在readconfig.py中进行试验,如下 1.使用os.path.realpath(__file__)获取文件所在目录 import ...
- 雷林鹏分享:jQuery EasyUI 树形菜单 - 树形网格动态加载
jQuery EasyUI 树形菜单 - 树形网格动态加载 动态加载树形网格有助于从服务器上加载部分的行数据,避免加载大型数据的长时间等待.本教程将向您展示如何创建带有动态加载特性的树形网格(Tree ...
- WCF初见之SQL数据库的增删改查
1.首先要连接数据库,自然要有数据库啦,创建一个数据库表Login,并插入一个数据: --创建数据库表login CREATE TABLE Login ( UName VARCHAR(20) PRIM ...
- linux权限管理之基本权限
基本权限 UGO ======================================================== 文件权限设置: 可以赋于某个用户或组 能够以何种方式 访问某个文件 ...
- Digital Deletions HDU - 1404
Digital deletions is a two-player game. The rule of the game is as following. Begin by writing down ...
- windows下如何通过git bash获取gitlab ssh公钥
Gitlab1. 安装git,从程序目录打开 "Git Bash" 2. 键入命令:ssh-keygen -t rsa -C "email@email.com" ...
- java中String创建对象分析(转)
String str=new String("abc"); 紧接着这段代码之后的往往是这个问题,那就是这行代码究竟创建了几个String对象呢? 相信大家对这道题并不陌生,答案 ...
- oracle的 表、 procedure、package等对象被锁,处理方法
1.0 oracle中表被锁,处理方法 select t4.object_name, t3.spid, t1.oracle_username, t1.os_user_name from v$pro ...
- SQLserver如何创建一个表
例如: create table news(news_id int primary key identity(1,1),news_title varchar(50) not null,news_aut ...