DEV Community

Discussion on: How to use Factory Method Design Pattern in C#

Collapse
 
codethreader profile image
CodeThreader

I enjoyed so much reading your 39map code. I had to build something so similar two days ago.

I wish I read this just 2 days ago, would have saved me a full day. I ran into a very weird problem and I will fit it into your example.

I already had my types collected via reflection and I continued smt like

foreach( t in types)
{
ICustomMap map = (ICustomMap) Activator.CreateInstance(t));

//all maps with ID "notOfficial" are not gov approved so don't load them yet

if (map.ID != "notOfficial")
mapsCollection.add(map);

//what happened to my surprise, map.ID didn't return map.ID of the instance but instead that of the base class "NULL"

//outside of the loop, if I loop the mapsCollection, map.ID shows the proper ID

}

Collapse
 
gary_woodfine profile image
Gary Woodfine

Maybe it would have been a cool Idea to actually create an attribute to your Map definition of Unofficial and Official then you could actually create separate collections for them.