Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In a C++ code base I\'ve been working on, they have a bunch of instances of the

ID: 654304 • Letter: I

Question

In a C++ code base I've been working on, they have a bunch of instances of the observer pattern, but it's a little different from the classical pattern. In the classic, the Observer is a specific interface with specific method(s) that are called when the Observable changes or has new data.

Observable                       Observer
register( Observer* )            update( data1, data2 ... )
In our code base, we use a template class that takes the number and type of arguments to the update() method as parameters, and allows you to specify any pointer-to-method as the recipient of the data.

Observable                        
register( Functor<data1, data2> )
Is this an improvement over the original pattern? Here's what I think are the pros of each approach:

Classic:

clarity - the interface marks which classes are recipients of observable data
less code - the boiler plate and ugly template syntax to create the functor are somewhat onerous
Variant:

flexibility - I can make any object in my system receive the update (if it has the right method signature) without having to change the original object
As a another option, do I have it all wrong that this is the Observer pattern in practice?

Explanation / Answer

So the answer is

yes, it is an improvement over the original GOF variant of the pattern

no, it is not better than every other known implementation of the Observer pattern

Also note that what you suggested here is the "standard observer implementation" in functional languages or languages with functional elements where events/event sinks are typically implemented in a comparable way (for example, in C# using delegates).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote