Visitor Pattern and Multimethods 프로그래밍

여기 교수님에게 쓴 메일! 비지터 패턴이라는게 결국 멀티메소드가 없는 언어에서 이를 흉내내기 위해서 만든 것이며, 따라서 멀티메소드가 있는 언어보다 다소 복잡성을 요구하게 된 거 아니냐는 내용이다. 그리고 멀티메소드의 장점에도 불구하고 자바나 C++같은 일반적인 프로그래밍 언어에 결여된 이유가 무엇인지 묻고 있다.

Dear Pugh,

I have a question on Visitor Pattern. You said the Visitor pattern is also called double dispatch. It allows us to select method based on run-time type of two arguments. (selected based on node type & visitor type).

I have a strong feeling that the Visitor pattern is kind of a workaround to overcome the lack of language feature multiple dispatch (aka multimethods). Java supports only single dispatch, but we do need to select method based on RUN-TIME TYPE of TWO objects.

For example, if we assume that a Java-like language supports multimethods, the same example which you used in the lecture can be written quite easily.

class Node {

class NumberNode extends Node {
int number;

class PlusNode extends Node {
Node n1, n2;

void prettyPrint(Node n)

prettyPrint(NumberNode n)

prettyPrint(PlusNode n)
System.out.println(" + ");

For me, the Visitor pattern seems to be a trick to introduce multiple dispatch in a language that lacks it. I think it is more natural to implement with multimethods.

I think the Visitor pattern has some inherent problems because it is not natural enough. At first it seems quite confusing to find interaction between visit and accept methods. Moreover, whenever the return type or arguments of visitor changes, you have to add a new Visitor class and accept methods. I know you can reduce this problem using generics in Java 5.0.

I asked you in the class why Java haven't implemented multimethods. You told me, "There exists some implementation burden and multimethod itself is not a common language feature among many programmmers." But, it seems to me that the multimethod is a right way to go because it gives us a power to remove some RTTIs (like a bunch of instanceof in Java programs).

I wonder whether what I am thinkg is correct or not. And I can't find why the language feature multimethod should be omitted in polular programming languages.

Kwang Yul Seo


댓글 입력 영역



서씨의 프로필입니다.