Wednesday, March 25, 2009

Inputs and Outputs 3: subscriptions

There was a major to-do item from our last code post (9-11-08): how does the input find out about the value of the output? We know from the "push or pull?" post that the outputs will push their value to the inputs -- but how? Whenever an output changes it needs to know who to tell. The input that wants this information needs to tell the output: as soon as you change, tell me. This relationship (as implied by the title of the post) is that of a subscription service (outputs) and subscribers (inputs).

An output needs to provide a way to:
  • add a new subscriber
  • unsubscribe a current subscriber
  • notify subscribers
  • make its state known
An input needs to:
  • be updated when a change occurs
In the programming world, this is not a new problem. There is a time-honored solution to this called the "observer pattern." The pattern describes Subjects and Observers which correlate exactly to our Outputs and Inputs. This problem is so common that in .NET they built in a way to do most of this work for you: .NET, "Events" will give you all of this. Since we're doing this in C# anyway, I'm opting for events. Events have added benefits which we may also take advantage of later.

CODE: http://downloads.softhardsoft.com/2009-03-25.zip

0 comments: