473,695 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generalized "observer" pattern


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)

I am trying to distill the "fundamenta l" hard problems in software
development, especially regarding C++ and I've seen too many issues
relating to the observer pattern I think it is elevated to "fundamenta l".

By fundamental issues I mean, "avoiding deadlock conditions", object
lifetime management etc.

There is no specific question really, just an invitiation for thoughts
on the topic.
Sep 23 '07 #1
4 4671

Gianni Mariani wrote:
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.
Aside, and probably OT, I've written a little generic class that
performs
RAII subscription/unsubsription. I attempted to make it definition
independent, handling either attach/detach, subscribe/unsubscribe or
add/remove as potential interface. It was merely a nice exercise for
SFINAE (and credit to some posts in c.l.c++, as well as boost
archives).

CAVEAT: Does not use weak pointer yet as described in my other
mail. I just at the time of writing this have not thought of that
problem
yet.

Here goes:

namespace AviGen {
namespace GenSubPriv{

typedef char (&subscribe)[0];
typedef char (&add)[1];
typedef char (&attach)[2];
template < typename T, void (T::*)() >
struct ptmf_helper {};

template< typename T subscribe get_method_lite ral( ... ); //
Subscribe
template< typename T add
get_method_lite ral( ptmf_helper<T,& T::add>* );
template< typename T attach
get_method_lite ral( ptmf_helper<T,& T::attach>* );

template <class PubT, class SubT, std::size_t>
class GenSubBase;

// Used subscribe/unsubscribe to subscribe.
template <class PubT, class SubT>
class GenSubBase<PubT , SubT, sizeof(subscrib e): public SubT
{
protected:
GenSubBase( PubT& publisher ): publisher_( publisher )
{ publisher_.subs cribe( *this ); }
virtual ~GenSubBase()
{ publisher_.unsu bscribe( *this ); }
private:
PubT* publisher_;
};

// Used add/remove to subscribe.
template <class PubT, class SubT>
class GenSubBase<PubT , SubT, sizeof(add): public SubT
{
protected:
GenSubBase( PubT& publisher ): publisher_( publisher )
{ publisher_.add( *this ); }
virtual ~GenSubBase()
{ publisher_.remo ve( *this ); }
private:
PubT* publisher_;
};

// Used attach/detach to subscribe.
template <class PubT, class SubT>
class GenSubBase<PubT , SubT, sizeof(attach): public SubT
{
protected:
GenSubBase( PubT& publisher ): publisher_( publisher )
{ publisher_.atta ch( *this ); }
virtual ~GenSubBase()
{ publisher_.deta ch( *this ); }
private:
PubT* publisher_;
};

}//end namespace GenSubPriv

//
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~//
// Author : werasmus
// Date : Sep 22, 2007 7:41:39 PM
// Description:
// This class provides the ability to subscribe/unsubscribe in a RAII
fashion.
// Currently it caters for the publisher to outlive the subscriber,
hence the
// publisher is accepted as reference in the constructor.
// Note:
// Currently works for Publishers with interface <add/remove>, <attach/
detachand
// <subscribe/unsubscribe>.
// TODO:
// We could modify the publisher member iaw. a policy at later stage,
the default
// probably being weak pointer.
//
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~//
template <class PubT, class SubT>
class GenSubscriber
: public GenSubPriv::Gen SubBase<PubT,
SubT,sizeof(Gen SubPriv::get_me thod_literal<Pu bT>(0))>
{
protected:
typedef typename GenSubPriv::Gen SubBase<PubT,
SubT,sizeof(Gen SubPriv::get_me thod_literal<Pu bT>(0))Base;
//_____________ operations _____________//
GenSubscriber( PubT& publisher ): Base( publisher ){ }
};

Sep 24 '07 #2
On Sep 24, 12:17 am, Gianni Mariani <gi3nos...@mari ani.wswrote:
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.
I don't know if there is anything really formal, but I'd tend to
go with the description in the GoF book, if only because it
seems to be the most widespread.
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)
I'm not sure, but "Twin" sounds like you're managing a
one-to-one relationship. In my experience, it's almost always a
1->n relationship: each Observable will have many Observer.
I am trying to distill the "fundamenta l" hard problems in
software development, especially regarding C++ and I've seen
too many issues relating to the observer pattern I think it is
elevated to "fundamenta l".
By fundamental issues I mean, "avoiding deadlock conditions",
object lifetime management etc.
And persistency?

