Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

The Interface Construct in C#

Rate me:
Please Sign up or sign in to vote.
4.77/5 (86 votes)
31 Oct 20078 min read 302.6K   4.8K   171  
Using interfaces as a means to decouple classes, making your application more flexible.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace InterfaceDemo
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.RadioButton radioButtonXMLinput;
		private System.Windows.Forms.RadioButton radioButtonIniInput;
		private System.Windows.Forms.RadioButton radioButtoBinInput;
		private System.Windows.Forms.RadioButton radioButtonRedGUI;
		private System.Windows.Forms.RadioButton radioButtonBlueGUI;
		private System.Windows.Forms.RadioButton radioButtonYellowGUI;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

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

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.button1 = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.radioButtoBinInput = new System.Windows.Forms.RadioButton();
			this.radioButtonIniInput = new System.Windows.Forms.RadioButton();
			this.radioButtonXMLinput = new System.Windows.Forms.RadioButton();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.radioButtonYellowGUI = new System.Windows.Forms.RadioButton();
			this.radioButtonRedGUI = new System.Windows.Forms.RadioButton();
			this.radioButtonBlueGUI = new System.Windows.Forms.RadioButton();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(152, 240);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(120, 24);
			this.button1.TabIndex = 0;
			this.button1.Text = "Drive";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.BackColor = System.Drawing.Color.Transparent;
			this.groupBox1.Controls.Add(this.radioButtoBinInput);
			this.groupBox1.Controls.Add(this.radioButtonIniInput);
			this.groupBox1.Controls.Add(this.radioButtonXMLinput);
			this.groupBox1.ForeColor = System.Drawing.Color.Red;
			this.groupBox1.Location = new System.Drawing.Point(304, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(128, 248);
			this.groupBox1.TabIndex = 9;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Pick Service Provider";
			// 
			// radioButtoBinInput
			// 
			this.radioButtoBinInput.ForeColor = System.Drawing.Color.Red;
			this.radioButtoBinInput.Location = new System.Drawing.Point(24, 188);
			this.radioButtoBinInput.Name = "radioButtoBinInput";
			this.radioButtoBinInput.Size = new System.Drawing.Size(57, 16);
			this.radioButtoBinInput.TabIndex = 2;
			this.radioButtoBinInput.Text = "Bin file";
			// 
			// radioButtonIniInput
			// 
			this.radioButtonIniInput.ForeColor = System.Drawing.Color.Red;
			this.radioButtonIniInput.Location = new System.Drawing.Point(24, 115);
			this.radioButtonIniInput.Name = "radioButtonIniInput";
			this.radioButtonIniInput.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.radioButtonIniInput.Size = new System.Drawing.Size(61, 16);
			this.radioButtonIniInput.TabIndex = 1;
			this.radioButtonIniInput.Text = "Ini file";
			// 
			// radioButtonXMLinput
			// 
			this.radioButtonXMLinput.ForeColor = System.Drawing.Color.Red;
			this.radioButtonXMLinput.Location = new System.Drawing.Point(26, 38);
			this.radioButtonXMLinput.Name = "radioButtonXMLinput";
			this.radioButtonXMLinput.Size = new System.Drawing.Size(64, 18);
			this.radioButtonXMLinput.TabIndex = 0;
			this.radioButtonXMLinput.Text = "XML file";
			// 
			// groupBox2
			// 
			this.groupBox2.BackColor = System.Drawing.Color.Transparent;
			this.groupBox2.Controls.Add(this.radioButtonYellowGUI);
			this.groupBox2.Controls.Add(this.radioButtonRedGUI);
			this.groupBox2.Controls.Add(this.radioButtonBlueGUI);
			this.groupBox2.ForeColor = System.Drawing.Color.Red;
			this.groupBox2.Location = new System.Drawing.Point(16, 8);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(96, 240);
			this.groupBox2.TabIndex = 10;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Pick Client";
			// 
			// radioButtonYellowGUI
			// 
			this.radioButtonYellowGUI.ForeColor = System.Drawing.Color.Red;
			this.radioButtonYellowGUI.Location = new System.Drawing.Point(11, 187);
			this.radioButtonYellowGUI.Name = "radioButtonYellowGUI";
			this.radioButtonYellowGUI.Size = new System.Drawing.Size(80, 16);
			this.radioButtonYellowGUI.TabIndex = 4;
			this.radioButtonYellowGUI.Text = "YellowGUI";
			// 
			// radioButtonRedGUI
			// 
			this.radioButtonRedGUI.ForeColor = System.Drawing.Color.Red;
			this.radioButtonRedGUI.Location = new System.Drawing.Point(18, 114);
			this.radioButtonRedGUI.Name = "radioButtonRedGUI";
			this.radioButtonRedGUI.Size = new System.Drawing.Size(78, 19);
			this.radioButtonRedGUI.TabIndex = 3;
			this.radioButtonRedGUI.Text = "RedGUI";
			// 
			// radioButtonBlueGUI
			// 
			this.radioButtonBlueGUI.ForeColor = System.Drawing.Color.Red;
			this.radioButtonBlueGUI.Location = new System.Drawing.Point(16, 40);
			this.radioButtonBlueGUI.Name = "radioButtonBlueGUI";
			this.radioButtonBlueGUI.Size = new System.Drawing.Size(72, 16);
			this.radioButtonBlueGUI.TabIndex = 2;
			this.radioButtonBlueGUI.Text = "BlueGUI";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
			this.ClientSize = new System.Drawing.Size(442, 272);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.button1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "Form1";
			this.Text = "InterfaceDemo";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		/*
		 * DRIVER
		 * 
		 * The three client classes and the three service classes can be combined in nine different ways.
		 * The client classes (the color GUIs) does not care about the service classes (reader classes) that they use.
		 * The only thing the client wants is for a service class to implement the IInput interface.
		 *
		 * For a class to not care about the type of another class is indirection.
		 * Indirection makes the design flexible for future changes.
		 */ 
		private void button1_Click(object sender, System.EventArgs e)
		{
			// Input XML file to Blue GUI
			if ((radioButtonBlueGUI.Checked) && (radioButtonXMLinput.Checked))
			{
				IInput input = new XMLInput();
				BlueGUI blueGUI = new BlueGUI(input);
				blueGUI.GetInput();
			}

			// Input XML file to Red GUI
			if ((radioButtonRedGUI.Checked) && (radioButtonXMLinput.Checked))
			{
				IInput input = new XMLInput();
				RedGUI redGUI = new RedGUI(input);
				redGUI.GetInput();
			}
			
			// Input XML file to Yellow GUI
			if ((radioButtonYellowGUI.Checked) && (radioButtonXMLinput.Checked))
			{
				IInput input = new XMLInput();
				YellowGUI yellowGUI = new YellowGUI(input);
				yellowGUI.GetInput();
			}
			
			// Input INI file to Blue GUI
			if ((radioButtonIniInput.Checked) && (radioButtonBlueGUI.Checked))
			{
				IInput input = new INIInput();
				BlueGUI blueGUI = new BlueGUI(input);
				blueGUI.GetInput();
			}

			// Input INI file to Red GUI
			if ((radioButtonIniInput.Checked) && (radioButtonRedGUI.Checked))
			{
				IInput input = new INIInput();
				RedGUI redGUI = new RedGUI(input);
				redGUI.GetInput();
			}

			// Input INI file to Yellow GUI
			if ((radioButtonIniInput.Checked) && (radioButtonYellowGUI.Checked))
			{
				IInput input = new INIInput();
				YellowGUI yellowGUI = new YellowGUI(input);
				yellowGUI.GetInput();			
			}

			// Input Bin file to Blue GUI
			if ((radioButtoBinInput.Checked) && (radioButtonBlueGUI.Checked))
			{
				IInput input = new BinInput();
				BlueGUI blueGUI = new BlueGUI(input);
				blueGUI.GetInput();			
			}
			
			// Input Bin file to Red GUI
			if ((radioButtoBinInput.Checked) && (radioButtonRedGUI.Checked))
			{
				IInput input = new BinInput();
				RedGUI redGUI = new RedGUI(input);
				redGUI.GetInput();			
			}

			// Input Bin file to Yellow GUI
			if ((radioButtoBinInput.Checked) && (radioButtonYellowGUI.Checked))
			{
				IInput input = new BinInput();
				YellowGUI yellowGUI = new YellowGUI(input);
				yellowGUI.GetInput();			
			}			
		}
	}
}

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
Denmark Denmark
I am working as a software developer for COWI Engineers and Planners since year 2000, http://www.cowi.dk.

My main area of development is GIS (Geographical Information Systems) software using C# and Delphi with the mapping tools: MapXtreme 2004/MapBasic/MapInfo.


Comments and Discussions