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

Applying Observer Pattern in C++ Applications

Rate me:
Please Sign up or sign in to vote.
4.69/5 (27 votes)
8 Jan 2001 158.4K   2.1K   109  
This article explains how to avoid object dependencies using the Observer Pattern with a simple example.
// CalMon.cpp : implementation of the CCalorieMonitor class
//
#include "stdafx.h"
#include "CalMon.h"
#include <math.h>

/////////////////////////////////////////////////////////////////////////////
// CCalorieMonitor message handlers

BEGIN_MESSAGE_MAP(CCalorieMonitor, CCardioMonitor)
	//{{AFX_MSG_MAP(CCalorieMonitor)
		ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

CCalorieMonitor::CCalorieMonitor( CCardioSubject * pCardioSubject ) : CCardioMonitor( pCardioSubject )
{
	// Step 1 - Get calories per minute and initialize total calories
	m_nCaloriesPerMinute = m_pCardioSubject->GetCaloriesPerMinute();
	m_dbTotalCalories	 = 0.0;
}

CCalorieMonitor::~CCalorieMonitor()
{
}

BOOL CCalorieMonitor::Update( CSubject * pSubject )
{
	// Step 1 - Make sure the right subject updates the time monitor
	if( m_pCardioSubject != pSubject )
	{
		return FALSE;
	}
	// Step 2 - Get calories per minute and calculate total calories
	m_nCaloriesPerMinute = m_pCardioSubject->GetCaloriesPerMinute();
	// Step 3 - Update the window, if the calorie monitor is started
	if( m_bStarted == TRUE )
	{
		m_dbTotalCalories += ( double ) m_nCaloriesPerMinute / 60.00;
		Invalidate();
	}
	return TRUE;
}

BOOL CCalorieMonitor::Create( const RECT & rRect, CWnd * pParentWnd, UINT nID )
{
	// Step 1 - Adjust Rectangle
	CRect WndRect = CRect( rRect.left, rRect.top, rRect.left + 140, rRect.top + 120 );
	// Step 2 - Create window
	return CWnd::Create( NULL, NULL, WS_VISIBLE | WS_BORDER | WS_CHILD | WS_TABSTOP, 
		WndRect, pParentWnd, nID );
}

void CCalorieMonitor::OnPaint() 
{
	CRect		ClientRect;
	CRect		DrawRect;
	CRect		TitleRect;
	CString	csTitle = "Calorie Monitor";
	CString	csText1 = "Calories / Minute";
	CString	csText2 = "Total  Calories";
	CString	csCaloriesPerMinute		= itoa( m_nCaloriesPerMinute, "           ", 10 );
	CString	csTotalCalories			= itoa( ( INT ) floor( m_dbTotalCalories ), "           ", 10 );

	CPaintDC dc(this);

	// Step 1 - Set background color, mode and text color
	GetClientRect( ClientRect );
	dc.FillSolidRect( ClientRect, RGB( 0, 0, 0 ) );
	TitleRect = CRect( ClientRect.left, ClientRect.top, ClientRect.right, ClientRect.top + 30 );
	dc.FillSolidRect( TitleRect, RGB( 0, 128, 128 ) );
	dc.SetBkMode( TRANSPARENT );
	dc.SetTextColor( RGB( 255, 255, 255 ) );

	// Step 2 - Set the text
	DrawRect = CRect( ClientRect.left, ClientRect.top + 5, ClientRect.right, ClientRect.top + 45 );
	dc.DrawText( csTitle, DrawRect, DT_CENTER | DT_WORDBREAK );

	DrawRect = CRect( ClientRect.left + 15, ClientRect.top + 35, ClientRect.left + 80, ClientRect.top + 75 );
	dc.DrawText( csText1, DrawRect, DT_LEFT | DT_WORDBREAK );

	DrawRect = CRect( ClientRect.left + 85, ClientRect.top + 40, ClientRect.left + 135, ClientRect.top + 60 );
	dc.DrawText( csCaloriesPerMinute, DrawRect, DT_LEFT | DT_WORDBREAK );

	DrawRect = CRect( ClientRect.left + 15, ClientRect.top + 75, ClientRect.left + 80, ClientRect.top + 115 );
	dc.DrawText( csText2, DrawRect, DT_LEFT | DT_WORDBREAK );
	
	DrawRect = CRect( ClientRect.left + 85, ClientRect.top + 80, ClientRect.left + 135, ClientRect.top + 105 );
	dc.DrawText( csTotalCalories, DrawRect, DT_LEFT | DT_WORDBREAK );
}

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
Switzerland Switzerland
Kulathu Sarma is working as a Technology Manager for GoldAvenue, a company based in Geneva, Switzerland and responsible for supporting e-business initiatives of GoldAvenue in B2B and B2C Sectors. He has been programming in C/C++ for 9 years and actively working on Patterns for the past 5 years.

Comments and Discussions