自定义容器的工厂方法是否应该返回新创建的实例?

发布于 2024-07-26 15:53:15 字数 511 浏览 4 评论 0原文

我有一个按键索引的自定义集合 Doohickeys。 该集合有一个工厂方法 createDoohickey(key) 和一个访问器 Doohickey(key)createDoohickey(key) 应该返回新对象还是应该返回 void

在第一种情况下,我会像这样使用它,

myDoohickey = doohickeys.createDoohickey(key);
doStuff(myDoohickey);

在另一种情况下,

doohickeys.createDoohickey(key);
doStuff(doohickeys(key));

您认为哪种更可取,为什么?

编辑我相信这些年来我学到了很多东西,而且我接受的答案实际上并不是最好的答案。

I have a custom collection Doohickeys indexed by keys. This collection has a factory method createDoohickey(key) and an accessor Doohickey(key). Should createDoohickey(key) return the new object or should it return void?

In the first case, I would use it like this

myDoohickey = doohickeys.createDoohickey(key);
doStuff(myDoohickey);

in the other case like this

doohickeys.createDoohickey(key);
doStuff(doohickeys(key));

Which one would you consider preferable, and why?

EDIT I believe I have learned quite a bit over the years and that the answer I accepted is actually not really the best one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酷遇一生 2024-08-02 15:53:15

我不确定容器应该有一个工厂方法来创建它包含的对象 - 良好的关注点分离是将对象创建与容器分开。

因此,我认为你应该有这样的东西:

Doohickey myDookickey = new Doohickey();
doohickys.Add(key, myDookickey);
doStuff(myDookickey); // or
doStuff(doohickeys(key));

然后你可以自由地使用单独的对象工厂或直接实例化Doohickey。 这使得使用模拟 Doohickey 对象对容器进行单元测试变得更加容易。

如果你必须有工厂方法,我会返回Doohickey,因为在不需要时忽略返回值(假设返回了引用/指针)可能比在需要时使用索引来检索它更便宜。

I'm not sure the container should have a factory method to create the object it contains - good separation of concerns would be to separate object creation from the container.

Therefore, I think you should have something like this:

Doohickey myDookickey = new Doohickey();
doohickys.Add(key, myDookickey);
doStuff(myDookickey); // or
doStuff(doohickeys(key));

You are then free to use a separate object factory or instantiate Doohickey directly. This makes it easier to unit test the container with mock Doohickey objects.

If you must have the factory method, I would return the Doohickey, because it is probably cheaper to ignore the return value (assuming a reference/pointer is returned) when it is not needed than to use the index to retrieve it when it is.

孤千羽 2024-08-02 15:53:15

是的,工厂应该返回新创建的实例。
然而,就像亚历克斯指出的那样,您可以将存储库和工厂分开。 我很可能会设计工厂来引用存储库。 我不认为只要工厂方法实际返回新创建的实例,将两者组合成一个类就意味着世界末日。

Yes, a Factory should return the newly created instance.
However like Alex pointed out you could seperate the Repository and the Factory. I would most likely design the factory to have a reference to the repository. I don't think it would be the end of the world of you combined the two into one class just as long as the factory method actually returns the newly created instance.

月寒剑心 2024-08-02 15:53:15

上面的示例将工厂方法嵌入到容器类中。 我同意 willcodejavaforfood 的观点,从长远来看,这可能是也可能不是一个好主意。 问题在于,将工厂拆分成自己的类来减少耦合是否值得付出额外的成本。

至于为什么工厂方法应该返回新创建的实例,最好将创建与包含分离。 该课程的某些用户可能需要其中之一,但不需要另一个。 此外,当您将创建和包含结合起来时,您将无法再在不测试包含的情况下(单元)测试创建。

The example above embeds the factory method in the container class. I agree with willcodejavaforfood, and this may or may not be a good idea over the longer term. The issue is whether the reduced coupling of splitting the factory into its own class is worth the extra cost.

As far as why a factory method should return the newly created instance, it is preferable to decouple creation from containment. Some users of the class might want one but not the other. Also, when you couple creation and containment you can no longer (unit) test creation without also testing containment.

ま柒月 2024-08-02 15:53:15

这个答案来自我的一个同事。 他建议让 createDoohickey(key) 确实返回 Doohickey; 另外,也可以将其称为 getDoohickey(key)

编辑我相信,根据我所学到的知识,这个答案不一定是最好的答案。

This answer is from a colleague of mine. He suggested to have createDoohickey(key) indeed return the Doohickey; and, additionally, to call it getDoohickey(key) instead.

EDIT I believe after what I have learnt since that this answer is not necessarily the best one any more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文