473,761 Members | 8,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Observer Design Pattern

Hello All!

I am trying to implement my own Design Patterns Library.
I have read the following documentation about Observer Pattern:
1) Design Patterns by GoF
Classic description of Observer. Also describes implementation
via ChangeManager (Mediator + Singleton)
2) Pattern hatching by John Vlissides
Describes Observer's implementation via Visitor Design Pattern.
3) Design Patterns Explained by Alan Shalloway and James Trott
Describes Observer's implementation via Adaptor Design Pattern.
4) CUJ's article : "Prying eyes : A Policy-Based Observer I,II" by
Andrei Alexandrescu
Very interesting articles, which describes attempt to implement
a policy-based Observer and several serious problems:
a) Mutual recursion (potential infinite loops, etc...)
b) Active observers problem (iteration problem)
c) Notification order problem
d) GetState()'s return value problem

I am trying to combine best aspects of each approach.

Consider the following pseudo-code example:

class Subject
{
public:
virtual void Attach(Observer *) = 0;
virtual void Detach(Observer *) = 0;
virtual void Notify() = 0;

virtual @@@ GetState() = 0; // !!! What type should be
returned???
};

class Observer
{
public:
virtual void Update(Subject* ) = 0;
};

class MySubject : public Subject
{
public:
void Attach(Observer *)
{
/// Add pair
}
void Detach(Observer *)
{
/// Delete pair
}
void Notify()
{
/// Call observer->Update(this) for each observer in mapping
}

@@@ GetState()
{
/// Return MySubject-specific data. BUT HOW???
}
private:
// Subject-Observer mapping
// MySubject-specific data
};

class YourSubject : public Subject
{
public:
// Along similar lines
private:
// Subject-Observer mapping
// YourSubject-specific data
};

class MyObserver : public Observer /// This class represents MySubject
and YourSubject
{
public:
void Update(Subject* s)
{
/// Only Subject's interface is available here (not MySubject
or YourSubject)
/// I know one bad solution - type switch via dynamic_cast.
if TypeOf(s) == MySubject
{
/// Update MySubject-specific part of representation
}
else if TypeOf(s) == YourSubject
{
/// Update YourSubject-specific part of representation
}
}
private:
/// MyObserver-specific data (e.g. GUI widgets for subjects
representation)
};
The problem lies in poor Subject's interface.
Subject doesn't know anything about MySubject's private data.

I don't understand why GoF includes GetState method in Subject's
interface.
Type of GetState()'s return value depends up concrete subclass of
Subject, i.e.
class ConcreteSubject : Subject
{
public:
ReturnValue<Con creteSubject> GetState();
...
};

Main question:
How to avoid type switching in ConcreteObserve r::Update(Subje ct* s)?

Thanks!

P.S.
Sorry for my english :)

May 31 '06
22 4739
Diego Martins wrote:

I was thinking the "boaz" was an ENGRISH troll with his opinion already
formed (ill-formed) about GoF patterns

but now, I see the "boaz" was using the GoF patterns like strict APIs


No, I think you were right the first time.

Jun 1 '06 #21

Diego Martins wrote:
Daniel T. wrote:
bo*******@yahoo .com wrote:
Thomas J. Gritzan wrote:
> Thomas J. Gritzan schrieb:
> > bo*******@yahoo .com schrieb:
> >>Next their Template and Algorithm patterns - do you really think that
> >>we need those?
> >
> >
> > The Template design pattern is used in the STL all over the place:
> >
> > std::sort, std::copy, ...
>
> Err, not std::copy.
>
> Used in:
>
> std::sort, std::for_each, std::find_if, std::transform and the like...

Then eigher you don't know how those algorithms are implemented, or you
don't know the algorithm design pattern from the GoF book - take you
pick :)


He got the latter wrong. sort, for_each, find_if, transform, &c. are
all examples of the Strategy pattern.
There is NO correlation between the STL and design pattern GoF.


Either you don't understand design patterns or you don't know the STL,
take your choice. :-) Actaully, I don't think you do get to choose. You
look at the implementations in the Design Patterns book and assume that
they are saying that those are the only way to implement them and
that's just plain wrong. That's why I told the OP that implementing a
"Design Pattern Library" can't be done in general. There are too many
different ways to implement the patterns discussed.

Even with a pattern as simple as Observer, I showed two completely
different ways to implement (one using runtime polymorphism and a pull
approach, one using tempates and a push approach.)

The Iterator pattern is fundimental to STL and as I show above,
Strategy is used all over the place as well.

Based on one of your other posts, you don't seem to think that STL uses
Iterator. You need to broaden your mind and look again.

GoF Iterator C++ Iterator
First() it = container.begin ()
Next() operator++()
IsDone() it == container.end()
CurrentItem() *it


very good post :)

now, all is clear to me about this thread

I was thinking the "boaz" was an ENGRISH troll with his opinion already
formed (ill-formed) about GoF patterns

but now, I see the "boaz" was using the GoF patterns like strict APIs

design patterns are ideas. annotated and discussed ideas.

honest :)

If we are getting personal - I was thinking that "Diego Martins" stand
for a fool in english and now I know that I'm right.
If other care to know tough why STL and GoF have nothing in common
though then this was taken from inverew with Spetanov the creator of
the STL -
" I think STL and Generic Programming mark a definite departure from
the common C++ programming style, which I find is almost completely
derived from SmallTalk. Do you agree?

