Double dispatch and visitor
Dispatch
运行时多态,通过基类指针查找具体派生类的方法。
Single dispatch
单派发
示例:
Base* p = new Derived();
p->Func();
Double dispatch
派发、分发、分派,可以类比:总机-分机
两次dispatch
经常发生在使用vector保存同一类层级的指针
示例:
class Shape
{
public:
virtual void Intersect(const Shape& s) const= 0;
virtual void Intersect(const Circle& s) const= 0;
virtual void Intersect(const Triangle& s) const= 0;
};
class Circle: public Shape()
{
public:
void Intersect(const Shape& s) const overload {s.Intersect(*this);}
void Intersect(const Circle& s) const overload {cout << "circle intersect with circle";}
void Intersect(const Triangle& s) const overload {cout << "circle intersect with tirangle";}
};
class Triangle: publice Shape()
{
public:
void Intersect(const Shape& s) const overload {s.Intersect(*this);}
void Intersect(const Circle& s) const overload {cout << "triangle intersect with circle";}
void Intersect(const Triangle& s) const overload {cout << "triangle interset with triangle";}
};
void Func(const Shape& s1, const Shape& s2)
{
return s1.Intersect(s2); // double dispatch
}
void main()
{
Circle c;
Triangle t;
std::vector<std::pair<Shape* s1, Shape* s2>> pairs {{&c, &c}, {&c, &t}, {&t, &c}, {&t, &t}};
for(auto p: pairs)
{
p.first->Intersect(p.second);
}
}
Visitor pattern(待补充)
一种设计模式,用于解决double dispatch相关的问题
Some reference
What you described is a pattern known as the Visitor design pattern. This pattern is used in object-oriented programming to define a new operation without changing the classes of the elements on which it operates.
Here’s a detailed breakdown of what you described:
-
Double Dispatch: This refers to the technique of dispatching a function call to different concrete functions depending on the runtime types of two objects involved in the call.
-
Class Hierarchy: This refers to a structure in object-oriented programming where classes are organized in a hierarchical manner, with each class inheriting attributes and behaviors from its parent (superclass).
-
Operations: In the context of the Visitor pattern, operations refer to the new functionality that needs to be added to a class hierarchy without altering the classes themselves.
-
Visitors: In the Visitor pattern, visitors are a set of related operations that are implemented in separate classes. These operations can then be applied to elements of a class hierarchy without changing the classes themselves.
-
accept() Function: This is a virtual function defined in the base class of the node hierarchy. It takes a reference to a Visitor and is implemented differently in each concrete node class to allow the visitor to operate on the node.
-
Hierarchy of Nodes: This refers to a structure where the classes representing language constructs or AST nodes are organized in a hierarchical manner, with each node class representing a specific language construct or AST node.
In summary, the Visitor pattern allows you to define new operations on a class hierarchy without modifying the classes themselves by separating the new behaviors into separate Visitor classes and using the double dispatch mechanism to select the appropriate operation for each node in the hierarchy.
标签:const,Double,dispatch,classes,Intersect,Visitor,pattern,class From: https://blog.csdn.net/ywjatjd/article/details/137160420