groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject Fwd: Proposition on a simple implementation of event-driven programming in Groovy.
Date Thu, 23 Apr 2015 09:04:59 GMT
Hi there,

  I have submitted a pull request [1] on github today to propose a simple
implementation for an event feature for Groovy. Here is a short
summerize of what I've written on github:

  The feature is inspired from C#'s Event Handler [2] feature syntax and
aims to be very similar to Qt's signals and slots feature to exchange
messages between objects in an event-driven programming style. This is
an idea I've had while working on an android application written in
Groovy [3].

  The idea is to add an Event class that inherits from Closure and
contains a list of closures from subscribed objects. When the event is
sent, which basically correponds to a closure call, every closure in the
subscribers list is called with the given parameters. This is a typical
implementation of the observer pattern with the ease of Groovy.

  Here is how the system would work :

// This class has an event to which other objects can subscribe
class ObjectWithEvent{
  public Event somethingChanged = new Event()

  // In a random function, something happens and we decide to send the
event with a value
  public void makeSomething(){

// This class will observe the event
// In the constructor, we subscribe to the event by giving it a callback
class ObjectListening{

   private ObjectWithEvent objectWithEvent

   public ObjectListening(){
  objectWithEvent.somethingChanged += this.&onSomethingChanged

  private void onSomethingChanged(int newValue){
  // Code here

C#'s event handlers and Qt's signal/slots are core features of these two
languages. It makess developement very easy when dealing with large
amount of objects that communicate often. Groovy's syntax make it very
easy to implement and I am absolutely willing to work on this.

  What do you think about the idea ?

  _Christophe Henry_


View raw message