Answer:
Yes. STL is not object oriented. I think that object orientedness is
almost as much of a hoax as Artificial Intelligence. I have yet to see
an interesting piece of code that comes from these OO people. In a
sense, I am unfair to AI: I learned a lot of stuff from the MIT AI Lab
crowd, they have done some really fundamental work: Bill Gosper's
Hakmem is one of the best things for a programmer to read. AI might not
have had a serious foundation, but it produced Gosper and Stallman
(Emacs), Moses (Macsyma) and Sussman (Scheme, together with Guy
Steele). I find OOP technically unsound. It attempts to decompose the
world in terms of interfaces that vary on a single type. To deal with
the real problems you need multisorted algebras - families of
interfaces that span multiple types. I find OOP philosophically
unsound. It claims that everything is an object. Even if it is true it
is not very interesting - saying that everything is an object is saying
nothing at all. I find OOP methodologicall y wrong. It starts with
classes. It is as if mathematicians would start with axioms. You do not
start with axioms - you start with proofs. Only when you have found a
bunch of related proofs, can you come up with axioms. You end with
axioms. The same thing is true in programming: you have to start with
interesting algorithms. Only when you understand them well, can you
come up with an interface that will let them work."
Since the name of the GoF book is - the full name - "Design Patterns:
Elements of Reusable Object-Oriented Software" I don't see how those
two can coexists (in trem of the philosy behind them).
More specifucly unlike this Diego Martins I know what API is and I know
that their book is not something that you just copy and past from. But
I simly think that Spetanov is right in what he is saying about OOP and
other things
More spesificly - the fact that STL iterator like somewhat like what
GoF iterator dont mean its the same. If you look for interface similary
then maybe you think of the design pattern as kind "API" but the basic
consept of STL is contanier, algorithems and iterators that combine the
2 formers together. the true anlog of iterators in STL is a pointer
plain and simple, actualy vector iterator is a pointer, no class not
OOP simple and clean cocept. This is simply something I failed to find
in the GoF book. But then again if someone think that he/she found the
light there then use this book I realy don't care as long as I don't
have to deal with his/her code
Lets hope that it will never become so personal again..

Jun 1 '06 #22
On 1 Jun 2006 12:13:30 -0700, I waved a wand and this message magically
appeared from bo*******@yahoo .com:
Lets hope that it will never become so personal again..


In other words, you sucked at maths.
--
http://www.munted.org.uk

Take a nap, it saves lives.
Jun 1 '06 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7046
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
3
2629
by: Michael Schneider | last post by:
Hello All, I am comming back to python after being away for several years. I would like to use weak refs in an observer pattern implementation. The problme that I have seems to be that weakrefs can't manage functions. ------------------- from docs: http://www.python.org/doc/current/lib/module-weakref.html
4
1938
by: decrypted | last post by:
Since I couldn't find a OO design/architext forum, I thought I would post here... I have a win app with forms management. forms are grouped by category (pertains to company, pertains to project. etc). This is represented, for example, by a company window containing additional windows such as company information, document history, yada yada. I am using the Observer/Observable pattern to refresh all the forms in a group when another form...
0
1624
by: FluffyCat | last post by:
Last week I continued my series of design patterns examples using PHP 5. Here now is my 14th example, the Observer pattern. http://www.fluffycat.com/PHP-Design-Patterns-Observer/ In the Observer Pattern, a Subject object notifies an Observer object if it's the Subject object's state changes. Pretty simple and fairly useful.
0
1097
by: schoenfeld1 | last post by:
I have the actor/observer pattern implemented in my application. I have added a remoted object as an observer to an actor object. In other words, a proxy object (returned by the Activator.GetObject()) is a listener to some actor object which unpredictably calls some method from proxy object's interface. The remoted object is a long-living object. The question is whether this is reliable or not? Since the logical validity of the...
4
4680
by: Gianni Mariani | last post by:
Two issues here: a) What is the accepted definition of "observer pattern". While I can't point to anything specific, I do remember having issues with inconsistency in the definition. b) Generic observer design in C++. I have been pushing the Austria "Twin" interface for a while and more recently the multi threaded version of it "TwinMT" (see the 6129 alpha on sourceforge)
1
1875
by: Christopher | last post by:
The observer pattern itself is easy enough. I've implemented it using a Event that contains data for any Event type I forsee my application using. My problem is I don't want one and only one general purpose type of Event. I want to write my design in such a way that more Event Types can be created and used down the road as needed. How can you design the Events in such a way that more can be handled later by the same Observer and Subject...
4
2117
by: Mohamed Mansour | last post by:
Hello, What is the purpose of implementing the Observer Pattern if we can trigger an event easily? For example (from books), You have a "Forecaster" which notifies "Observable" when a prediction is ready, then there is a "WeatherViewer" which calls methods from the "Observer Interface".
5
1482
by: Alan Isaac | last post by:
I have two questions about the "observer pattern" in Python. This is question #1. (I'll put the other is a separate post.) Here is a standard example of the observer pattern in Python:
0
9376
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9988
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8813
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.