設計模式:28 男人和女人_訪問者模式

來源:互聯網
上載者:User

訪問者模式:表示一個作用於某對象結構中的各元素的操作。它使你可以在不改變各元素的類的前提下定義作用於這些元素的新操作。

 

適用:資料結構相對穩定的系統

資料結構和作用於結構上的操作之間的耦合解脫開,使得操作集合可以相對自由地啞奴啊/

 

目的:把處理從資料結構分離出來/iiao穩定的資料結構和變化的演算法,使用訪問者模式比較合適。因為訪問者模式使得演算法操作的增加變得更加容易。

 

優點:增加新的操作很容易,只需要增加一個新的訪問者。訪問者模式將有關的行為集中到一個訪問者對象中。

 

缺點:增加新的資料結構變得困難。

Element:人類:

ConcreteElementA:男人

ObjectStructure:對象結構類

Visitor:狀態類

 

main.cpp

#include <iostream>#include <stdlib.h>#include "ObjectStructure.h"#include "Action.h"#include "Person.h"using namespace std;void process(){ObjectStructure object;object.attach(std::shared_ptr<Person>(new Man("男人")));object.attach(std::shared_ptr<Person>(new Woman("女人")));//成功時的反應object.display(std::shared_ptr<Action>(new Sucess()));//失敗時的反應object.display(std::shared_ptr<Action>(new Failure()));//戀愛時的反應object.display(std::shared_ptr<Action>(new Amativeness()));}int main(int argc,char* argv[]){process();system("pause");return 0;}


Person.h

#ifndef PERSON_H#define PERSON_H#include <memory>#include <string>class Action;class Person{public:Person(const std::string& sName);virtual ~Person(void);//接受virtual void accept(std::shared_ptr<Action> ptrVisitor);public:std::string _sName;};//男人class Man : public Person{public:Man(const std::string& sName);~Man();void accept(std::shared_ptr<Action> ptrVisitor);};//女人class Woman : public Person{public:Woman(const std::string& sName);~Woman();void accept(std::shared_ptr<Action> ptrVisitor);};#endif


Person.cpp

#include "Person.h"#include "Action.h"Person::Person(const std::string& sName):_sName(sName){}Person::~Person(void){}void Person::accept(std::shared_ptr<Action> ptrVisitor){}Man::Man(const std::string& sName):Person(sName){}Man::~Man(){}void Man::accept(std::shared_ptr<Action> ptrVisitor){ptrVisitor->getManConclusion(this);}Woman::Woman(const std::string& sName):Person(sName){}Woman::~Woman(){}void Woman::accept(std::shared_ptr<Action> ptrVisitor){ptrVisitor->getWomanConclusion(this);}


Action.h

#ifndef ACTION_H#define ACTION_H#include "Person.h"//狀態class Action{public:Action(void);virtual ~Action(void);virtual void getManConclusion(Man* ptrMan);virtual void getWomanConclusion(Woman* ptrWoman);};//成功class Sucess : public Action{public:void getManConclusion(Man* ptrMan);void getWomanConclusion(Woman* ptrWoman);};//失敗class Failure : public Action{public:void getManConclusion(Man* ptrMan);void getWomanConclusion(Woman* ptrWoman);};//戀愛class Amativeness : public Action{public:void getManConclusion(Man* ptrMan);void getWomanConclusion(Woman* ptrWoman);};#endif


Action.cpp

#include "Action.h"#include <iostream>#include "Person.h"using namespace std;Action::Action(void){}Action::~Action(void){}void Action::getManConclusion(Man* ptrMan){}void Action::getWomanConclusion(Woman* ptrWoman){}void Sucess::getManConclusion(Man* ptrMan){cout << ptrMan->_sName << "成功時,背後多半有一個偉大的女人" << endl;}void Sucess::getWomanConclusion(Woman* ptrWoman){cout << ptrWoman->_sName << "成功時,背後大多有一個不成功的男人" << endl;}void Failure::getManConclusion(Man* ptrMan){cout << ptrMan->_sName << "失敗時,借酒澆愁" << endl;}void Failure::getWomanConclusion(Woman* ptrWoman){cout << ptrWoman->_sName << "失敗時,撒嬌" << endl;}void Amativeness::getManConclusion(Man* ptrMan){cout << ptrMan->_sName << "戀愛時,言聽計從" << endl;}void Amativeness::getWomanConclusion(Woman* ptrWoman){cout << ptrWoman->_sName << "戀愛時,智商為0" << endl;}


ObjectStructure.h

#ifndef OBJECTSTRUCTURE_H#define OBJECTSTRUCTURE_H#include <vector>#include <memory>using namespace std;class Person;class Action;class ObjectStructure{public:ObjectStructure(void);~ObjectStructure(void);void attach(std::shared_ptr<Person>& ptrPerson);void display(std::shared_ptr<Action> ptrVisitor);public:vector< std::shared_ptr<Person> > _vecPtrPerson;};#endif


ObjectStructure.cpp

#include "ObjectStructure.h"#include "Person.h"#include "Action.h"ObjectStructure::ObjectStructure(){}ObjectStructure::~ObjectStructure(void){}void ObjectStructure::attach(std::shared_ptr<Person>& ptrPerson){_vecPtrPerson.push_back(ptrPerson);}void ObjectStructure::display(std::shared_ptr<Action> ptrVisitor){for(unsigned ui = 0 ; ui < _vecPtrPerson.size() ; ui++){_vecPtrPerson[ui]->accept(ptrVisitor);}}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.