Click here to Skip to main content
11,642,402 members (61,204 online)
Click here to Skip to main content
Add your own
alternative version

Observing the world - how to build a reuseable implementation of a design pattern

, 11 Jun 2001 82.4K 316 84
An article about the techniques to pour reusable design (a design pattern) into reusable code with an example of the observer pattern.
// Observer_DemoDlg.cpp : implementation file

#include "stdafx.h"
#include "Observer_Demo.h"
#include "Observer_DemoDlg.h"

#include "weather_forecast.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

// For simplicity the objects of our weather forecasting system are declared as globals
// and initialized in OnInitDialog()

// Subjects
Strategy	g_Strategy;		// The global forecast strategy
TempSens	g_TempSens;		// a temperature sensor

// Observers
HurrDet		g_HurrDet;		// Monitors Strategy and TempSens
Technician	g_Technician;	// Monitors TempSens for physical state events

// CMainDlg dialog

CMainDlg::CMainDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMainDlg::IDD, pParent)
	m_fTemperature = 0.0;
	m_fThreshold = 0.0;
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

void CMainDlg::DoDataExchange(CDataExchange* pDX)
	DDX_Control(pDX, IDE_OUTPUT, m_ctrlOutput);
	DDX_Text(pDX, IDE_TEMP, m_fTemperature);
	DDX_Text(pDX, IDE_THRESH, m_fThreshold);


// CMainDlg message handlers

BOOL CMainDlg::OnInitDialog()

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	// Set our edit control as output for any text logging
	theApp.m_OutLogger.SetEditCtrl( m_ctrlOutput.m_hWnd );

	// Add Technician to the list of TempSens observers
	g_Technician.SetTempSensor( &g_TempSens );

	// Add HurrDet to the list of TempSens and Strategys observers
	g_HurrDet.SetTempSensor( &g_TempSens, m_fThreshold );
	g_HurrDet.SetStrategy( &g_Strategy );	
	return TRUE;  // return TRUE  unless you set the focus to a control

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMainDlg::OnPaint() 
	if (IsIconic())
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMainDlg::OnQueryDragIcon()
	return (HCURSOR) m_hIcon;

void CMainDlg::OnApplyTemp() 
	g_TempSens.SetTemp( m_fTemperature );

void CMainDlg::OnApplyTresh() 
	g_HurrDet.SetTempSensor( &g_TempSens, m_fThreshold );

void CMainDlg::OnNewstrategy() 

void CMainDlg::OnOtherEvent() 
	g_TempSens.TechEvent( TempSens::eOtherFailure );

void CMainDlg::OnPowerEvent() 
	g_TempSens.TechEvent( TempSens::ePowerFailure );

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.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Daniel Lohmann
Germany Germany
Daniel Lohmann ( is Assistant Professor at the Distributed Systems and Operating Systems department at Friedrich-Alexander-University Erlangen-Nuremberg, Germany. His main research topic is the design of a highly customizable and scalable operating system product line for deeply embedded systems using static configuration and aspect-oriented techniques. Before joining Universität Erlangen he worked as a freelance trainer and consultant for NT system programming, advanced C++ programming and OOA/OOD. He is interested in upcoming programming techniques like aspect-oriented programming, generative programming and C++ meta coding and has written some nice and handy tools for Windows NT which you can download at his web site.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 12 Jun 2001
Article Copyright 2001 by Daniel Lohmann
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid