原文地址:http://blog.youkuaiyun.com/feixiaoxing/article/details/7188574
不知不觉当中,我们就到了最后一种设计模式,即访问者模式。访问者模式,听上去复杂一些。但是,这种模式用简单的一句话说,就是不同的人对不同的事物有不同的感觉。比如说吧,豆腐可以做成麻辣豆腐,也可以做成臭豆腐。可是,不同的地方的人未必都喜欢这两种豆腐。四川的朋友可能更喜欢辣豆腐,江浙的人就可能对臭豆腐更喜欢一些。那么,这种情况应该怎么用设计模式表达呢?
- typedef struct _Tofu
- {
- int type;
- void (*eat) (struct _Visitor* pVisitor, struct _Tofu* pTofu);
- }Tofu;
- typedef struct _Visitor
- {
- int region;
- void (*process)(struct _Tofu* pTofu, struct _Visitor* pVisitor);
- }Visitor;
- void eat(struct _Visitor* pVisitor, struct _Tofu* pTofu)
- {
- assert(NULL != pVisitor && NULL != pTofu);
- pVisitor->process(pTofu, pVisitor);
- }
- void process(struct _Tofu* pTofu, struct _Visitor* pVisitor)
- {
- assert(NULL != pTofu && NULL != pVisitor);
- if(pTofu->type == SPICY_FOOD && pVisitor->region == WEST ||
- pTofu->type == STRONG_SMELL_FOOD && pVisitor->region == EAST)
- {
- printf("I like this food!\n");
- return;
- }
- printf("I hate this food!\n");
- }
本文通过一个生动的例子介绍了访问者设计模式。以不同地区人们对不同豆腐口味的偏好为例,阐述了如何通过访问者模式来实现这一过程。文中给出了具体的C++代码实现。
392

被折叠的 条评论
为什么被折叠?



