Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the story of the Observer Design Pattern?

I have been investigating about the observer pattern for an assignment. So far, I have come to the conclusion that it first appeared in a book written by a group of people called "The Gang of Four".

But I also read that its first implementation was in a SmallTalk MVC-based framework.

Is there an origin to the observer pattern? Who designed it first? Which of the GoF members did it? Has it suffered any changes since its creation?

Also, some implementations of the pattern include what they call a "ConcreteSubject" which is a generalization of the Subject class. Is this a variation of the pattern, or rather an evolution from the original model?

like image 277
Jota Porras Avatar asked May 17 '13 01:05

Jota Porras


1 Answers

The Gang of Four's main contribution to Design Patterns is really giving names to some commonly-used patterns to assist communication of design intent. It's so much easier to write

// this is an observer

than a big ol' block of comments that no one will read. And if people shared the jargon, developers can communicate more effectively.

The Observer pattern has been around long before OO programming. Most often it was referred to using the term "callback", often implemented with function pointers in various languages, or perhaps even a flag that was used to indicate which function/procedure/subroutine should be called. This represented one of the earliest forms of abstract communication between modules. I've even seen similar approaches taken in assembler languages - storing a callback address and using it to indirectly notify that "something happened".

A big thing to remember... the implementations that the Gang of Four show in the Design Patterns book are not "absolute" - they're there to demonstrate an approach. You can just as easily implement the Observer pattern with a function pointer as you can with an abstract class, interface, or C# delegate.

(I teach a Design Patterns course at Johns Hopkins, btw ;) )

like image 95
Scott Stanchfield Avatar answered Nov 27 '22 22:11

Scott Stanchfield