简单工厂与工厂方法
Simple factory vs Factory method
简单工厂:
工厂方法:
大家好。我正在寻找简单工厂和工厂方法之间的区别。我知道结构差异(上图),但我无法理解用例的差异。例如,这是对工厂方法的解释:
In Factory Method pattern we will introduce a new interface called
‘IMobileFactory’ and two concrete implementation’s ‘NokiaFactory’ and
‘IphoneFactory’. These concrete classes control the object creation.
In my example the client want a Nokia object. So the steps are given
below.
1.The client will load a reference to ‘NokiaFactory’. But Client won’t refer the ‘NokiaFactory’ class directly like the Simple Factory
pattern. The client refers the concrete implementation through an
interface ‘IMobileFactory’.
2.Then the Client call the ‘CreateMobile()’ method that will return an object of type ‘IMobile’.
3.Here we have to inform the client the concrete implementation to be used through some configuration or parameters.
第一步看不懂:
But Client won’t refer the ‘NokiaFactory’ class directly like the
Simple Factory pattern. The client refers the concrete implementation
through an interface ‘IMobileFactory’.
所以客户这样写:
IMobileFactory factory = LoadFactory("NokiaFactory");
那为什么它有用而且更好呢?有什么好处?为什么我不应该只写这个:
NokiaFactory factory = new NokiaFactory();
或者那个呢:
IMobileFactory factory = new NokiaFactory();
您需要接口来避免为每种类型创建不同的流,对于 10 种类型,您将有 1 行代码而不是 10 行
IMobileFactory factory = loadFactory(myFactoryName);
而不是按类型:
NokiaFactory factory = new NokiaFactory();
...
IphoneFactory factory = new IphoneFactory();
...
使用 loadFactory
方法与 new NokiaFactory()
的不同之处在于,您不需要显式创建新对象,您将对象创建委托给 loadFactory
而 returns 相关对象
所以你的问题是关于比较这个设计 #1:
IMobileFactory factory = LoadFactory("NokiaFactory");
此设计#2:
NokiaFactory factory = new NokiaFactory(); // or:
IMobileFactory factory = new NokiaFactory();
如您所见,最大的区别在于,虽然设计 #1 中的客户端不知道任何具体类型,例如NokiaFactory
或IPhoneFactory
,设计 #2 中的客户端。
了解 NokiaFactory
或 IPhoneFactory
等具体事物的缺点应该是众所周知的。如果你想对这些类型进行修改,比如你想给NokiaFactory
添加一个新的方法,而这个方法不是IMobileFactory
接口的一部分,那么客户端代码就会受到影响不必要。客户端不关心新方法,但它的代码必须是recompiled/redeployed.
更新
再多解释一下。
例如在NokiaFactory
class中添加了一个名为Foo
的新方法:
class NokiaFactory {
// old code
public void Foo() { ... }
}
Foo
不是 IMobileFactory
接口的方法而是添加到 NokiaFactory
因为有另一个客户端需要该方法,并且该客户端很乐意依赖NokiaFactory
class。换句话说,该客户会欢迎来自 NokiaFactory
class 的更改,但第一个客户不会。
从设计角度:
当Object类型不固定时使用SimpleFactory。例如:手机我的制造商 - 诺基亚,iPhone。也许你明天想添加一个新的制造商。
当对象的类型固定时,使用工厂方法。例如:按类型划分的电话 - 固定电话和移动电话。
最后,归结为您想要如何设计您的系统。你想要什么是可扩展的。
简单工厂:
工厂方法:
大家好。我正在寻找简单工厂和工厂方法之间的区别。我知道结构差异(上图),但我无法理解用例的差异。例如,这是对工厂方法的解释:
In Factory Method pattern we will introduce a new interface called ‘IMobileFactory’ and two concrete implementation’s ‘NokiaFactory’ and ‘IphoneFactory’. These concrete classes control the object creation.
In my example the client want a Nokia object. So the steps are given below.
1.The client will load a reference to ‘NokiaFactory’. But Client won’t refer the ‘NokiaFactory’ class directly like the Simple Factory pattern. The client refers the concrete implementation through an interface ‘IMobileFactory’.
2.Then the Client call the ‘CreateMobile()’ method that will return an object of type ‘IMobile’.
3.Here we have to inform the client the concrete implementation to be used through some configuration or parameters.
第一步看不懂:
But Client won’t refer the ‘NokiaFactory’ class directly like the Simple Factory pattern. The client refers the concrete implementation through an interface ‘IMobileFactory’.
所以客户这样写:
IMobileFactory factory = LoadFactory("NokiaFactory");
那为什么它有用而且更好呢?有什么好处?为什么我不应该只写这个:
NokiaFactory factory = new NokiaFactory();
或者那个呢:
IMobileFactory factory = new NokiaFactory();
您需要接口来避免为每种类型创建不同的流,对于 10 种类型,您将有 1 行代码而不是 10 行
IMobileFactory factory = loadFactory(myFactoryName);
而不是按类型:
NokiaFactory factory = new NokiaFactory();
...
IphoneFactory factory = new IphoneFactory();
...
使用 loadFactory
方法与 new NokiaFactory()
的不同之处在于,您不需要显式创建新对象,您将对象创建委托给 loadFactory
而 returns 相关对象
所以你的问题是关于比较这个设计 #1:
IMobileFactory factory = LoadFactory("NokiaFactory");
此设计#2:
NokiaFactory factory = new NokiaFactory(); // or:
IMobileFactory factory = new NokiaFactory();
如您所见,最大的区别在于,虽然设计 #1 中的客户端不知道任何具体类型,例如NokiaFactory
或IPhoneFactory
,设计 #2 中的客户端。
了解 NokiaFactory
或 IPhoneFactory
等具体事物的缺点应该是众所周知的。如果你想对这些类型进行修改,比如你想给NokiaFactory
添加一个新的方法,而这个方法不是IMobileFactory
接口的一部分,那么客户端代码就会受到影响不必要。客户端不关心新方法,但它的代码必须是recompiled/redeployed.
更新
再多解释一下。
例如在NokiaFactory
class中添加了一个名为Foo
的新方法:
class NokiaFactory {
// old code
public void Foo() { ... }
}
Foo
不是 IMobileFactory
接口的方法而是添加到 NokiaFactory
因为有另一个客户端需要该方法,并且该客户端很乐意依赖NokiaFactory
class。换句话说,该客户会欢迎来自 NokiaFactory
class 的更改,但第一个客户不会。
从设计角度:
当Object类型不固定时使用SimpleFactory。例如:手机我的制造商 - 诺基亚,iPhone。也许你明天想添加一个新的制造商。
当对象的类型固定时,使用工厂方法。例如:按类型划分的电话 - 固定电话和移动电话。
最后,归结为您想要如何设计您的系统。你想要什么是可扩展的。