- A flexible dispatching framework


 

 

This is an example of how, with PolyD, the same client code can be tested using different dispatching policies. The interesting part of the example is at the end of the file.

This is the source code:


Same client, multiple dispathers: Dance.java


import ovm.polyd.policy.*;
import ovm.polyd.tags.*;
import static ovm.polyd.PolyD.build;

class Test{

public interface Dance {
 public void dance (Person x,Place y);
}

@PolyD
@DispatchingPolicy (MultiDisp.class)
public interface DanceMulti extends Dance {}

@PolyD
@DispatchingPolicy (Overloading.class)
public interface DanceOver extends Dance {}

public static class Person{}
public static class Dancer extends Person{}
public static class Place{}
public static class Stage extends Place{}

public static class Impl {
 public void dance(Dancer p, Stage q) {
  printComment("Dance is an expression of art!");
 }
 public void dance(Person p, Stage q) {
  printComment("What is that guy doing on the stage?");
 }
 public void dance(Person p, Place q) {
  printComment("That person is dancing. Strange.");
 }

 void printComment(String s) {
  System.out.println("> "+s);
 }
}

 static void tryMe(Dance d) {
  Person joe     = new Person();
  Place  office  = new Place();
  Person nureyev = new Dancer();
  Place  bolshoi = new Stage();

  System.out.println();

  d.dance(joe,bolshoi);
  d.dance(nureyev,bolshoi);
  d.dance(nureyev,office);

  System.out.println();
 }

 public static void main(String[] s) {
  Impl i=new Impl();

  tryMe(build(DanceMulti.class,i));

  tryMe(build(DanceOver.class,i));

 }
}
 

This is the output:


[toni@acunei polydTest]$ javac -classpath polyd.jar Dance.java 
[toni@acunei polydTest]$ java -classpath ".:polyd.jar:asm-1.5.1.jar" Test

> What is that guy doing on the stage?
> Dance is an expression of art!
> That person is dancing. Strange.


> That person is dancing. Strange.
> That person is dancing. Strange.
> That person is dancing. Strange.