seek better design to have multi-dispatching with java -
in language not supporting multiple dispatching, such java, code
/* example using run time type comparison via java's "instanceof" operator */ interface collideable { void collidewith(collideable other); } class asteroid implements collideable { public void collidewith(collideable other) { if (other instanceof asteroid) { system.out.println("aaaaaa"); } else if (other instanceof spaceship) { system.out.println("bbbbbb"); } else if (other instanceof plane) { system.out.println("ccccccc"); } } } class spaceship implements collideable { public void collidewith(collideable other) { if (other instanceof asteroid) { system.out.println("ddddddd"); } else if (other instanceof spaceship) { system.out.println("eeeeeee"); } else if (other instanceof plane) { system.out.println("fffffff"); } } } class plane implements collideable { public void collidewith(collideable other) { if (other instanceof asteroid) { system.out.println("ggggggg"); }else if (other instanceof spaceship) { system.out.println("hhhhhhh"); }else if (other instanceof plane) { system.out.println("kkkkkk"); } } }
as visitor pattern can solve problem, i'm thinking if should impl each leaf class void collidewith() { visitor.visit(this); // visitor set in setter }
interface visitor { void visit(collideable c); void visit(asteroid c); void visit(spaceship c); void visit(plane c); }
then each unique println should implemented in subclass of visitor below?
class asteroidvisitor implements visitor { void visit(collideable c) {} void visit(asteroid c) { system.out.println("aaaaaa"); } void visit(spaceship c) { system.out.println("bbbbbbb"); } void visit(plane c) { system.out.println("cccccc"); } } //etc spaceshipvisitor , planevisitor()
is best way deal kind of refactoring replace instanceof?
edit: print out example of unique operations in each method, not ultimate result. modify example clear.
in example gave don't need 2 interfaces need collideable
interface. collideable interface can defined follows:
interface collideable { void collidewith(collideable other); string gettype(); }
then spaceship
, plane
, , asteroid
implement collidewith
, gettype
. example implementation, spaceship's implementation this:
class spaceship implements collideable { public void collidewith(collideable other) { system.out.println(this.gettype() + " collides " + other.gettype()); } public string gettype(){ return "spaceship"; } }
you can go 1 step further , declare collideable abstract class, providing implementation of collidewith since same. example:
abstract class collideable { void collidewith(collideable other){ system.out.println(this.gettype() + " collides " + other.gettype()); } string gettype(); }
as side note , general tip, visitor
interface poor design. purpose of interface define methods all classes implementing interface must provide method definition (implementation) of. interface specific: provides several types (spaceship
, asteroid
, etc) method parameters. therefore bad design, why did need interface begin with? doesn't can used anywhere else.
Comments
Post a Comment