Yeah I covered both of those possibilities in my two posts. And they're decent, but not ideal.
The nested class solution does not allow for the possibility of say a framework that loads plugins; external classes that implement the interface then get loaded by the factory.
And the assembly based solution is what ultimately I'd go with if necessary, because it does afford all flexibility. But it just does seem like overkill to create an entirely new assembly just to have a factory in it. It also prevents the plugin approach because only classes within the factory assembly could be loaded.
If the Widget class could possibly exist as a type outside of the factory then simply make the Widget class part of the same assembly as the factory class and give it "internal" scope. The drawback of this is any other class in the assembly can instantiate it:
interface IWidget { string Name { get; } } internal class Widget : IWidget { public Widget() { }
#region IWidget Members
public string Name { get { return "Name"; } } #endregion }
class Factory { public static IWidget CreateWidget() { return new Widget(); } }
If you view the Widget class as an implementation detail of the factory class (which is what I would suggest), then you should make the Widget class a nested class:
interface IWidget { string Name { get; } } class Factory { class Widget : IWidget { #region IWidget Members public Widget() { }
public string Name { get { return "Name"; } }
#endregion } public static IWidget CreateWidget() { return new Widget(); } }
This makes the Widget class persona non grata--just like any other private member of the factory class--with no possible way to instantiate it outside of the factory class.
Factory pattern, possible in C#?
gofrm
The nested class solution does not allow for the possibility of say a framework that loads plugins; external classes that implement the interface then get loaded by the factory.
And the assembly based solution is what ultimately I'd go with if necessary, because it does afford all flexibility. But it just does seem like overkill to create an entirely new assembly just to have a factory in it. It also prevents the plugin approach because only classes within the factory assembly could be loaded.
Amit Jitendra
sacherel
There's a couple of ways you can implement this.
If the Widget class could possibly exist as a type outside of the factory then simply make the Widget class part of the same assembly as the factory class and give it "internal" scope. The drawback of this is any other class in the assembly can instantiate it:
interface IWidget
{
string Name
{
get;
}
}
internal class Widget : IWidget
{
public Widget()
{
}
#region IWidget Members
public string Name
{
get
{
return "Name";
}
}
#endregion
}
class Factory
{
public static IWidget CreateWidget()
{
return new Widget();
}
}
If you view the Widget class as an implementation detail of the factory class (which is what I would suggest), then you should make the Widget class a nested class:
interface IWidget
{
string Name
{
get;
}
}
class Factory
{
class Widget : IWidget
{
#region IWidget Members
public Widget()
{
}
public string Name
{
get
{
return "Name";
}
}
#endregion
}
public static IWidget CreateWidget()
{
return new Widget();
}
}