Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-05

Generic Observables

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Michal Wallace

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)

Reply by email

Michael Van Canneyt
to Michal Wallace
Michal Wallace wrote 6 lines
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.)

You are aware that the classes unit contains the observer pattern, and together with the fpmediators it has all you need to implement the MVC model (through mediators) ?

I must admit though, that the use of message methods is very interesting.

Michael.

Reply by email

Michal Wallace
to Michael Van Canneyt
Michael Van Canneyt wrote
You are aware that the classes unit contains the observer pattern, and together with the fpmediators it has all you need to implement the MVC model (through mediators) ?

Nope. :) I actually had thought I'd seen something about observers in the docs before, but I couldn't find it.

Anyway, this wasn't really meant to be production code. More of an exploration/demonstration of using things like generics, interfaces, and messages together.

I'll add a link to the classes/fpmediators docs in the readme though. Thanks!!

Reply by email

Graeme Geldenhuys
to Michal Wallace: I was experimenting with using some of FPC's features together and came up with this:…
Michal Wallace wrote 5 lines
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

Just curious. If you use interfaces already, then why use message methods for notifications? Why not simply stick to "designing to a interface" theory, and use the interface support you already have?

You might also find Model-GUI-Mediator (MGM) interesting. I wrote an article about it some years back. You can get a copy of the artile with sources here:

http://opensoft.homeip.net/articles/

tiOPF (a Object Persistence Framework) has a much more advanced Model-GUI-Mediator implementation that currently works with VCL, LCL, fpGUI and FireMonkey. See the tiBaseMediator, tiModelMediator, tiMediators and tiListMediators units, or simply compile Demo 21 (available for VCL, LCL or fpGUI), which is a simple Address Book demo using only MGM to build and manage the whole UI.

http://www.tiopf.com

Also an interesting read - especially if you are into Interface usage and model design. Some years back Joanna Carter (well known in the Delphi circles) wrote several articles building a basic Model-View-Presenter (MVP) framework, using interfaces extensively. This has taught me so much about using interfaces.

http://www.carterconsulting.org.uk/MVP.htm

Anyway, thanks for sharing your code. It is nice seeing others also interested in better data and UI modelling - compare to the crappy db-ware design Borland forced onto the world.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Reply by email