为了区分Factory Method和Abstract Factory的区别,查阅了些资料,总算有点感悟,下面是本人的感悟的总结。
在设计模式中,Factory Method和Abstract Factory的区别并不容易理解,主要是Abstrct Factory一般情况下是要通过Factory Method来实现,或者说Abstract Factory包含Factroy Method。
从字面上来理解,Factory Method强调的是Method,Abstract Factory强调的是抽象类,也就是继承关系。
下面,将使用例子来对比Factory Method和Abstract Factory。
有两种不同的产品,分别是电视和收音机,下面分别用Factory Method和Abstract Factory模式对两种产品进行创建。
(1) Factory Method模式主要通过工厂类的方法的多态来实现对多种不同产品的创建。其C#示例代码如下所示。
namespace
FactoryMethod
{
class Program
{
public interface IProduct
{
void ProductName();
}
public class CRadio : IProduct
{
public void ProductName()
{
Console.WriteLine( " Radio " );
}
}
public class CTV : IProduct
{
public void ProductName()
{
Console.WriteLine( " TV " );
}
}
public interface IFactory
{
IProduct CreateProduct( string ProductName);
}
public class CConcreteFactory
{
public IProduct CreateProduct( string ProductName)
{
if ( 0 == ProductName.CompareTo( " TV " ))
{
return new CTV();
}
else
{
return new CRadio();
}
}
}
static void Main( string [] args)
{
CConcreteFactory ConFactory = new CConcreteFactory();
IProduct TVProduct = ConFactory.CreateProduct( " TV " );
TVProduct.ProductName();
IProduct RadioProduct = ConFactory.CreateProduct( " Radio " );
RadioProduct.ProductName();
Console.Read();
}
}
}
{
class Program
{
public interface IProduct
{
void ProductName();
}
public class CRadio : IProduct
{
public void ProductName()
{
Console.WriteLine( " Radio " );
}
}
public class CTV : IProduct
{
public void ProductName()
{
Console.WriteLine( " TV " );
}
}
public interface IFactory
{
IProduct CreateProduct( string ProductName);
}
public class CConcreteFactory
{
public IProduct CreateProduct( string ProductName)
{
if ( 0 == ProductName.CompareTo( " TV " ))
{
return new CTV();
}
else
{
return new CRadio();
}
}
}
static void Main( string [] args)
{
CConcreteFactory ConFactory = new CConcreteFactory();
IProduct TVProduct = ConFactory.CreateProduct( " TV " );
TVProduct.ProductName();
IProduct RadioProduct = ConFactory.CreateProduct( " Radio " );
RadioProduct.ProductName();
Console.Read();
}
}
}
(2) AbstractFactory模式主要是通过工厂类的不同的子类来实现不同类别的产品的创建。其C#示例代码如下所示:
namespace
AbstractFactory
{
class Program
{
public interface IProduct
{
void ProductName();
}
public class CRadio : IProduct
{
public void ProductName()
{
Console.WriteLine( " Radio " );
}
}
public class CTV : IProduct
{
public void ProductName()
{
Console.WriteLine( " TV " );
}
}
public interface IFactory
{
IProduct CreateProduct( string ProductName);
}
public class CRadioFactory
{
public IProduct CreateProduct()
{
return new CRadio();
}
}
public class CTVFactory
{
public IProduct CreateProduct()
{
return new CTV();
}
}
static void Main( string [] args)
{
CTVFactory TVFactory = new CTVFactory();
IProduct TVProduct = TVFactory.CreateProduct();
TVProduct.ProductName();
CRadioFactory RadioFactory = new CRadioFactory();
IProduct RadioProduct = RadioFactory.CreateProduct();
RadioProduct.ProductName();
Console.Read();
}
}
}
{
class Program
{
public interface IProduct
{
void ProductName();
}
public class CRadio : IProduct
{
public void ProductName()
{
Console.WriteLine( " Radio " );
}
}
public class CTV : IProduct
{
public void ProductName()
{
Console.WriteLine( " TV " );
}
}
public interface IFactory
{
IProduct CreateProduct( string ProductName);
}
public class CRadioFactory
{
public IProduct CreateProduct()
{
return new CRadio();
}
}
public class CTVFactory
{
public IProduct CreateProduct()
{
return new CTV();
}
}
static void Main( string [] args)
{
CTVFactory TVFactory = new CTVFactory();
IProduct TVProduct = TVFactory.CreateProduct();
TVProduct.ProductName();
CRadioFactory RadioFactory = new CRadioFactory();
IProduct RadioProduct = RadioFactory.CreateProduct();
RadioProduct.ProductName();
Console.Read();
}
}
}