Design pattern: 28 Men and women _ Visitor mode

Source: Internet
Author: User
Tags data structures

Visitor Pattern: Represents an operation that acts on elements in an object's structure. It allows you to define new actions that act on these elements without changing the class of each element.

Application: A system with relatively stable data structure

The coupling between the data structure and the operations acting on the structure is freed, allowing the set of operations to be relatively free of dumb slaves.

Objective: To/iiao a stable data structure and change algorithm, and to use the visitor pattern is appropriate. Because the visitor pattern makes it easier to increase the algorithm's operation.

Pros: It's easy to add new operations, just add a new visitor. The visitor pattern concentrates the behavior on a visitor object.

Cons: Adding new data structures becomes difficult.

Element: Human:

Concreteelementa: Man

Objectstructure: Object Structure class

Visitor: Status Class

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 ("Woman"));

	Successful response
	Object.display (std::shared_ptr<action> (New sucess ()));
	Failure Response
	Object.display (std::shared_ptr<action> (New Failure ()));
	The reaction object.display in Love
	(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);
	Accept
	virtual void accept (std::shared_ptr<action> ptrvisitor);
Public:
	std::string _sname;
};

Men
Class Man:public person
{public
: Man
	(const std::string& sName);
	~man ();
	void Accept (std::shared_ptr<action> ptrvisitor);
};

Woman
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


::P erson (const std::string& sName): _sname (sName)
{
}


Person::~person (void)
{
}

void Person::accept (std::shared_ptr<action> ptrvisitor)
{
}

Man::man (const std::string& sName):P Erson (sName)
{
}

Man::~man ()
{
}

void Man::accept (std::shared_ptr<action> ptrvisitor)
{
	ptrvisitor->getmanconclusion (this);
}

Woman::woman (const std::string& sName):P Erson (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"

//Status
class ACTION
{public
:
	Action (void);
	Virtual ~action (void);
	virtual void getmanconclusion (man* ptrman);
	virtual void getwomanconclusion (woman* ptrwoman);

Success
class Sucess:public Action
{public
:
	void Getmanconclusion (man* ptrman);
	void Getwomanconclusion (woman* ptrwoman);
};

Failed
class Failure:public Action
{public
:
	void Getmanconclusion (man* ptrman);
	void Getwomanconclusion (woman* ptrwoman);
};

Love
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::getwo Manconclusion (woman* ptrwoman) {} void Sucess::getmanconclusion (man* ptrman) {cout << ptrman->_sname &LT;&L T
"When it comes to success, there's probably a great woman behind it" << Endl; } void Sucess::getwomanconclusion (woman* ptrwoman) {cout << ptrwoman->_sname << "When successful, most of them have an unsuccessful man behind them" <
< Endl;

} void Failure::getmanconclusion (man* ptrman) {cout << ptrman->_sname << "alcohol when failed" << Endl;} 

void Failure::getwomanconclusion (woman* ptrwoman) {cout << ptrwoman->_sname << "when failed, spoiled" << Endl;} 

void Amativeness::getmanconclusion (man* ptrman) {cout << ptrman->_sname << "When in Love" << Endl;} void Amativeness::getwomanconclusion (woman* ptrwoman) {cout << ptrwoman->_sname << "In Love, IQ is 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::d isplay (std::shared_ptr<action> ptrvisitor)
{for
	(unsigned UI = 0; UI < _ Vecptrperson.size (); ui++)
	{
		_vecptrperson[ui]->accept (ptrvisitor);
	}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.