Leandro Maro
Leandro Maro

Reputation: 343

What is the difference between the Interpreter pattern and Visitor pattern?

I have troubles understanding these two design patterns.

Can you please give me contextual information or an example so I can get a clear idea and be able to map the difference between the two of them.

Thanks.

Upvotes: 1

Views: 2079

Answers (1)

martidis
martidis

Reputation: 2975

The visitor pattern allows you to add functionality to classes without altering them. You keep in a single place/class the same kind of behaviour for different type of objects while (potentially) having different implementation for each type. You can extend or change the behaviour for multiple types of objects while working on a single class (the visitor). Also useful when you want to extend behaviour of classes that are not under your control, without wrapping or extending them.

In visitor the driver of the behaviour is based on behalf of which type of object the operation is performed.

The interpreter pattern can be used on domain problems which can be expressed in simple language/sentences. Then the problems can be solved by interpreting these sentences. So we get an input, we can understand (interpret) it and then implement certain behaviour based on the interpretation/categorization of the input.

In interpreter the driver of the behaviour is based on what the input is, the interpretation/categorization of the input.

Upvotes: 5

Related Questions