Double dispatch和Visitor

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:

  1. 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.

  2. 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).

  3. 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.

  4. 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.

  5. 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.

  6. 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.

相关推荐

  1. Double dispatchVisitor

    2024-04-04 23:46:01       34 阅读
  2. Visitor Pattern

    2024-04-04 23:46:01       31 阅读
  3. boost::apply_visitor

    2024-04-04 23:46:01       50 阅读
  4. 访问者模式(Visitor

    2024-04-04 23:46:01       57 阅读
  5. 设计模式--访问者模式(Visitor Pattern)

    2024-04-04 23:46:01       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-04 23:46:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-04 23:46:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-04 23:46:01       82 阅读
  4. Python语言-面向对象

    2024-04-04 23:46:01       91 阅读

热门阅读

  1. 计算机组成原理讲解

    2024-04-04 23:46:01       38 阅读
  2. springboot和redis与jwt实现jwt的token双重拦截

    2024-04-04 23:46:01       41 阅读
  3. 爬虫开发教程及案例

    2024-04-04 23:46:01       42 阅读
  4. 领域驱动设计战术设计

    2024-04-04 23:46:01       52 阅读
  5. Docker运维

    2024-04-04 23:46:01       45 阅读
  6. 【Linux】GCC编译器(七)

    2024-04-04 23:46:01       38 阅读
  7. 蓝桥杯备考随手记: practise04

    2024-04-04 23:46:01       36 阅读
  8. 文心一言 vs GPT-4 -- 全面横向比较

    2024-04-04 23:46:01       37 阅读
  9. Universal_Robots_ROS2_Driver 安装问题详解(humble)

    2024-04-04 23:46:01       34 阅读
  10. webpack 热更新的实现原理

    2024-04-04 23:46:01       35 阅读