SgDotNet
Singapore Professional .NET User Group -For Cool Developers

(Abstract) Factory Pattern??

Latest post 04-10-2008 12:26 PM by cruizer. 7 replies.
  • 09-19-2004 12:40 AM

    • stw
    • Top 50 Contributor
    • Joined on 05-04-2004
    • Singapore
    • Posts 94

    (Abstract) Factory Pattern??

    Hi there,

    pattern rulez. Why scratch your head when others did that already... However the devil is in the details (as we would say in German). Yes I did read the article on MSDN about it. So my question:

    I'd like to create an abstract factory that is configurable (a.k.a. runtime extenable). I want to call it with a parameter, that is matched against an configuration file to return an instance of a specific class (all classes inherit from a baseclass and this is what the factory returns.

    So I have

    Public Function  EasterEggFactory(Eggtype as String) as EasterEgg
        Select Case Eggtype
              Case "red"
                    Return new EggRed
              Case "blue"
                    Return new EggBlue
              Case else
                    Return new Egg
        End Select
    End Function

    The possible Classes are Hardcoded. However I'd like to read them from a configuration. (How to get them into a hasttable is the easy part). So my code should look like

    Dim classname as String  =  LookupClassName(EggType)
    if classname.Equals("") then
         classname = "Egg"
    end if
    Return new <and here I got stuck, how to create a class I know the name with option strict on>

    Who helps?
    :-) stw
    If you think education is expensive - try ignorance!
  • 09-19-2004 1:23 AM In reply to

    Re: (Abstract) Factory Pattern??

    System.Reflection - You could possibly use the Type.GetConstructor()/GetConstructors() methods to obtain ConstructorInfo objects which you can Invoke() to obtain the appropriate class type you need.

    The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

  • 09-19-2004 2:14 AM In reply to

    • Binesh
    • Top 25 Contributor
    • Joined on 05-04-2004
    • Singapore
    • Posts 167

    Re: (Abstract) Factory Pattern??

    Hi


     private object _classObj = null; private Type _classType= null private Assembly _assembly= null  _assembly = Assembly.Load(" YourDll "); _classType = _assembly.GetType(" ClassName ",false,true); _classObj  = Activator.CreateInstance(_classType);  /* Property */ PropertyInfo pInfo=_classType.GetProperty(" PropName "); pInfo.SetValue(_classObj,"value",null);  /*  Method */ MethodInfo mInfo = _classType.GetMethod(" MethodName "); mInfo.Invoke(_classObj,null); 
     


     Since you are using Factory class (Assuming BaseFactory is your base class )

     BaseFactory _factObj  =  Activator.CreateInstance(_classType) as  BaseFactory; _factObj.Method(); _factObj.Prop = "somevalue"; 
     

    hth...
    If we knew what it was we were doing, it would not be called research, would it?.... Albert Einstein
  • 09-22-2004 3:20 AM In reply to

    Re: (Abstract) Factory Pattern??

    assume you got XmlNode node referring to one of <Add> in the below after lookup (you can use anyway to store your configuration this is just an example, eg your hashtable way)

    <EggFactory>
    <Add Name="" Assembly="EasterEgg.dll" Type="YourNameSpace.Egg" />
    <Add Name="Red" Assembly="EasterEgg.dll" Type="YourNameSpace.EggRed" />
    <Add Name="Blue" Assembly="EasterEgg2.dll" Type="YourNameSpace.EggBlue" />
    </EggFactory>

    string asm = this.GetAssemblyFullName(node.Attributes["Assembly"].Value);
    string type = node.Attributes["Type"].Value;
    object obj = Assembly.LoadFile(asm).CreateInstance(
         type, false, BindingFlags.CreateInstance, null, null,
         CultureInfo.CurrentCulture, null);
    EasterEgg egg = obj as EasterEgg;
    return egg;


    // Helper
    private string GetAssemblyFullName(string assemblyValue)
    {
       string path = assemblyValue.Replace(@"\", "/");
       if (path.IndexOf("/") == -1)
       {
          path = Environment.CurrentDirectory + "/" + path;
       }
       return path;
    }

    You should be able to figure out vb.net counterparts..hth.
    blackinkbottle "please polish my crude soul", to the maker of blade the Samurai kneed down and said.
  • 06-27-2005 3:36 PM In reply to

    Re: (Abstract) Factory Pattern??

     stw wrote:

    I'd like to create an abstract factory that is configurable (a.k.a. runtime extenable). I want to call it with a parameter, that is matched against an configuration file to return an instance of a specific class (all classes inherit from a baseclass and this is what the factory returns.

    So I have

    Public Function  EasterEggFactory(Eggtype as String) as EasterEgg
        Select Case Eggtype
              Case "red"
                    Return new EggRed
              Case "blue"
                    Return new EggBlue
              Case else
                    Return new Egg
        End Select
    End Function

    The possible Classes are Hardcoded. However I'd like to read them from a configuration. (How to get them into a hasttable is the easy part). So my code should look like

    Dim classname as String  =  LookupClassName(EggType)
    if classname.Equals("") then
         classname = "Egg"
    end if
    Return new

    Who helps?
    :-) stw


    This looks cool, cause we can potentially later add new sub-classes and just change the config file to instantiate the new classes without changing the code in the factory.  Cool.

    However, I have always heard about overhead of Reflection.  Anyone can care to comment on their experiences on this?
    Knowledge, Hardwork, Patience...
  • 06-30-2005 1:13 AM In reply to

    Re: (Abstract) Factory Pattern??

     Cyrus Crypt wrote:

    However, I have always heard about overhead of Reflection.  Anyone can care to comment on their experiences on this?

    You might want to visit this link to see what methods are considered lightweight and heavyweight in the Reflection namespace http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/default.aspx

  • 04-10-2008 10:11 AM In reply to

    Re: (Abstract) Factory Pattern??

    It can be achieved easily through the use of DI container. Take a look at this. 

    http://sgdotnet.org/forums/permalink/204591/205374/ShowThread.aspx#205374

     

    Filed under:
  • 04-10-2008 12:26 PM In reply to

    • cruizer
    • Top 25 Contributor
    • Joined on 07-25-2007
    • Singapore
    • Posts 153

    Re: (Abstract) Factory Pattern??

    yeah, why create your own (from scratch)? you can use a proven IoC container like Spring.NET, Castle Windsor or StructureMap. I heard that even MS now has its own IoC container (is that what they call "Unity?").
    http://devpinoy.org/blogs/cruizer
Page 1 of 1 (8 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems