Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Implementing Observer, Strategy, and Decorator Design Patterns on a Temperature Model

, 27 Feb 2006
This article demonstrates the use of the Observer, Strategy, and Decorator patterns in C#.
// ****************************************************************
// *		     		MVC Example V0.3						  *
// *				-----------------------					      *
// *															  *
// * Implementing Observer,Strategy and Decorator Design Patterns *
// *														      *
// *		Modified from Joseph Bergin's "Building				  *
// *        Graphical User Interfaces with the MVC Pattern"		  *
// *		http://csis.pace.edu/%7Ebergin/mvc/mvcgui.html   	  *
// *															  *
// *															  *
// *		Created by Alex Mirzakandov, 2006					  *
// *		alextz@hotmail.co.il								  *
// ****************************************************************
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MVC_Example
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MVCForm : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		public TemperatureModel temperature = new TemperatureModel();

		public MVCForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();


		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// MVCForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(520, 443);
			this.IsMdiContainer = true;
			this.Name = "MVCForm";
			this.Text = "MVC Example";
			this.Load += new System.EventHandler(this.MVCForm_Load);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new MVCForm());
		}

		private void MVCForm_Load(object sender, System.EventArgs e)
		{
			TemperatureForm celsius = new TemperatureForm("Celsius");
			TemperatureForm farenheit = new TemperatureForm("Farenheit");
			FarenheitGauge farenheitGauge = new FarenheitGauge();
			CelsiusScroll celsiusScroll = new CelsiusScroll();

			celsius.MdiParent = this;
			celsius.valueStrategy = new CelsiusStrategy(temperature);
			celsius.Location = new Point(0,0);
			celsius.Show();
			temperature.TemperatureChanged+= new TemperatureModel.TemperatureChangedHandler(celsius.TemperatureDisplay);
			
			farenheit.MdiParent = this;
			farenheit.valueStrategy = new FarenheitStrategy(temperature);
			farenheit.Show();
			temperature.TemperatureChanged+= new TemperatureModel.TemperatureChangedHandler(farenheit.TemperatureDisplay);

			farenheitGauge.MdiParent = this;
			farenheitGauge.valueStrategy = new ValueDecorator(new FarenheitStrategy(temperature),300,-200);
			farenheitGauge.MaxTemp = 300;
			farenheitGauge.MinTemp = -200;
			farenheitGauge.Show();
			temperature.TemperatureChanged+= new TemperatureModel.TemperatureChangedHandler(farenheitGauge.TemperatureDisplay);

			celsiusScroll.MdiParent = this;
			celsiusScroll.valueStrategy = new ValueDecorator(new CelsiusStrategy(temperature),150,-100);
			celsiusScroll.MaxTemp = 150;
			celsiusScroll.MinTemp = -100;
			celsiusScroll.Show();
			temperature.TemperatureChanged+= new TemperatureModel.TemperatureChangedHandler(celsiusScroll.TemperatureDisplay);

		}

	}
}

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

nashcontrol
Web Developer
Israel Israel
No Biography provided
Follow on   Twitter

| Advertise | Privacy | Mobile
Web02 | 2.8.140921.1 | Last Updated 27 Feb 2006
Article Copyright 2006 by nashcontrol
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid