0

Im really trying to understand the importance of this interface, but beside helping us to write more quickly, the methods in the concrete classes (by only implementing the methods) I just can't find the need to use it.

The definition is this

an abstraction which declares the accept operation. This is the entry point which enables an object to be "visited" by the visitor object. Each object from a collection should implement this abstraction in order to be able to be visited

." Its clear, but still you can manualy write those accept methods in every single class(which is lot of unnecessary work I agree) but still beside that you can get a class to be visitable, without the IVisitable interface...

//IVisitable.java
package Visitor;

/**
*
* @author dragan
*/
public interface IVisitable {                 
    public void accept (Visitor v); 
}

// Bgirl.java

public class Bgirl implements  IVisitable{

    int _br_godina;

    public Bgirl(int g) {
        br_godina = g;
    }

    public int getBr_godina() {
        return _br_godina;
    }

    public void accept (Visitor v){        
        v.visit(this);            
    }

}

// Main.java

package Visitor;

/**
*
* @author dragan
*/
public class Main {

    public static void main(String[] args) {

        Bgirl terra = new Bgirl(5);  

        System.out.println(terra.getBr_godina());

        VisitorImplement v = new VisitorImplement();              

    }  
}

// VisitorImplement.java

package Visitor;

/**
 *
 * @author dragan
 */
public class VisitorImplement implements Visitor{

    @Override
    public void visit(Bgirl prva) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

      prva._br_godina = 3;

    }

//    @Override
//    public void visit(Bboy prvi) {
//       // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//        System.out.println("BBOY VISIT");
//    
//    }
//    


}
Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
BabaMara
  • 29
  • 2
  • 7
  • please look here - http://stackoverflow.com/a/39174843/4390212 – DimaSan Oct 20 '16 at 13:01
  • 1
    Could you please show us the IVistable interface and the source of the definition? There are two interfaces involved in the visitorpattern that I know, For example at: https://en.wikipedia.org/wiki/Visitor_pattern – Tobias Otto Oct 20 '16 at 13:25
  • so from what i understand, its usefull when you have an array of different object, and you can call the accept method through polymorphism? – BabaMara Oct 20 '16 at 13:25
  • You don´t need an array of different objects for a good example of the visitor pattern. You need two different operations on some different classes which allow visiting / implement IVistable. – Tobias Otto Oct 20 '16 at 13:32
  • what do you mean by that? – BabaMara Oct 20 '16 at 13:53
  • http://www.oodesign.com/visitor-pattern.html source – BabaMara Oct 20 '16 at 13:57
  • 2
    Your question sounds almost as if you're asking why you would want to use interfaces in most design patterns -- what is the benefit of this abstraction. If so, then you'll want to get and read a good book on design patterns such as the Gang of Four book or the Heads Up book. – Hovercraft Full Of Eels Oct 20 '16 at 14:06

1 Answers1

0

Look into your main() method: you can just directly call terra._br_godina = 3 and thus no need to use any visitor.

A visitor is useful when you don't know the concrete type of your terra and even don't know which method should be called to fullfil your wish. All you have is just an abstract type or interface e.g. Girl (or IVisitable). So, to demonstrate the usefulness of Visitor Pattern, your main() method should be like this:

public static void main(String[] args) {
    IVisitable terra = new Bgirl(5);
    // Want to set _br_godina of terra to 3 but do not and cannot know
    // which method should be called
    // Let's create a visitor and let him visit her,
    // he knows how to set _br_godina of her to 3
    VisitorImplement v = new VisitorImplement();
    terra.accept(v); // luckily, every girl accepts the "accept()"
}
Nghia Bui
  • 3,694
  • 14
  • 21