Click here to Skip to main content
15,881,870 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
4.21/5 (16 votes)
27 Feb 20062 min read 67.4K   1.1K   80  
This article demonstrates the use of the Observer, Strategy, and Decorator patterns in C#.
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace MVC_Example
{
	public class TemperatureForm : System.Windows.Forms.Form, IView
	{
		public System.Windows.Forms.Button buttonIncrease;
		public System.Windows.Forms.Button buttonDecrease;
		public System.Windows.Forms.Label labelIntro;
		public System.Windows.Forms.TextBox textBoxTemprerature;
		public ValueStrategy valueStrategy;

		private System.ComponentModel.Container components = null;

		public TemperatureForm(string Name)
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();

			this.labelIntro.Text = Name;
		}

		/// <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 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()
		{
			this.textBoxTemprerature = new System.Windows.Forms.TextBox();
			this.buttonIncrease = new System.Windows.Forms.Button();
			this.buttonDecrease = new System.Windows.Forms.Button();
			this.labelIntro = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// textBoxTemprerature
			// 
			this.textBoxTemprerature.Location = new System.Drawing.Point(16, 48);
			this.textBoxTemprerature.Name = "textBoxTemprerature";
			this.textBoxTemprerature.Size = new System.Drawing.Size(128, 20);
			this.textBoxTemprerature.TabIndex = 13;
			this.textBoxTemprerature.Text = "";
			// 
			// buttonIncrease
			// 
			this.buttonIncrease.Location = new System.Drawing.Point(16, 80);
			this.buttonIncrease.Name = "buttonIncrease";
			this.buttonIncrease.Size = new System.Drawing.Size(64, 23);
			this.buttonIncrease.TabIndex = 11;
			this.buttonIncrease.Text = "Increase";
			this.buttonIncrease.Click += new System.EventHandler(this.buttonIncrease_Click);
			// 
			// buttonDecrease
			// 
			this.buttonDecrease.Location = new System.Drawing.Point(80, 80);
			this.buttonDecrease.Name = "buttonDecrease";
			this.buttonDecrease.Size = new System.Drawing.Size(64, 23);
			this.buttonDecrease.TabIndex = 12;
			this.buttonDecrease.Text = "Decrease";
			this.buttonDecrease.Click += new System.EventHandler(this.buttonDecrease_Click);
			// 
			// labelIntro
			// 
			this.labelIntro.AutoSize = true;
			this.labelIntro.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(177)));
			this.labelIntro.Location = new System.Drawing.Point(40, 8);
			this.labelIntro.Name = "labelIntro";
			this.labelIntro.Size = new System.Drawing.Size(71, 25);
			this.labelIntro.TabIndex = 10;
			this.labelIntro.Text = "Celsius";
			// 
			// TemperatureForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(160, 123);
			this.Controls.Add(this.textBoxTemprerature);
			this.Controls.Add(this.buttonIncrease);
			this.Controls.Add(this.buttonDecrease);
			this.Controls.Add(this.labelIntro);
			this.Location = new System.Drawing.Point(0, 220);
			this.Name = "TemperatureForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
			this.Text = "Temperature";
			this.Load += new System.EventHandler(this.TemperatureForm_Load);
			this.ResumeLayout(false);

		}
		#endregion

		

		private void buttonIncrease_Click(object sender, System.EventArgs e)
		{
			++valueStrategy.Temperature;
			
		}

		private void buttonDecrease_Click(object sender, System.EventArgs e)
		{
			--valueStrategy.Temperature;
			
		}


		private void TemperatureForm_Load(object sender, System.EventArgs e)
		{
			
			textBoxTemprerature.Text = valueStrategy.Temperature.ToString();
		}

		public void TemperatureDisplay(object sender, EventArgs e)
		{
			textBoxTemprerature.Text = valueStrategy.Temperature.ToString("#.#");
		}
	}
}

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


Written By
Web Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions