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");
//
// }
//
}