Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#

Implementation of Class Factories in C#

Rate me:
Please Sign up or sign in to vote.
4.85/5 (33 votes)
16 Jan 20039 min read 131.3K   146   85  
Details five ways to implement a class factory in C#.
//=========================================================================
//
// HashSwitchFactory.cs
//
// Sample implementation of a class factory using a hash table &
// switch statement.
//
// Sean Winstead, 1/17/2003
//
//=========================================================================
using System;
using System.Collections;

namespace ClassFactory
{
  public class HashSwitchFactory : BaseClassFactory
  {
    // The following hash table maps an XSLT element name to an
    // integer index. The Create method uses the index to identify
    // the class to be instantiated.
    private Hashtable _ElementHash;

    public HashSwitchFactory()
    {
      // Populate the hash table.
      _ElementHash = new Hashtable(50, (float)0.5);
      _ElementHash.Add(Globals.ElementNames[0], 0);
      _ElementHash.Add(Globals.ElementNames[1], 1);
      _ElementHash.Add(Globals.ElementNames[2], 2);
      _ElementHash.Add(Globals.ElementNames[3], 3);
      _ElementHash.Add(Globals.ElementNames[4], 4);
      _ElementHash.Add(Globals.ElementNames[5], 5);
      _ElementHash.Add(Globals.ElementNames[6], 6);
      _ElementHash.Add(Globals.ElementNames[7], 7);
      _ElementHash.Add(Globals.ElementNames[8], 8);
      _ElementHash.Add(Globals.ElementNames[9], 9);
      _ElementHash.Add(Globals.ElementNames[10], 10);
      _ElementHash.Add(Globals.ElementNames[11], 11);
      _ElementHash.Add(Globals.ElementNames[12], 12);
      _ElementHash.Add(Globals.ElementNames[13], 13);
      _ElementHash.Add(Globals.ElementNames[14], 14);
      _ElementHash.Add(Globals.ElementNames[15], 15);
      _ElementHash.Add(Globals.ElementNames[16], 16);
      _ElementHash.Add(Globals.ElementNames[17], 17);
      _ElementHash.Add(Globals.ElementNames[18], 18);
      _ElementHash.Add(Globals.ElementNames[19], 19);
      _ElementHash.Add(Globals.ElementNames[20], 20);
      _ElementHash.Add(Globals.ElementNames[21], 21);
      _ElementHash.Add(Globals.ElementNames[22], 22);
      _ElementHash.Add(Globals.ElementNames[23], 23);
    }

    public override BaseElement Create(string name)
    {
      switch ((int) _ElementHash[name])
      {
        case 0:
          return new XslApplyImports();
        case 1:
          return new XslApplyTemplates();
        case 2:
          return new XslAttribute();
        case 3:
          return new XslAttributeSet();
        case 4:
          return new XslCallTemplate();
        case 5:
          return new XslChoose();
        case 6:
          return new XslComment();
        case 7:
          return new XslCopy();
        case 8:
          return new XslCopyOf();
        case 9:
          return new XslDecimalFormat();
        case 10:
          return new XslDocument();
        case 11:
          return new XslElement();
        case 12:
          return new XslFallback();
        case 13:
          return new XslForEach();
        case 14:
          return new XslIf();
        case 15:
          return new XslImport();
        case 16:
          return new XslInclude();
        case 17:
          return new XslKey();
        case 18:
          return new XslMessage();
        case 19:
          return new XslNamespaceAlias();
        case 20:
          return new XslNumber();
        case 21:
          return new XslOtherwise();
        case 22:
          return new XslOutput();
        case 23:
          return new XslParam();
        default:
          return null;
      }
    }

  }
}

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
United States United States
Sean is a consultant for Falafel Software, Inc. and gets to spend his time working on fun .NET projects. He lives and works in beautiful Colorado Springs.

Comments and Discussions