groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From christo...@c-henry.fr
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(){
  somethingChanged(newValue)
  }
}

// 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 ?

  Cordialement,
  _Christophe Henry_

Links:
------
[1] 
https://github.com/apache/incubator-groovy/pull/4#issuecomment-94856515
[2] https://msdn.microsoft.com/en-us/library/ms366768.aspx
[3] https://github.com/AugierLe42e/Pheobius

Mime
View raw message