Here is the way I typically see the Abstract Factory Pattern shown:
public abstract class Factory
{
public abstract Product GetProduct();
}
public class ConcreteFactory1 : Factory
{
public override Product GetProduct() { return new Product1(); }
}
class ConcreteFactory2 : Factory
{
public override Product GetProduct() { return new Product2(); }
}
interface Product
{
void SomeMethod();
}
class Product1 : Product
{
public void SomeMethod() { Console.WriteLine("1"); }
}
class Product2 : Product
{
public void SomeMethod() { Console.WriteLine("2"); }
}
class Program
{
static void Main(string[] args)
{
Factory f = null;
Product p = null;
switch (args[0])
{
case "1":
f = new ConcreteFactory1();
p = f.GetProduct();
break;
case "2":
f = new ConcreteFactory2();
p = f.GetProduct();
break;
}
p.SomeMethod();
}
}
I typically write it like this, which is not the true pattern:
interface Product
{
void SomeMethod();
}
class Product1 : Product
{
public void SomeMethod() { Console.WriteLine("1"); }
}
class Product2 : Product
{
public void SomeMethod() { Console.WriteLine("2"); }
}
public static class Factory
{
public static Product GetProduct(prodType)
{
Product p = null;
switch (prodType)
{
case "1":
p = new Product1();
break;
case "2":
p = new Product2();
break;
}
return p;
}
}
class Program
{
static void Main(string[] args)
{
Product p = Factory.GetProduct(args[0]);
p.SomeMethod();
}
}
My question is - what is the advantage of the concrete factories? I have never understood the point - it just seems to add another unnecessary layer. The second example seems much more concise and straightforward.
prodType
. Instead, the concrete factory implementation is fed via some form of dependency injection to your abstract factory, and your code goes on its merry way without having any concept of what factory is being used, or the concrete types needed. Especially if those concrete factory implementations are provided by 3rd party consumers of your API. – Consistory