oop - Does Java 8 provide an alternative to the visitor pattern? -
this popular answer on stack overflow has difference between functional programming , object-oriented programming:
object-oriented languages when have fixed set of operations on things, , code evolves, add new things. can accomplished adding new classes implement existing methods, , existing classes left alone.
functional languages when have fixed set of things, , code evolves, add new operations on existing things. can accomplished adding new functions compute existing data types, , existing functions left alone.
say have animal
interface:
public interface animal { public void speak(); }
and have dog
, cat
, fish
, , bird
implement interface. if want add new method animal
named jump()
, have go through of subclasses , implement jump()
.
the visitor pattern can alleviate problem, seems new functional features introduced in java 8 should able solve problem in different manner. in scala
use pattern matching, java doesn't have yet.
does java 8 make easier add new operations on existing things?
what you're trying accomplish, while admirable, isn't fit java in cases. before that...
java 8 adds default methods interfaces! can define default methods based on other methods in interface. available abstract classes.
public interface animal { public void speak(); public default void jump() { speak(); system.out.println("...but higher!"); } }
but in end, you're going have provide functionality each type. don't see huge difference between adding new method , creating visitor class or partial functions. it's question of location. want organize code action or object? (functional or object oriented, verb or noun, etc.)
i suppose point i'm trying make java code organized 'noun' reasons aren't changing time soon.
the visitor pattern along static methods best bet organizing things action. however, think visitors make sense when don't depend on exact type of object they're visiting. instance, animal visitor might used make animal speak , jump, because both of things supported animals. jump visitor doesn't make sense me because behavior inherently specific each animal.
java makes true "verb" approach little difficult because chooses overloaded method run based on compile time type of arguments (see below , overloaded method selection based on parameter's real type). methods dynamically dispatched based on type of this
. that's 1 of reasons inheritance preferred method of handling these types of situations.
public class animalactions { public static void jump(animal a) { a.speak(); system.out.println("...but higher!"); } public static void jump(bird b) { ... } public static void jump(cat c) { ... } // ... } // ... animal = new cat(); animalactions.jump(a); // call animalactions.jump(animal) // because type of `a` animal @ // compile time.
you can around using instanceof
, other forms of reflection.
public class animalactions { public static void jump(animal a) { if (a instanceof bird) { bird b = (bird)a; // ... } else if (a instanceof cat) { cat c = (cat)a; // ... } // ... } }
but you're doing work jvm designed you.
animal = new cat(); a.jump(); // jumps cat should
java has few tools make adding methods broad set of classes easier. namely abstract classes , default interface methods. java focused on dispatching methods based on object invoking method. if want write flexible , performant java, think 1 idiom have adopt.
p.s. because i'm that guy™ i'm going bring lisp, common lisp object system (clos). provides multimethods dispatch based on arguments. book practical common lisp provides an example of how differs java.
Comments
Post a Comment