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

Delegate Factory

, 11 Jan 2005 CPOL
A dynamic factory using delegates.
delegatefactory_demo.zip
Objects
Objects.csproj.user
TestFactory
App.ico
TestFactory.csproj.user
using System;
using Objects;

namespace TestFactory
{
	/// <summary>
	/// Summary description for ClassMain.
	/// </summary>
	class ClassMain
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			try
			{
				// registering the types that the factory will create
				ObjectFactory.RegisterHandler(Class1.ClassType, new ObjectCreator(Class1.ObjectCreator));
				ObjectFactory.RegisterHandler(Class2.ClassType, new ObjectCreator(Class2.ObjectCreator));
				ObjectFactory.RegisterHandler(Class3.ClassType, new ObjectCreator(Class3.ObjectCreator));

				AObject aobject = null;
				// creating the objects
				for (int i = 0; i<100; i++)
				{
					aobject = ObjectFactory.CreateObject(i%3+1, null);
					aobject.Print();
				}

				// unregistering a type
				if (!ObjectFactory.UnregisterHandler(Class1.ClassType))
					Console.WriteLine("Really ?!");

				// trying to create an unregistered type
				aobject = ObjectFactory.CreateObject(1, null);
				if (aobject != null)
					aobject.Print();
				else
					Console.WriteLine("aobject is null");
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message);
			}
			Console.ReadLine();
		}
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Asa Meltzer
Software Developer
Israel Israel
Software designer and programmer.
Programming languages:
MFC, C++, Java , C#, VB and sometimes C and assembly.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 11 Jan 2005
Article Copyright 2005 by Asa Meltzer
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid