tags:

views:

51

answers:

2

Consider the following hypothetical people management system. Suppose each Person object belong to a number of Group objects and each Group contains a number of Person objects. We could represent it by adding a list to each Person and each Group object, but then we have to keep this in sync when we create, delete or modify an object. On change callbacks won't work well in this situation as they could lead to an infinite loop.

Now suppose each Group has a name and a description. The name is stored in a dict so that we can find which group uses which name and the description is indexed so that we can search it. These need to be updated whenever a group changes.

The application has a GUI which can display the Person and Group. Whenever a property of the object changes, the GUI needs to update.

Suppose we have to deal with a large number of effects like this. Keeping track of this is rather confusing. I could imagine these using properties, custom collections or maybe even metaclasses. Are there any design patterns/frameworks for dealing with these kind of systems?

+2  A: 

Most ORMs handles this. Just use an in-memory SQLite table, and let it do the hard work.

Ignacio Vazquez-Abrams
+1  A: 

This is quite tricky. Yes the Person and Group data could map to tables in a relational database, but the auto-updating views is more challenging.

Fortunately Qt (and therefore PyQT) has an MVC framework that automatically handles much of what you want. The data storage component can use database tables to store the Person and Group data. You can also create multiple views on the same underlying data model, and configure them to automatically update when the underlying data changes. This uses a Qt feature called Signals, the views subscribe to the model's signals to they get informed of the change.

The book Rapid GUI Programming with Python and QT has examples for very similar cases to this one, see chapters 14 and 15. There also may be other GUI frameworks that offer similar MVC functionality, with views that are automatically informed when the underlying data changes.

Simon Hibbs