• 周三. 12 月 4th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Visitor mode of design mode

King Wang

1 月 16, 2022

Introduce

  • Visitor—— Abstract Visitor
    Abstract class or interface , Declare which elements visitors can access , To be specific to the procedure is visit The parameters of the method define which objects can be accessed .
  • ConcreteVisitor—— Specific visitors
    It affects what visitors do when they visit a class , What are you going to do .
  • Element—— Abstract elements
    Interface or abstract class , State what kind of visitors are allowed to visit , The procedure is through accept Method .
  • ConcreteElement—— Specific elements
    Realization accept Method , Usually visitor.visit(this), Basically, a pattern has been formed .
  • ObjectStruture—— The structure of the object
    The element producer , Generally, it can be accommodated in many different types 、 Containers with different interfaces , Such as List、 Set、 Map etc. , In the project , This character is rarely abstracted

advantage : Comply with the single responsibility principle ; Excellent scalability ; Very flexible

shortcoming : Specific elements to visitors to publish details ; It’s difficult to change specific elements ; It violates the principle of inversion of dependence

application :

  • An object structure contains many class objects , They have different interfaces , And you want to do something with these objects that depends on their specific classes , In other words, it’s a situation that can’t be competent with iterator mode
  • Many different and unrelated operations need to be performed on the objects in an object structure , And you want to avoid having these operations “ Pollution ” Classes for these objects

UML Class diagram :

Simple code :

#ifndef SIMPLE_VISITOR_H
#define SIMPLE_VISITOR_H
#include <iostream>
#include <typeinfo>
#include <list>
using namespace std;
class Visitor;
class Element
{
public:
virtual void accept(Visitor *visitor) = 0;
};
class ConcreteElementA;
class ConcreteElementB;
/**
* @brief The Visitor class
* Visitor class , In the object structure ConcreteElement Each class of declares a Visit operation .
*/
class Visitor
{
public:
virtual void visitConcreteElementA(ConcreteElementA *ceA) = 0;
virtual void visitConcreteElementB(ConcreteElementB *ceB) = 0;
};
/**
* @brief The ConcreteVisitor1 class
* ConcreteVisitor1, Specific visitors , Implemented by each Visitor Declared operation . Each operation is
* A of the present algorithm . part , The algorithm fragment is the class corresponding to the object in the structure .
*/
class ConcreteVisitor1: public Visitor
{
public:
void visitConcreteElementA(ConcreteElementA *ceA) override
{
cout<<"["<<typeid(ceA).name()<<"] By ["<<typeid (this).name()<<"] visit "<<endl;
}
void visitConcreteElementB(ConcreteElementB *ceB) override
{
cout<<"["<<typeid(ceB).name()<<"] By ["<<typeid (this).name()<<"] visit "<<endl;
}
};
/**
* @brief The ConcreteVisitor1 class
* ConcreteVisitor2, Specific visitors , Implemented by each Visitor Declared operation . Each operation is
* Now part of the algorithm , The algorithm fragment is the class corresponding to the object in the structure .
*/
class ConcreteVisitor2: public Visitor
{
public:
void visitConcreteElementA(ConcreteElementA *ceA) override
{
cout<<"["<<typeid(ceA).name()<<"] By ["<<typeid (this).name()<<"] visit "<<endl;
}
void visitConcreteElementB(ConcreteElementB *ceB) override
{
cout<<"["<<typeid(ceB).name()<<"] By ["<<typeid (this).name()<<"] visit "<<endl;
}
};
class ConcreteElementA : public Element
{
public:
void accept(Visitor *visitor) override
{
visitor->visitConcreteElementA(this);
}
void operationA()
{
cout<<"operationA"<<endl;
}
};
class ConcreteElementB: public Element
{
public:
void accept(Visitor *visitor) override
{
visitor->visitConcreteElementB(this);
}
void operationB()
{
cout<<"operationB"<<endl;
}
};
class ObjectStructure
{
public:
ObjectStructure()
{
m_list.clear();
}
void attach(Element *ele)
{
m_list.push_back(ele);
}
void detach(Element *ele)
{
m_list.remove(ele);
}
void accept(Visitor *vis)
{
for (list<Element*>::iterator it=m_list.begin() ; it != m_list.end(); it++) {
(*it)->accept(vis);
}
}
private:
list<Element*> m_list;
};
#endif // SIMPLE_VISITOR_H

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.

  Big talk design pattern No 28 Chapter , Examples of men and women

UML chart :

Code :

#ifndef MAN_WOMEN_VISITOR_H
#define MAN_WOMEN_VISITOR_H
#include <iostream>
#include <typeinfo>
#include <list>
using namespace std;
class Action;
class Men;
class Women;
class Person
{
public:
virtual void accept(Action *act) = 0;
};
class Action
{
public:
virtual void getMenConclusion(Men *men) = 0;
virtual void getWomenConclusion(Women *women) = 0;
};
class Men : public Person
{
public:
void accept(Action *act) override
{
act->getMenConclusion(this);
}
};
class Women : public Person
{
public:
void accept(Action *act) override
{
act->getWomenConclusion(this);
}
};
class Success : public Action
{
public:
void getMenConclusion(Men *men) override
{
cout<<" [ "<<typeid(men).name()<<"] ["<<typeid(this).name()<<" ] when , There's probably a great woman behind her "<<endl;
}
void getWomenConclusion(Women *women) override
{
cout<<" [ "<<typeid(women).name()<<"] ["<<typeid(this).name()<<" ] when , There's an unsuccessful man behind most of them "<<endl;
}
};
class Failing : public Action
{
public:
void getMenConclusion(Men *men) override
{
cout<<" [ "<<typeid(men).name()<<"] ["<<typeid(this).name()<<" ] when , Drinking in a sullen mood , No one needs to persuade ."<<endl;
}
void getWomenConclusion(Women *women) override
{
cout<<" [ "<<typeid(women).name()<<"] ["<<typeid(this).name()<<" ] when , eyes brimming with tears , No one can persuade ."<<endl;
}
};
class Amativeness : public Action
{
public:
void getMenConclusion(Men *men) override
{
cout<<" [ "<<typeid(men).name()<<"] ["<<typeid(this).name()<<" ] when , Pretend to understand everything you don't understand ."<<endl;
}
void getWomenConclusion(Women *women) override
{
cout<<" [ "<<typeid(women).name()<<"] ["<<typeid(this).name()<<" ] when , If you know something, pretend you don't know ."<<endl;
}
};
class ObjectMenWomen
{
public:
ObjectMenWomen()
{
m_list.clear();
}
void attach(Person *ele)
{
m_list.push_back(ele);
}
void detach(Person *ele)
{
m_list.remove(ele);
}
void display(Action *vis)
{
for (list<Person*>::iterator it=m_list.begin() ; it != m_list.end(); it++) {
(*it)->accept(vis);
}
}
private:
list<Person*> m_list;
};
#endif // MAN_WOMEN_VISITOR_H

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.

 

 

 

 

发表回复