SgDotNet
Singapore Professional .NET User Group -For Cool Developers

(Abstract) Factory Pattern??

rated by 0 users
This post has 7 Replies | 1 Follower

Top 50 Contributor
Posts 94
stw Posted: 09-19-2004 12:40 AM
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!
Top 10 Contributor
Posts 2,284
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

Top 25 Contributor
Posts 167
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
Top 25 Contributor
Posts 330
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.
Top 50 Contributor
Posts 146
 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...
Top 25 Contributor
Posts 232

 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

Top 100 Contributor
Posts 22

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

 

Top 25 Contributor
Posts 184
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