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

Another way of polymorphism

Rate me:
Please Sign up or sign in to vote.
4.09/5 (8 votes)
14 Feb 20059 min read 51.5K   71   23  
An article on how ad hoc adapters generation can be emulated in C#.
/*
Copyright (c) 2005 by vistal.sourceforge.net
All rights reserved.
*/

using System;

namespace Net.SourceForge.Vistal.Samples.DummySwitch
{
	interface IDummySwitch
	{
		void Display();
		void PressButton();
	}

	class DummySwitch : Machine
	{
		protected new class Main : Machine.Mode, IDummySwitch
		{
			public void Display()
			{
				Console.Out.WriteLine("main mode");
			}

			public void PressButton()
			{
				NextMode(typeof(Another));
			}
		}
		
		protected class Another : Machine.Mode, IDummySwitch
		{
			public void Display()
			{
				Console.Out.WriteLine("another mode");
			}

			public virtual void PressButton()
			{
				NextMode(typeof(Main));
			}
		}
	}

	class ExtendedSwitch : DummySwitch
	{
		protected new class Another : DummySwitch.Another, IDummySwitch
		{
			public override void PressButton()
			{
				NextMode(typeof(Extension));
			}
		}

		protected class Extension  : Machine.Mode, IDummySwitch
		{
			public void Display()
			{
				Console.Out.WriteLine("extension mode");
			}

			public void PressButton()
			{
				NextMode(typeof(Main));
			}
		}
	}

	class EntryPoint
	{
		static void PlayWith(IDummySwitch toy)
		{
			for (int i = 0; i < 5; i++)
			{
				toy.Display();
				toy.PressButton();
			}
		}

		[STAThread]
		static void Main(string[] args)
		{
			Console.Out.WriteLine("Play with the dummy object: ");
			PlayWith((IDummySwitch)Machine.Create(typeof(IDummySwitch), new DummySwitch()));

			Console.Out.WriteLine("\nThe same game with the EXTENDED dummy object: ");
			PlayWith((IDummySwitch)Machine.Create(typeof(IDummySwitch), new ExtendedSwitch()));
		}
	}
}

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
Russian Federation Russian Federation
An experienced software developer.

Now I'm participating in VisualSVN project that is an integration package between Subversion and Visual Studio.

Comments and Discussions