Steve Amerige | 12 Jun 2012 15:25
Picon
Favicon

[groovy-user] Hooks and Callbacks

Hi all,

I'm looking for a recommendation on how to best implement hooks and 
callbacks in groovy.  The basic pattern includes the following:

1. Hooks are keys against which callbacks (closures) can be registered.  
A map is associated with this registration that contains information 
that can be used when callbacks against a hook is executed.

2. In code, callbacks for a given hook can be executed.  Arguments can 
be passed to the hook.execute() call that, in conjunction with the map 
that is associated with callbacks, determines the order in which 
callbacks are executed or even whether a given callback is executed or not.

3. The above is not an event-driven pattern.  It just allows for code 
logic to be more extensible and allows for template-based code design 
where basic functionality can be provided by the template designer and 
augmented by the template user by registering additional callbacks 
against known hooks.

I've fully implemented this in C++ code, but I'm now looking to take 
advantage of whatever Groovy might provide to make this easier.

Any thoughts?

Thanks,
Steve Amerige
SAS Institute, Deployment Developer

---------------------------------------------------------------------
(Continue reading)

Jim White | 12 Jun 2012 17:37
Gravatar

Re: [groovy-user] Hooks and Callbacks

You should check out Expando.  It is a map where you add a method name and closure pairs and it then calls get dispatched using Groovy's usual overloaded methods rules.  



Chaining can be done in lots of different ways and mostly depends on your API, primarily whether the chain call is embedded in each closure or is external.  Something to think about (but perhaps not directly applicable since you're working from an existing design) are aspects.  They provide a very general scheme for doing every kind of (possibly conditional) chaining you can imagine.

Jim

On Tue, Jun 12, 2012 at 6:25 AM, Steve Amerige <Steve.Amerige-LVvIjLR0h5w@public.gmane.org> wrote:
Hi all,

I'm looking for a recommendation on how to best implement hooks and callbacks in groovy.  The basic pattern includes the following:

1. Hooks are keys against which callbacks (closures) can be registered.  A map is associated with this registration that contains information that can be used when callbacks against a hook is executed.

2. In code, callbacks for a given hook can be executed.  Arguments can be passed to the hook.execute() call that, in conjunction with the map that is associated with callbacks, determines the order in which callbacks are executed or even whether a given callback is executed or not.

3. The above is not an event-driven pattern.  It just allows for code logic to be more extensible and allows for template-based code design where basic functionality can be provided by the template designer and augmented by the template user by registering additional callbacks against known hooks.

I've fully implemented this in C++ code, but I'm now looking to take advantage of whatever Groovy might provide to make this easier.

Any thoughts?

Thanks,
Steve Amerige
SAS Institute, Deployment Developer


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email




Gmane