add Visitor Pattern

master
FengJungle 2019-11-10 22:59:08 +08:00
parent 3237803068
commit 8f767aa8b8
9 changed files with 305 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

View File

@ -0,0 +1,59 @@
#ifndef __DEMO_H__
#define __DEMO_H__
// 抽象访问者 Visitor
class Visitor
{
public:
virtual void visit(ConcreteElementA*) = 0;
virtual void visit(ConcreteElementB*) = 0;
};
// 具体访问者 ConcreteVisitor
class ConcreteVisitor :public Visitor
{
public:
// 实现一种针对特定元素的访问操作
void visit(ConcreteElementA*){
// 元素A的访问操作代码
}
void visit(ConcreteElementB*){
// 元素B的访问操作代码
}
};
// 抽象元素
class Element
{
public:
// 声明抽象方法,以一个抽象访问者的指针作为函数参数
virtual void accept(Visitor*) = 0;
};
// 具体元素
class ConcreteElement :public Element
{
public:
void accept(Visitor* visitor){
visitor->visit(this);
}
};
// 对象结构
class ObjectStructure
{
public:
// 提供接口接受访问者访问
void accept(Visitor* visitor){
// 遍历访问对象结构中的元素
for (){
elementList[i]->accept(visitor);
}
}
void addElement(){}
void removeElement(){}
private:
lsit<Element*>elementList;
};
#endif

View File

@ -0,0 +1,34 @@
#include "Element.h"
/***** Apple *******/
Apple::Apple(){
setPrice(0);
setNum(0);
setName("");
}
Apple::Apple(string name, int price){
setPrice(price);
setNum(0);
setName(name);
}
void Apple::accept(Visitor* visitor){
visitor->visit(this);
}
/***** Book *******/
Book::Book(){
setPrice(0);
setNum(0);
setName("");
}
Book::Book(string iName, int iPrice){
setPrice(iPrice);
setNum(0);
setName(iName);
}
void Book::accept(Visitor* visitor){
visitor->visit(this);
}

View File

@ -0,0 +1,56 @@
#ifndef __ELEMENT_H__
#define __ELEMENT_H__
#include "Visitor.h"
#include <iostream>
using namespace std;
// 抽象元素
class Element
{
public:
Element(){};
virtual void accept(Visitor*) = 0;
void setPrice(int iPrice){
this->price = iPrice;
}
int getPrice(){
return this->price;
}
void setNum(int iNum){
this->num = iNum;
}
int getNum(){
return num;
}
void setName(string iName){
this->name = iName;
}
string getName(){
return this->name;
}
private:
int price;
int num;
string name;
};
// 具体元素Apple
class Apple :public Element
{
public:
Apple();
Apple(string name, int price);
void accept(Visitor*);
};
// 具体元素Book
class Book :public Element
{
public:
Book();
Book(string name, int price);
void accept(Visitor*);
};
#endif

View File

@ -0,0 +1,25 @@
#ifndef __SHOPPINGCART_H__
#define __SHOPPINGCART_H__
#include "Element.h"
#include "Visitor.h"
#include <vector>
class ShoppingCart
{
public:
ShoppingCart(){}
void addElement(Element* element){
printf(" 商品名:%s, \t数量:%d, \t加入购物车成功!\n", element->getName().c_str(), element->getNum());
elementList.push_back(element);
}
void accept(Visitor* visitor){
for (int i = 0; i < elementList.size(); i++){
elementList[i]->accept(visitor);
}
}
private:
vector<Element*>elementList;
};
#endif

View File

@ -0,0 +1,45 @@
#ifndef __VISITOR_H__
#define __VISITOR_H__
#include <iostream>
using namespace std;
// 前向声明
class Element;
class Apple;
class Book;
// 抽象访问者
class Visitor
{
public:
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;
};
// 具体访问者:收银员
class Cashier :public Visitor
{
public:
Cashier();
void visit(Apple* apple);
void visit(Book* book);
};
#endif

View File

@ -0,0 +1,36 @@
#include "Element.h"
#include "Visitor.h"
#include "ShoppingCart.h"
#include <Windows.h>
int main()
{
Apple *apple1 = new Apple("븐말却틥벎", 7);
Apple *apple2 = new Apple("빻큇틥벎", 5);
Book *book1 = new Book("븐짜촘", 129);
Book *book2 = new Book("老써諒", 49);
Cashier* cashier = new Cashier();
Customer* jungle = new Customer("Jungle");
jungle->setNum(apple1, 2);
jungle->setNum(apple2, 4);
jungle->setNum(book1, 1);
jungle->setNum(book2, 3);
ShoppingCart* shoppingCart = new ShoppingCart();
shoppingCart->addElement(apple1);
shoppingCart->addElement(apple2);
shoppingCart->addElement(book1);
shoppingCart->addElement(book2);
printf("\n\n");
shoppingCart->accept(jungle);
printf("\n\n");
shoppingCart->accept(cashier);
printf("\n\n");
system("pause");
return 0;
}

View File

@ -0,0 +1,50 @@
#include "Visitor.h"
#include "Element.h"
/***** Customer *******/
Customer::Customer(){
this->name = "";
}
Customer::Customer(string iName){
this->name = iName;
}
void Customer::setNum(Apple* apple, int iNum){
apple->setNum(iNum);
}
void Customer::setNum(Book* book, int iNum){
book->setNum(iNum);
}
void Customer::visit(Apple* apple){
int price = apple->getPrice();
printf(" %s \t单价: \t%d 元/kg\n", apple->getName().c_str(), apple->getPrice());
}
void Customer::visit(Book* book){
int price = book->getPrice();
string name = book->getName();
printf(" 《%s》\t单价: \t%d 元/本\n", book->getName().c_str(), book->getPrice());
}
/***** Cashier *******/
Cashier::Cashier(){
}
void Cashier::visit(Apple* apple){
string name = apple->getName();
int price = apple->getPrice();
int num = apple->getNum();
int total = price*num;
printf(" %s 总价: %d 元\n", name.c_str(), total);
}
void Cashier::visit(Book* book){
int price = book->getPrice();
string name = book->getName();
int num = book->getNum();
int total = price*num;
printf(" 《%s》 总价: %d 元\n", name.c_str(), total);
}