[fpc-pascal] Generic Observables

Michal Wallace michal.wallace at gmail.com
Thu May 2 21:26:35 CEST 2013


Hey all,

I was experimenting with using some of FPC's features together and came up
with this:

https://github.com/tangentstorm/lazmvc/blob/master/umodel.pas

Basically, it's just a generic implementation of the Observer design
pattern. It provides a class, TModel, tha you can use as an interface
delegate to make any TObject into a subject. (You can also just subclass
it.)

It also provides GModel<t>, a genericized version of TModel that contains a
property, value : T, which notifies observers when that value is is changed.

The notification is done through message methods, which means that any
TObject can act as an observer and receive callbacks.

The rest of that repository is a Lazarus project that demonstrates how to
use observers to implement the model-view-controller pattern.

The main form provides the view, which is just a little progress bar turned
on its side to act like a volume meter.

Clicking the "settings" button opens the Controller, which in this case is
just a separate form that has a slider control.

Because the main form registers itself as an Observer of the model that the
controller is managing, moving the slider on the controller causes the main
form's View to update in real time.

Anyway, it was kind of fun to make and demonstrates some of the more
interesting features of ObjFPC, so I thought I'd share. :)

You can clone the project through git, or download the whole thing as a zip
file from here:

https://github.com/tangentstorm/lazmvc/

-michal
(aka tangentstorm on #fpc)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130502/b6e9d078/attachment.html>


More information about the fpc-pascal mailing list