实例化具有相同发方法的不同对象时,不需要暴露实例的详细逻辑。
也就是说,这些对象具有相同的接口。
public interface IProduct {string GetProductName();}
public class ProductA : IProduct
{
string GetProductName() {return "this is Product A";}
}
public class ProductB : IProduct
{
string GetProductName() {return "this is Product B";}
}
public class SimpleFactory
{
IProduct CreateProduct(string productType)
{
if (productType == "A")
return new ProductA();
else
return new ProductB();
}
}