Click here to Skip to main content
13,293,369 members (48,078 online)
Click here to Skip to main content


79 bookmarked
Posted 27 Feb 2006

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#.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace MVC_Example
	/// <summary>
	/// Summary description for FarenheitGauge.
	/// </summary>
	public class FarenheitGauge : System.Windows.Forms.Form, IView
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		public ValueStrategy valueStrategy;

		private int width = 20;
		private int top = 20;
		private int left = 100;
		private int height = 200;

		public double MaxTemp;
		public double MinTemp;

		public FarenheitGauge()



		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
			if( disposing )
				if(components != null)
			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()
			// FarenheitGauge
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.Color.White;
			this.ClientSize = new System.Drawing.Size(208, 271);
			this.Location = new System.Drawing.Point(230, 70);
			this.Name = "FarenheitGauge";
			this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
			this.Text = "Farenheit Gauge";
			this.Load += new System.EventHandler(this.FarenheitGauge_Load);
			this.Paint += new System.Windows.Forms.PaintEventHandler(this.FarenheitGauge_Paint);


		private void FarenheitGauge_Load(object sender, System.EventArgs e)

		public void TemperatureDisplay(object sender, EventArgs e)

		private void FarenheitGauge_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
			Graphics g = e.Graphics;

			g.DrawRectangle(new Pen(Color.Black), left,top, width, height);
			g.FillEllipse(new SolidBrush(Color.Red),left-width/2, top+height-width/3,width*2, width*2);
			g.DrawEllipse(new Pen(Color.Black),left-width/2, top+height-width/3,width*2, width*2);
			g.FillRectangle(new SolidBrush(Color.White) ,left+1,top+1, width-1, height-1);

			double redtop = height*(valueStrategy.Temperature - MaxTemp)/((MinTemp)-MaxTemp);
			g.FillRectangle(new SolidBrush(Color.Red),left+1, top + (int)redtop, width-1, height-(int)redtop);


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

Web Developer
Israel Israel
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 27 Feb 2006
Article Copyright 2006 by nashcontrol
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid