从设计模式的类型上来说,简单工厂模式是属于创建型模式,又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。
优点 工厂类是整个模式的关键.包含了必要的逻辑判断,根据外界给定的信息,决定究竟应该创建哪个具体类的对象.通过使用工厂类,外界可以从直接创建具体产品对象的尴尬局面摆脱出来,仅仅需要负责“消费”对象就可以了。而不必管这些对象究竟如何创建及如何组织的.明确了各自的职责和权利,有利于整个软件体系结构的优化。
缺点 由于工厂类集中了所有实例的创建逻辑,违反了高内聚责任分配原则,将全部创建逻辑集中到了一个工厂类中;它所能创建的类只能是事先考虑到的,如果需要添加新的类,则就需要改变工厂类了。 当系统中的具体产品类不断增多时候,可能会出现要求工厂类根据不同条件创建不同实例的需求.这种对条件的判断和对具体产品类型的判断交错在一起,很难避免模块功能的蔓延,对系统的维护和扩展非常不利; 这些缺点在工厂方法模式中得到了一定的克服。
使用场景 工厂类负责创建的对象比较少; 客户只知道传入工厂类的参数,对于如何创建对象(逻辑)不关心; 由于简单工厂很容易违反高内聚责任分配原则,因此一般只在很简单的情况下应用。
上面介绍了那么多,我给大家写个小例子。这里举的例子就是跳舞,每个舞蹈家都会跳舞、交流。加入每个舞蹈家必须在相应机构拿到资格证才能加入舞蹈协会,舞蹈协会有分为好多种例如拉丁协会、芭蕾协会等。但是每种舞蹈又会有很大的差别,为了模拟这个功能。我随意写了一段C#程序给大家做参考
1.定义2个接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace factory.org.lxh.dao
{
public interface Dancer
{
//每个舞者都会跳舞,交流
void dance();
void communicate();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace factory.org.lxh.dao
{
public interface DanceInstitute
{
Dancer regist();
}
}
2.接口的实现类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using factory.org.lxh.dao;
namespace factory.org.lxh.impl
{
public class BalletDancer:Dancer
{
public void dance()
{
//throw new NotImplementedException();
Console.WriteLine("我是一只翩翩起舞的天鹅");
}
public void communicate()
{
//throw new NotImplementedException();
Console.WriteLine("我们经常讨论有关磋步的东西");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using factory.org.lxh.dao;
namespace factory.org.lxh.impl
{
class LatinDance : DanceInstitute
{
public Dancer regist()
{
return new Latin();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using factory.org.lxh.dao;
namespace factory.org.lxh.impl
{
public class BalletDancer:Dancer
{
public void dance()
{
//throw new NotImplementedException();
Console.WriteLine("我是一只翩翩起舞的天鹅");
}
public void communicate()
{
//throw new NotImplementedException();
Console.WriteLine("我们经常讨论有关磋步的东西");
}
}
}
3.下面的就是工厂类了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using factory.org.lxh.dao;
namespace factory.org.lxh.impl
{
class LatinDance : DanceInstitute
{
public Dancer regist()
{
return new Latin();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using factory.org.lxh.dao;
namespace factory.org.lxh.impl
{
class BeautifulDance : DanceInstitute
{
public Dancer regist()
{
return new BalletDancer();
}
}
}
4.测试类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using factory.org.lxh.dao;
using factory.org.lxh.impl;
namespace factory
{
class Program
{
private static Dancer d1;
private static Dancer d2;
static void Main(string[] args)
{
DanceInstitute d = new LatinDance();
d1 = d.regist();
d2 = d.regist();
d1.dance();
d1.communicate();
//d2.dance();
// d2.communicate();
Console.ReadKey();
}
}
}
很简单吧,下面来看下运行效果