今日完成代码200
时间5h
学习内容:看了看阅读数目《软工》,写了阅读笔记。写了大数据的hbase作业,写了软件构造的作业调用接口给图片加特效
写了高飞作业2
实验 25:访问者模式
[实验任务一]:打包员
在我们课堂上的“购物车”的例子中,增加一个新的访问者:打包员,负责对购物车中货物装包。
实验要求:
#include <iostream>
#include <string>
#include <list>
using namespace std;
class Product;
class Book;
class Apple;
class Visitor
{
protected:
string name;
public:
void SetName(string name) {
this->name = name;
}
virtual void Visit(Apple* apple) {};
virtual void Visit(Book* book) {};
};
class Product
{
public:
// Methods
virtual void Accept(Visitor* visitor) {};
};
// "ConcreteElement"
class Apple : public Product {
public:
void Accept(Visitor* visitor) {
visitor->Visit(this);
}
};
class Book :public Product {
public:
void Accept(Visitor* visitor) {
visitor->Visit(this);
}
};
class Customer : public Visitor {
public:
void Visit(Book* book) {
cout << "顾客" << name << "买书" << endl;
}
void Visit(Apple* apple) {
cout << "顾客" << name << "选苹果" << endl;
}
};
class Saler : public Visitor {
public:
void Visit(Book* book) {
cout << "收银员" << name << "直接计算书的价格" << endl;
}
void Visit(Apple* apple) {
cout << "收银员" << name << "给苹果过秤,然后计算其价格" << endl;
}
};
class Packer : public Visitor {
public:
void Visit(Book* book) {
cout << "打包员" << name << "给书打包" << endl;
}
void Visit(Apple* apple) {
cout << "打包员" << name << "给苹果打包" << endl;
}
};
// "ObjectStructure"
class BuyBasket
{
private:
list<Product*> products;
public:
void Attach(Product* product)
{
products.push_back(product);
}
void Detach(Product* product)
{
products.remove(product);
}
void Accept(Visitor* visitor)
{
for (std::list<Product*>::iterator it = products.begin(); it != products.end(); ++it)
(*it)->Accept(visitor);
}
};
int main()
{
BuyBasket* buybasket = new BuyBasket();
Product* apple = new Apple();
Product* book = new Book();
buybasket->Attach(apple);
buybasket->Attach(book);
Customer* customer = new Customer();
customer->SetName("张三");
Saler* saler = new Saler();
saler->SetName("李四");
Packer* packer = new Packer();
packer->SetName("王五");
// Employees are visited
cout << "----customer----" << endl;
buybasket->Accept(customer);
cout << "----saler----" << endl;
buybasket->Accept(saler);
cout << "----packer----" << endl;
buybasket->Accept(packer);
}
标签:总结,cout,Visitor,void,Visit,public,12.1,每日,class From: https://www.cnblogs.com/zhaoshengfu/p/17870910.html