For the first time I implement, and something did not work.
public interface FieldVisitor { Object visit(Field field); } And these are methods from implementation:
private FieldVisitor factory = new FieldVisitor() { public Object visit(PresetField presetField) { .... } public Object visit(StringField stringField) { .... } @Override public Object visit(Field field) { System.out.println(field.getClass()); .... } }; I call this:
public Node getFieldFor(Field field) { return (Node) factory.visit(field); } It can be seen that objects of different classes are coming:
class com.dma.params.model.field.PresetField class com.dma.params.model.field.PresetField class com.dma.params.model.field.NumberField class com.dma.params.model.field.NumberField class com.dma.params.model.field.StringField class com.dma.params.model.field.BooleanField However, as I see, I only get to the method for the superclass. In general, as I understand what type of pointer, such a function is called. How can I change this behavior? That function for actual type was caused or I somewhere tupanul with a pattern?
visit(Field field)method, this means that polymorphism will be only for the parameter of theFieldtype, and thevisit(PresetField), visit(StringField)are completely independent, the overload is not override - markovfactory.visit(field), wherefieldis an object of theFieldclass, respectively, and thevisit(Field field)method is called. And I don’t see a superclass at all. - post_zeew