Difference betwen Visitor pattern & Double Dispatch

前端 未结 5 1641
逝去的感伤
逝去的感伤 2020-11-30 00:05

I am reading about visitor pattern, and it appears the same like Double Dispatch. Is there any difference between the two. Do the two terms means the same thing.

re

5条回答
  •  有刺的猬
    2020-11-30 00:10

    Dynamic Dispatch refers to the concept of dispatching to a method based on runtime information, in general. Most OO systems (as in Java/C#/C++) usually implement dynamic dispatch via virtual methods (whether or not all methods are virtual depend from the language); this restricts them to dispatch according to a single method argument (the implicit object reference).

    In general you could want to dispatch according to an arbitrary number of elements. Double Dispatch, for example, is the requirement/ability to dispatch according to two arguments of the method.

    On the other hand, the Visitor Pattern is an implementation of Multi Dispatch in general and thus Double Dispatch in particular in such OO systems.

提交回复
热议问题