Package sim.engine

Class MethodStep

java.lang.Object
sim.engine.MethodStep
All Implemented Interfaces:
Serializable, Steppable

public class MethodStep extends Object implements Steppable
A Steppable which calls an underlying method using Java's reflection system. The underlying method can either have no arguments or have one argumen (SimState), and is specified by its name as a String.

You can use MethodStep to call methods on classes that aren't Steppables. For example, if you have an object myObject and wish to submit both its foo and bar methods (no arguments) to be called at various times when the Schedule sees fit, you might do this:


    MyClass myObject = ... ;
    schedule.scheduleRepeating( ... , new MethodStep(myObject, "foo"), ...);
    schedule.scheduleRepeating( ... , new MethodStep(myObject, "bar"), ...);
    

This will call the foo() method and bar() method to be called at the appropriate times on myObject.

You can also use MethodStep to pop up objects which aren't Steppables by their nature. For example:


    JFrame myJFrame = ... ;
    schedule.schedule( ... , new MethodStep(myJFrame, "show"), ...);
    

MethodStep can also be called on methods which expect to be passed in the Steppable's SimState argument. When the MethodStep is called, it passes its argument to them. You do this by adding a true to the constructor:


    MyClass myObject = ... ;
    schedule.scheduleRepeating( ... , new MethodStep(myObject, "baz", true), ...);
    schedule.scheduleRepeating( ... , new MethodStep(myObject, "quux", true), ...);
    

This will call the baz(SimState) method and quux(SimState) method to be called at the appropriate times on myObject.

This method is mostly to help in ease of porting: but it's not good Java. Reflection is slow and violates all sorts of design contracts -- generally speaking, you should use anonymous Steppables intead. For example, the examples above could have instead been written this way:


    final MyClass myObject = ... ;
    final JFrame myJFrame = ... ;
    schedule.scheduleRepeating( ... , new Steppable() { public void step(SimState state) { myObject.foo() } }, ...);
    schedule.scheduleRepeating( ... , new Steppable() { public void step(SimState state) { myObject.foo() } }, ...);
    schedule.schedule( ... , new Steppable() { public void step(SimState state) { myJFrame.show() } }, ...);
    schedule.scheduleRepeating( ... , new Steppable() { public void step(SimState state) { myObject.baz(state) } }, ...);
    schedule.scheduleRepeating( ... , new Steppable() { public void step(SimState state) { myObject.quux(state) } }, ...);
    
See Also:
  • Constructor Details

    • MethodStep

      public MethodStep(Object object, String methodName)
    • MethodStep

      public MethodStep(Object object, String methodName, boolean passInSimState)
  • Method Details