-
Notifications
You must be signed in to change notification settings - Fork 0
/
visitorPattern.cpp
66 lines (59 loc) · 1.19 KB
/
visitorPattern.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// visitorPattern.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<string>
#include<stdio.h>
#include<iostream>
class element{
virtual void accept(class visitor &v) = 0;
};
class bread : public element{
public:
void accept(visitor &v);
std::string breadfunc(){return "bread";}
};
class onion : public element{
public:
void accept ( visitor &v);
std::string onionfunc(){ return "onion";}
};
class visitor{
public: virtual void visit( bread *e) = 0;
virtual void visit( onion *e) = 0;
};
void bread::accept( visitor &v){
v.visit(this);
}
void onion::accept( visitor &v){
v.visit(this);
}
class eatvisitor : public visitor{
void visit( bread * e){
std::cout<<"\n eat "<<e->breadfunc();
}
void visit( onion * e){
std::cout<<"\n eat "<<e->onionfunc();
}
};
class beatvisitor : public visitor{
void visit( bread * e){
std::cout<<"\n beat "<<e->breadfunc();
}
void visit( onion * e){
std::cout<<"\n beat "<<e->onionfunc();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
eatvisitor e;
beatvisitor b;
bread *x = new bread();
onion *y = new onion();
x->accept(e);
y->accept(b);
x->accept(b);
y->accept(e);
int stopx;
std::cin>>stopx;
return 0;
}