Click here to Skip to main content
12,699,807 members (36,610 online)
Click here to Skip to main content

Stats

45.7K views
114 downloads
43 bookmarked
Posted

Delegate Factory

, 11 Jan 2005 CPOL
A dynamic factory using delegates.
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.

You may also be interested in...

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