diff --git a/24.VisitorPattern/1.Picture/访问者模式UML图.png b/24.VisitorPattern/1.Picture/访问者模式UML图.png new file mode 100644 index 0000000..10f34af Binary files /dev/null and b/24.VisitorPattern/1.Picture/访问者模式UML图.png differ diff --git a/24.VisitorPattern/1.Picture/访问者模式实例UML图.png b/24.VisitorPattern/1.Picture/访问者模式实例UML图.png new file mode 100644 index 0000000..b09fd90 Binary files /dev/null and b/24.VisitorPattern/1.Picture/访问者模式实例UML图.png differ diff --git a/24.VisitorPattern/2.Code/Demo.h b/24.VisitorPattern/2.Code/Demo.h new file mode 100644 index 0000000..39ba6e3 --- /dev/null +++ b/24.VisitorPattern/2.Code/Demo.h @@ -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: + lsitelementList; +}; + +#endif \ No newline at end of file diff --git a/24.VisitorPattern/2.Code/Element.cpp b/24.VisitorPattern/2.Code/Element.cpp new file mode 100644 index 0000000..d04a31e --- /dev/null +++ b/24.VisitorPattern/2.Code/Element.cpp @@ -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); +} \ No newline at end of file diff --git a/24.VisitorPattern/2.Code/Element.h b/24.VisitorPattern/2.Code/Element.h new file mode 100644 index 0000000..9f17d97 --- /dev/null +++ b/24.VisitorPattern/2.Code/Element.h @@ -0,0 +1,56 @@ +#ifndef __ELEMENT_H__ +#define __ELEMENT_H__ + +#include "Visitor.h" +#include +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 \ No newline at end of file diff --git a/24.VisitorPattern/2.Code/ShoppingCart.h b/24.VisitorPattern/2.Code/ShoppingCart.h new file mode 100644 index 0000000..9bcaab3 --- /dev/null +++ b/24.VisitorPattern/2.Code/ShoppingCart.h @@ -0,0 +1,25 @@ +#ifndef __SHOPPINGCART_H__ +#define __SHOPPINGCART_H__ + +#include "Element.h" +#include "Visitor.h" +#include + +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: + vectorelementList; +}; + +#endif \ No newline at end of file diff --git a/24.VisitorPattern/2.Code/Visitor.h b/24.VisitorPattern/2.Code/Visitor.h new file mode 100644 index 0000000..a9c5fe7 --- /dev/null +++ b/24.VisitorPattern/2.Code/Visitor.h @@ -0,0 +1,45 @@ +#ifndef __VISITOR_H__ +#define __VISITOR_H__ + +#include +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 \ No newline at end of file diff --git a/24.VisitorPattern/2.Code/main.cpp b/24.VisitorPattern/2.Code/main.cpp new file mode 100644 index 0000000..c0abea7 --- /dev/null +++ b/24.VisitorPattern/2.Code/main.cpp @@ -0,0 +1,36 @@ +#include "Element.h" +#include "Visitor.h" +#include "ShoppingCart.h" +#include + +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; +} \ No newline at end of file diff --git a/24.VisitorPattern/2.Code/visitor.cpp b/24.VisitorPattern/2.Code/visitor.cpp new file mode 100644 index 0000000..332f4ae --- /dev/null +++ b/24.VisitorPattern/2.Code/visitor.cpp @@ -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); +} \ No newline at end of file