首页 > 其他分享 >Double dispatch和Visitor

Double dispatch和Visitor

时间:2024-04-04 15:31:43浏览次数:23  
标签:const Double dispatch classes Intersect Visitor pattern class

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.

标签:const,Double,dispatch,classes,Intersect,Visitor,pattern,class
From: https://blog.csdn.net/ywjatjd/article/details/137160420

相关文章

  • Wpf Beginstoryboard Storyboard DoubleAnimation Storyboart.TargetName,Storyboary.
    <Windowx:Class="WpfApp32.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • WPF Storyboary DoubleAnimationUsingPath PathGeometry
    <Windowx:Class="WpfApp30.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.......
  • 电脑技巧:推荐一款非常好用的文件重复清理工具DoubleKiller
    目录一、软件介绍二、功能介绍三、使用说明四、软件总结一、软件介绍DoubleKiller是一款专为用户解决重复文件问题而精心打造的小巧实用工具,安装包仅为1.2M。对于长期依赖电脑的工作者和电脑的职场人员来说,随着电脑使用时间的增长,电脑中难免会出现大量重复文件,这些......
  • QT开发:报错:QAxBase: Error calling IDispatch member Open: Exception thrown by serv
    在Qt中打开excel出现下面的错误提示:QAxBase:ErrorcallingIDispatchmemberOpen:Exceptionthrownbyserver怎么解决?错误提示通常意味着在尝试使用Qt的ActiveX模块(QAxBase)打开Excel文件时发生了异常。这可能是由于多种原因引起的,包括文件损坏、权限问题、Excel安装问题或者Q......
  • WPF中动画教程(DoubleAnimation的基本使用)
    实现效果今天以一个交互式小球的例子跟大家分享一下wpf动画中DoubleAnimation的基本使用。该小球会移动到我们鼠标左键或右键点击的地方。该示例的实现效果如下所示:页面设计xaml如下所示:<Windowx:Class="AnimationDemo.MainWindow"xmlns="http://schemas.microsof......
  • codeforces div4 Double Strings
    #include<iostream>#include<algorithm>#include<cstring>#include<map>usingnamespacestd;intT,n;strings[900005];map<string,int>mm;//存放每一个字符串是否出现过intmain(){ cin>>T; while(T--){ mm.clear();//每次清空mm里面的数......
  • 第16期 Double Commander 开源免费的Total Commander替代型【体验100款文件管理工具】
     体验背景:我们正在做一款文件版本管理软件,追光几何(追光几何),期待以最无感的方式,解决新一代工程师文件管理的问题,让大家有更多时间去做快乐和有成就感的事情。所以打算体验100款文件管理软件,来取长补短。真实1h体验DoubleCommander是一款开源的跨平台文件管理软件,灵感来源......
  • abs(int)、fabs(double)函数用法
    1.abs函数语法:#include<stdlib.h>intabs(intnum);功能:函数返回参数num.的绝对值。2.fabs函数语法:#include<math.h>doublefabs(doublearg);功能:函数返回参数arg的绝对值。注意两者的头文件的调用不同,abs作用对象为整数,头文件为#include<cstdlib>,fab......
  • opensips的dispatcher模块笔记
    操作系统:CentOS7.6_x64opensips版本:2.4.9dispatcher模块模块实现了基于目的地址的调度功能,可用作无状态负载均衡,但不能保证均匀分配。今天整理下CentOS7环境下opensips2.4.9的dispatcher模块使用示例,并提供运行效果视频。我将从以下几方面进行展开:模块数据库说明模块参......
  • Double类型数值相加导致精度缺失问题
    问题描述doublev1=13.01;for(inti=0;i<10;i++){v1+=13;System.out.println(v1);}解决方案doublev1=4.5;doublev2=4.55;BigDecimalb1=newBigDecimal(Double.toString(v1));BigDecimalb2=new......