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

46 lines
703 B
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#ifndef __VISITOR_H__
#define __VISITOR_H__
#include <iostream>
using namespace std;
// 前向声明
class Element;
class Apple;
class Book;
// 抽象访问者
class Visitor
{
public:
Visitor(){};
virtual ~Visitor(){}
// 声明一组访问方法
virtual void visit(Apple*) = 0;
virtual void visit(Book*) = 0;
};
// 具体访问者:顾客
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;
};
// 具体访问者Cashier
class Cashier :public Visitor
{
public:
Cashier();
void visit(Apple* apple);
void visit(Book* book);
};
#endif