이글루스 로그인


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)
{
System.out.println(n.number);
}

prettyPrint(PlusNode n)
{
prettyPrint(n.n1);
System.out.println(" + ");
prettyPrint(n.n2);
}


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.

Thanks,
Kwang Yul Seo

by 서광열 | 2005/10/06 13:23 | 프로그래밍 | 트랙백 | 덧글(1)

트랙백 주소 : http://skyul.egloos.com/tb/1809927
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]
Commented by 감자 at 2008/05/06 16:03
그럼 교수님의 답변은 무엇인가요??

:         :

:

비공개 덧글


◀ 이전 페이지          다음 페이지 ▶