You're not the first. Back in the early 1990's, there was a lot
of discussion of "relationsh ip management", with the hope of
finding some nice generic solution. As far as I know, there
were no real useful results, but the issue still seems to be
considered important---Java's hyping "container-managed
relations" in EJB.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 24 '07 #3
On 2007-09-24 00:17, Gianni Mariani wrote:
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.
I would assume that it is the one given in the GoF book, if you do not
have it the wikipedia article is more or less a copy from the book,
especially the "Participan t classes" section.
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)

I am trying to distill the "fundamenta l" hard problems in software
development, especially regarding C++ and I've seen too many issues
relating to the observer pattern I think it is elevated to "fundamenta l".
I do not see how there can any issues with the observer pattern, it is
very simple, however I perhaps not very useful either. I prefer a more
advanced version (I am sure this is some pattern but I do not know its
name) where the observing class is not only informed that something has
happened, but also what. So instead of just having a notify() function
the observing class has a onFoo(Bar& sender, FooArgs arguments) function
that tells the observing class what happened (the arguments) and to whom
(the sender). Anyone know the name of this pattern?

--
Erik Wikström
Sep 24 '07 #4

Erik Wikström wrote:

not see how there can any issues with the observer pattern, it is
very simple, however I perhaps not very useful either. I prefer a more
advanced version (I am sure this is some pattern but I do not know its
name) where the observing class is not only informed that something has
happened, but also what. So instead of just having a notify() function
the observing class has a onFoo(Bar& sender, FooArgs arguments) function
that tells the observing class what happened (the arguments) and to whom
(the sender). Anyone know the name of this pattern?
Aspect observer, perhaps? Notify( aspect) or Notify( state ). But
our observers only tend to observer a very concise aspect. One
could register for notification wrt. a particular concern or aspect.

Register: Observer. // where the observer has interface GetAspect.

Sep 24 '07 #5

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

Similar topics

53
4573
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document, and at the end there is even pure mindstorming text left in, but already I think it can be very useful. <url: http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01__alpha.doc.pdf>.
10
11113
by: cppdev | last post by:
Hi All! I want to clear the string contents from sensitive information such as passwords, and etc. It's always a case that password will appear as string at some point or another. And i feel uneasy leaving it hanging in memory indefinitely (especially in case when string is Interned). So at leats for the case when string is not interned i propose:
3
1589
by: Allan Ebdrup | last post by:
Is it possible to listen to when a constructor is called or when a property is set on a object, without having to implement changes in the observed objects class, in dotNet 2.0? It would be very effective for implementing a Model View Control architecture. Perhaps some stuff could be generated runtime using reflection? Kind Regards, Allan Ebdrup
3
1900
by: craig | last post by:
Given two existing but different classes OldA and OldB (that can not be made to derive from any new base class); is there a way to make them both "observer" objects so that they can be put in one central list and updated thru a common interface. (i.e. observer->update( ..))? Potential solution 1 (multiple inheritence): make a small new observer class, and two new classes: NewA: derived from OldA, and Observer,.. and NewB: derived from...
28
2256
by: fred.haab | last post by:
Not having server side scripting, I've been doing this for "last modified" tags on my pages: <div class="modified"> <script type="Text/JavaScript"> <!-- document.write("This page was last modified: " + document.lastModified); --> </script>
37
3946
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as a function
94
30299
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock Statement (C# Reference) statement to serialize access. " But when is it better to use "volatile" instead of "lock" ?
3
1578
by: Tom | last post by:
Can someone please explain the non-working aspect of binding to Width? See code below. Thanks !! -- Tom =================================================== using System; using System.Collections.Generic;
4
9854
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in this context, in fact some articles from pre-2.0 suggest using any collection class of your choice. So my questions focus more on the syntax of event properties provided by the "event" keyword in C#. (Disclaimer - I am a C++ programmer working...
0
8635
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8574
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,...
1
8850
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6493
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
5839
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
4342
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4580
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1977
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.