Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / Objective C

An Easy to Use Observer Pattern Implementation (No Inheritance Required)

Rate me:
Please Sign up or sign in to vote.
4.95/5 (21 votes)
26 Jan 20047 min read 89.3K   1.3K   41  
Observer Pattern implemented in a nice template model, easy to use as it does not require the classical inheritance and can easily decouple Subject and Observer
#include "StdAfx.h"
#include ".\observer3.h"

#include "Subject1.h"

CObserver3::CObserver3(void)
{
}

CObserver3::~CObserver3(void)
{
}
void CObserver3::StartProcessing	( CDummy *object )
{
	//printf ( "CObserver3::StartProcessing (event from: %s)...\n", object->Name().c_str() );
}
void CObserver3::InitCounter		( CSubject1 *object, int iCounter )
{
	printf ( "CObserver3::InitCounter (event from: %s) %ld...\n", object->Name().c_str(), iCounter );
}
void CObserver3::ProcessingItem		( CSubject1 *object, EventInformation &item, int iCounter )
{
	printf ( "CObserver3::ProcessingItem (event from: %s): %s(%ld) Counter: %ld ...\n", object->Name().c_str(), item.name.c_str(), item.value, iCounter );
}
void CObserver3::Event3				( CSubject1 *object, int iCounter, std::string &str, EventInformation &item )
{
	printf ( "CObserver3::Event3 (event from: %s)...\n", object->Name().c_str() );
}
void CObserver3::Event4				( CSubject1 *object, int iCounter, std::string str, EventInformation &item, const EventInformation *oldItem )
{
	printf ( "CObserver3::Event4 (event from: %s)...\n", object->Name().c_str() );
}
void CObserver3::Event5				( CSubject1 *object, int iCounter, std::string &str, EventInformation &item, const EventInformation *oldItem, int iOldCounter )
{
	printf ( "CObserver3::Event5 (event from: %s)...\n", object->Name().c_str() );
}
void CObserver3::EndProcessing		( CSubject1 *object )
{
	printf ( "CObserver3::EndProcessing (event from: %s)...\n", object->Name().c_str() );
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Technical Lead OneSaas - Cloud Integrations Made Easy
Australia Australia

Comments and Discussions