什么时候工厂方法比简单工厂更好? [英] When is factory method better than simple factory and vice versa?

查看:88
本文介绍了什么时候工厂方法比简单工厂更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照《 Head First设计模式》一书行事.

Working my way through the Head First Design Patterns book.

我相信我了解简单工厂和工厂方法,但是我很难知道工厂方法带来的好处是简单工厂.

I believe I understand the simple factory and the factory method, but I'm having trouble seeing what advantages factory method brings over simple factory.

如果对象A使用简单的工厂创建其B对象,则客户端可以按以下方式创建它:

If an object A uses a simple factory to create its B objects, then clients can create it like this:

A a = new A(new BFactory());

如果对象使用工厂方法,则客户端可以这样创建它:

whereas if an object uses a factory method, a client can create it like this:

A a = new ConcreteA(); // ConcreteA contains a method for instantiating 
                       // the same Bs that the BFactory above creates, with 
                       // the method hardwired into the subclass of A, ConcreteA.

因此,在简单工厂的情况下,客户将A与B工厂组成,而通过factory方法,客户将为其想要的B类型选择适当的子类.

So in the case of the simple factory, clients compose A with a B factory, whereas with the factory method, the client chooses the appropriate subclass for the types of B it wants.

在它们之间似乎没有太多选择.您要么必须选择要与哪个BFactory组成A,要么必须选择正确的A子类为您提供B.

There really doesn't seem to be much to choose between them. Either you have to choose which BFactory you want to compose A with, or you have to choose the right subclass of A to give you the Bs.

在什么情况下哪个比另一个更好?

Under what circumstances is one better than the other?

谢谢!

对于IMO来说,更令人困惑,因为它们在Head First叙述中从简单工厂过渡到工厂方法,方法是说(第119页)特许经营权是使用您的[简单]工厂制造比萨饼,但是在剩下的过程中开始采用他们自己的程序:他们会以不同的方式烘烤东西."并且他们有一张厨师的照片,显然对他的比萨做了些令人讨厌的事情.

Adding a little to the confusion IMO is the explanation given in the Head First narrative where they transition from simple factory to factory method by saying (p.119) "franchises were using your [simple] factory to create pizzas, but starting to employ their own home-grown procedures for the rest of the process: they'd bake things a little differently.." and they have that picture of a chef who apparently did something disgusting to his pizzas.

但是,使用简单的工厂为客户提供访问bake()方法或过程中任何其他部分的方法都没有.而且,如果有任何问题,使用工厂方法也无济于事.

But there's nothing about using a simple factory that gives clients access to the bake() method or any of the other parts of the process. And there's nothing about using a factory method that would help if there were any problem with it.

所以在我看来,Head First暗示在简单工厂上使用工厂方法的原因是虚假的.

So it looks to me like the reason Head First imply for using a factory method over a simple factory is bogus..

推荐答案

分析不同角色的差异:A的用户和A的提供者.这些模式对这些角色有不同的含义,随着情况的变化,这些含义很重要.时间.

Analyse the differences in terms of different roles: users of A and providers of A. The patterns have different implications for these roles, these are significant as things change over time.

对于

A myA = new SomeConcreteA()

A的客户对B甚至存在的事实一无所知.特定混凝土A的选择可能受到某些文档的影响,该文档使用特定的B或A系列的某些其他功能. A的提供者负责创建混凝土A的所有风味,因此当B的种类发生变化时,他们很可能需要做的工作.

the clients of A know nothing about the fact that Bs even exist. The choice of a particular concrete A might be influenced by some documentation that it uses particular Bs or some completely other features of the A family. The providers of A take responsibility for creating all the flavours of concrete A, so they are likely to have work to do when things such as kinds of B change.

对于

A myA = new A(myBFactory())

客户现在需要了解B及其工厂,并且可以完全控制要使用的工厂.提供者给了客户更多的责任,我们之间的耦合可能增加了-客户代码现在明确地至少依赖于一个类.当新的B出现时,A的提供者无事可做.

The client now needs to know about Bs and their factories and has complete control over which factory to use. The provider hs given the client much more responsibility, and we have perhaps increased coupling - the client code now explicitly depends on at least one more class. The provider of A has no work to do when new kinds of B come along.

请注意,我们正在注入B工厂,因此在编写客户端单元测试时,我们现在可以更轻松地为B提供模拟,因此测试客户端往往会更容易.总的来说,我发现出于这个原因,我会更频繁地使用这种注射方式.

Note that we are injecting the B factory and so when writing client unit tests we can now much more easily provide mocks for Bs, so testing the client tends to be easier. In general I find that I use this injection appraoch more frequently for this reason.

这篇关于什么时候工厂方法比简单工厂更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