Recently I’ve required some Observer Patterns to be included in some game development I’m doing at the moment (the game stuff has distracted me away from the RPi stuff, and that’s why the complete RPi AlarmClock code is delayed). I have a few issues with the Observer Pattern in its default guise, so I rolled my own with a few twists and it’s available for you to use also!
My (opinionated) Issues:
- I’m really not a fan of the way that the Observer Pattern implies inheritance being the standard way of implementing it. For languages that don’t support multiple inheritance (e.g. ObjC) you end up making some big changes to your inheritance tree however shallow it may (should) be. Especially awkward if you’re retro-fitting the pattern imo.
- The Observer Pattern also implies it should send out an update: call to any observers – this is rigid and also provides you with no additional information to know how to handle the update. If you’re observing multiple objects of different types you’ll have to spend time figuring out what the object is and what has changed.
What does CFObserver bring to the table?
CFObserver does away with these issues for the most part, in a simple to implement way:
- No Inheritance, just protocols.
- Implementing it can be done in as few as 1 very simple method for the Observer and 1 equally simple method for the Observee (Subject). – Save for a few 1 line protocol declarations.
- It retro fits really well.
- Allows you to have custom update calls to the Observers. You can even pass useful information through these calls rather than a simple vague “update” call.
Where can I get it?