×
Namespaces

Variants
Actions

Observer

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: ivey (29 Mar 2007)
Last edited: hamishwillee (27 Jul 2013)


Observer is an object on which a callback function can be invoked to notify it that some event has occurred in the object it is observing. The observed object does not need to know anything about the observer except that the callback function can be invoked on it. This pattern is quite common in Symbian C++. To achieve this, interface classes are used.


Example observer interface

class MMyObserver
{
public:
enum TMyEvent
{
EMyEvent1
,EMyEvent2
};
virtual void HandleMyEventL(TMyEvent aEvent) = 0;
};

To receive events the observer needs to implement this interface:

// header file
class CMyObserver : public CBase,
public MMyObserver
{
//...
private:
virtual void HandleMyEventL(TMyEvent aEvent);
//...
};
// cpp file
void CMyObserver::HandleMyEventL(MMyObserver::TMyEvent aEvent)
{
switch (aEvent)
{
case MMyObserver::EMyEvent1 :
{
//...
}
break;
case MMyObserver::EMyEvent2 :
{
//...
}
break;
default :
break;
}
}

Having done it the observer shall 'subscribe' to notifications by passing 'this'-pointer to the object it wants to observe. For example:

void CMyObserver::ConstructL()
{
//...
iObjectSendingEventsToItsObservers->SetObserver(this);
//...
}

And below is how it is done in the observable object class:

// header file
class CObjectSendingEventsToItsObservers : public CBase
{
public:
//...
void SetObserver(MMyObserver* aObserver);
//...
private:
MMyObserver* iObserver;
};
// cpp file
void CObjectSendingEventsToItsObservers::SetObserver(MMyObserver* aObserver)
{
iObserver = aObserver;
}

Now when an event occurs and this object wants to notify its observer it calls the callback function:

if (iObserver)
{
iObserver->HandleMyEventL(MMyObserver::EMyEventX);
}

It is also possible to store an array of observers if you need to have more than one observer:

// header file
class CObjectSendingEventsToItsObservers : public CBase
{
public:
//...
void SetObserverL(MMyObserver* aObserver);
void RemoveObserver(MMyObserver* aObserver);
//...
 
private:
//...
void NotifyObserversL(MMyObserver::TMyEvent aEvent);
 
private:
RPointerArray<MMyObserver> iObserverArray;
};
// cpp file
void CObjectSendingEventsToItsObservers::SetObserverL(MMyObserver* aObserver)
{
iObserverArray.AppendL(aObserver);
}
 
void CObjectSendingEventsToItsObservers::RemoveObserver(MMyObserver* aObserver)
{
TInt pos = iObserverArray.Find(aObserver);
if (pos >= 0)
{
iObserverArray.Remove(pos);
}
}
 
void CObjectSendingEventsToItsObservers::NotifyObserversL(MMyObserver::TMyEvent aEvent)
{
for (TInt index = 0; index < iObserverArray.Count(); index++)
{
iObserverArray[index]->HandleMyEventL(aEvent);
}
}
 
CObjectSendingEventsToItsObservers::~CObjectSendingEventsToItsObservers()
{
iObserverArray.Close();
}
This page was last modified on 27 July 2013, at 03:37.
62 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×