Objective C: CFObserver

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 in my opinion. 
  • The Observer Pattern also implies it should send out an update: call to any observers – this is unnecessarily quite 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.There are times when this is no hardship as you may want to completely refresh your own model of the object’s attributes, but this is not always the case.

Features of CFObserver
CFObserver does away with the above 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?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s