Click here to Skip to main content
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
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__;
#endif



// 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)
{
	//{{AFX_DATA_INIT(CMainDlg)
	m_fTemperature = 0.0;
	m_fThreshold = 0.0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMainDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMainDlg)
	DDX_Control(pDX, IDE_OUTPUT, m_ctrlOutput);
	DDX_Text(pDX, IDE_TEMP, m_fTemperature);
	DDX_Text(pDX, IDE_THRESH, m_fThreshold);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMainDlg, CDialog)
	//{{AFX_MSG_MAP(CMainDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDB_APPLY_TEMP, OnApplyTemp)
	ON_BN_CLICKED(IDB_APPLY_TRESH, OnApplyTresh)
	ON_BN_CLICKED(IDB_NEWSTRATEGY, OnNewstrategy)
	ON_BN_CLICKED(IDB_OTHER, OnOtherEvent)
	ON_BN_CLICKED(IDB_POWER, OnPowerEvent)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainDlg message handlers

BOOL CMainDlg::OnInitDialog()
{
	CDialog::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;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// 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() 
{
	UpdateData();
	g_TempSens.SetTemp( m_fTemperature );
}

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

void CMainDlg::OnNewstrategy() 
{
	g_Strategy.StrategyChanged();
}

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.

License

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

Share

About the Author

Daniel Lohmann

Germany Germany
Daniel Lohmann (daniel@losoft.de) 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.

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