Skip to content

jade-oo: Why I’m done with done()

April 27, 2010

As I mentioned in my previous article, I’m not entirely happy with my Block object. For a start, its name is completely wrong; nothing it does ever blocks the thread of execution. Secondly, I’m not happy that the Block object is responsible for both defining the characteristics of a Block objects, and for constructing their various implementations. Finally, I’ve realised that having a return type for the action() method makes the done() method entirely redundant.

So, what should the type of the object that’s returned by action() be called? A well-named type has a name that clearly and succinctly communicates its responsibility. The object returned by action() will represent one of four states:

  1. The Behaviour has finished forever; remove it from the pool of active Behaviours.
  2. The Behaviour is still active; keep it in the pool of active behaviours and call its action() method as soon as possible.
  3. The Behaviour is waiting for a message to arrive; only call its action() method again once a message has been received.
  4. The Behaviour is waiting for a message to arrive; only call its action() method again once a message has been received or if a maximum waiting time has elapsed.

These are the four states that a Behaviour can be in at the end of its action() method, and so I plan to rename the Block object as BehaviourState. The action() method will then return a BehaviourState object which encapsulates one of those four states:

  1. BehaviourStateFactory.FINISHED
  2. BehaviourStateFactory.ACTIVE
  3. BehaviourStateFactory.WAIT_FOR_MESSAGE;
  4. BehaviourStateFactory.WAIT_FOR_MESSAGE.setMaxWaitTime(long)

Note that each of those is a BehaviourState, but that they are provided by the BehaviourStateFactory, i.e. a separate object is responsible for BehaviourState construction.

Those four states are equivalent, in standard JADE, to:

  1. setting done() to return true.
  2. setting done() to return false.
  3. setting done() to return false, and calling block().
  4. setting done() to return false, and calling block(long).

This covers all logical combinations of both block() and done(), which means that we can not only replace block(), but also do away with done() altogether.


I’m still not 100% happy. I’m not sure that WAIT_FOR_MESSAGE should have a setMaxWaitTime(long) method – I don’t like the fact that the same mechanism is responsible for waiting for messages *and* setting a timeout. Also, I’m not sure that I need objects and a factory; part of me feels that an Enum would suffice. Perhaps this is something that will fall out of the implementation.

One Comment leave one →
  1. April 28, 2010 8:11 pm

    The updated BehaviourState mechanism has been checked in. I ditched the whole factory in the end, and now just construct objects as return types when required. I decided that my reasons for the factory were generally along the lines of premature optimisation, and so it’s better to get the return objects right first, and then worry about optimizing their instantiation later.

    One particularly pleasing consequence of removing the done() method is that I can now remove the CyclicBehaviour (as it was just a Behaviour with the done() method har-coded to return false). Deleting code is pretty much always a good sign (assuming that the code still works afterwards, that is).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: