Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between using the visitor pattern and an interface?

What is the difference between applying the visitor design pattern to your code and the following approach:

interface Dointerface {
    public void perform(Object o);
}

public class T {
    private Dointerface d;
    private String s;

    public String getS() {
            return s;
    }

    public T(String s) {
            this.s = s;
    }

    public void setInterface(Dointerface d) {
            this.d = d;
    }

    public void perform() {
            d.perform(this);
    }

    public static void main(String[] args) {
            T t = new T("Geonline");
            t.setInterface(new Dointerface() {
                    public void perform(Object o) {
                            T a = (T)o;
                            System.out.println(a.getS());
                    }
            });
            t.perform();
    }
}

I assume that by using interfaces, we're not really separating the algorithm.

like image 248
Vhaerun Avatar asked Oct 11 '08 18:10

Vhaerun


People also ask

When should you use the visitor pattern?

2 Answers. Save this answer. Show activity on this post. The visitor pattern is useful when you want to process a data structure containing different kinds of objects, and you want to perform a specific operation on each of them, depending on its type.

What are the similarities and differences between strategy and visitor?

Strategy is based on coding to an interface. Visitor is based on coding to an implementation. Strategy implements one operation in multiple ways. Visitor implements multiple operations.

What are the advantages and disadvantages of visitor pattern?

Advantages and Disadvantages of the Visitor Design PatternNew operations are easy to add without changing element classes (add a new concrete visitor). As a result, different concrete elements don't have to implement their part of a particular algorithm. Related behaviour only focuses on a single concrete visitor.

Why do we use facade pattern if we can do the same with interface?

To make it easy to use for users, we can add a facade which wrap the complexity of the task, and provide one simple interface instead. Same goes for the Facade Design Pattern. It hides the complexities of the system and provides an interface to the client from where the client can access the system.


2 Answers

There is quite a big difference.

The visitor pattern uses interfaces, but its purpose is to be able to perform an operation to one or more classes (who implement an interface) without having to change the classes. Hence, the implementation actually "visits" the class and does its thing without the class being modified.

An interface is a basic concept used to provide a common API to a potentially diverse group of classes. The typical test for an interface is that classes that share it are alike in at least that one respect (is-like-a) and in those cases can be treated as such.

Here is a simple example on wikipedia that shows a couple of visitors in java.

like image 196
AdamC Avatar answered Oct 21 '22 06:10

AdamC


Two things:

  • In your example you need two methods. The perfom and the setInterface. With a visitor pattern you would only need one method, the perfom, usually called accept.
  • If you need more than one 'performer', you will have to set the performer -via the setInterface method- for each. This makes it impossible to make your class immutable.
like image 34
Benno Richters Avatar answered Oct 21 '22 08:10

Benno Richters