DesignPattern/24.VisitorPattern/2.Code/Visitor.h

46 lines
703 B
C
Raw Normal View History

2019-11-10 14:59:08 +00:00
#ifndef __VISITOR_H__
#define __VISITOR_H__
#include <iostream>
using namespace std;
2021-10-28 15:15:51 +00:00
// 前向声明
2019-11-10 14:59:08 +00:00
class Element;
class Apple;
class Book;
2021-10-28 15:15:51 +00:00
// 抽象访问者
2019-11-10 14:59:08 +00:00
class Visitor
{
public:
Visitor(){};
virtual ~Visitor(){}
2021-10-28 15:15:51 +00:00
// 声明一组访问方法
2019-11-10 14:59:08 +00:00
virtual void visit(Apple*) = 0;
virtual void visit(Book*) = 0;
};
2021-10-28 15:15:51 +00:00
// 具体访问者:顾客
2019-11-10 14:59:08 +00:00
class Customer :public Visitor
{
public:
Customer();
Customer(string iName);
void setNum(Apple*, int);
void setNum(Book*, int);
void visit(Apple* apple);
void visit(Book* book);
private:
string name;
};
2021-10-28 15:15:51 +00:00
// 具体访问者Cashier
2019-11-10 14:59:08 +00:00
class Cashier :public Visitor
{
public:
Cashier();
void visit(Apple* apple);
void visit(Book* book);
};
#endif