这是一种模式、一个坏主意还是您知道如何改进?

发布于 2024-07-29 10:42:10 字数 263 浏览 2 评论 0原文

给定一个像这样的类层次结构: 包树 有一个帮助创建对象的类是个好主意吗? Ei 是一个类,其方法对应于可以创建的每个类(方法 equals() 返回一个 CompareCriteria,例如)。 我看到的优点是它会隐藏复杂的继承结构并减少依赖关系。

Given a class hierarchy like this one:
a package tree
would it be a good idea to have a class that helps in creating the objects? E.i. a class with methods that correspond to each class that can be created (method equals() returns a CompareCriteria eg).
The advantage I see is that it would hide away the complex inheritance structure and dependencies would be reduced.

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

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

发布评论

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

评论(5

自此以后,行同陌路 2024-08-05 10:42:41

看起来并不是特别复杂。 在选择之前,您可能需要研究一下如何进行单元测试——工厂会让正确注入模拟变得非常困难。

另外,我还没有看到其他人列出依赖注入——我也会研究 DI 工具包,因为它将解决单元测试问题以及与常见构建模式相关的其他一些问题。 看起来你的一些类是建立在其他类的基础上的(就像你可以将它们组合起来来解决问题),DI 在这方面有很大帮助。

Doesn't seem terribly complicated. You might want to look into how you are going to do your Unit Testing before you choose--factories can make it really hard to properly inject mocks.

Also I haven't seen anyone else list Dependency Injection--I'd look into a DI toolkit as well because it will solve the unit testing problem and a few others associated with common building patterns. It looks like some of your classes build on others (like you might combine them to solve a problem), and DI helps with that a lot.

猥︴琐丶欲为 2024-08-05 10:42:36

类层次结构没有说明是否需要工厂方法或任何其他专门的方法来创建对象。 你想用这些课程来做什么? 使用起来会很困难吗? 延期会不会很困难?

创建工厂或类似的设施是否会改善这种情况?

如果您无法下定决心,请创建一些使用类的示例(请使用现实的示例),然后重构它们以使用您的工厂。

练习之后,您应该能够自己做出决定。 或者带着一些更具体的问题回来。

The class hierachy says nothing about the need for a Factory Method or any other specialized way to create objects. What do you want to do with all these classes? Will it be difficult to use? Will it be difficult to extend?

Does creating a Factory or similar improve the situation?

If you can't make up your mind, create some examples of using the classes (realistic examples please) then refactor those to use you Factory.

After that exercise you should be able to decide for yourself. Or to come back with some more specific problems.

揽清风入怀 2024-08-05 10:42:32

对象创建有两种或三种模式。 最常见的是工厂,它似乎适用于这个层次结构。 我不太记得其他的(也不记得有多少),但我认为另一个称为原型。

如果您关心这些事情,请阅读有关设计模式的书。

There are two or three patterns for object creation. The most common is the factory, which seems applicable in this hierarchy. I don't quite recall the other ones (nor how many there are), but I think another one is called prototype.

If you care about such things, get the book on design patterns.

说不完的你爱 2024-08-05 10:42:28

它们实际上是一种设计模式——创造模式。 维基百科对这些模式类型有合理的描述 - 源自 GoF 书:

http://en.wikipedia .org/wiki/Creational_pattern

  • 抽象工厂模式
  • 工厂方法模式
  • 构建器模式
  • 延迟初始化模式
  • 对象池
  • 原型模式
  • 单例模式

They are actually a type of design pattern - creational patterns. Wikipedia has a reasonable description of these pattern types - which originate from the GoF book:

http://en.wikipedia.org/wiki/Creational_pattern

  • Abstract factory pattern
  • Factory method pattern
  • Builder pattern
  • Lazy initialization pattern
  • Object pool
  • Prototype pattern
  • Singleton pattern
一直在等你来 2024-08-05 10:42:24

对象创建的模式包括BuilderFactoryPrototype。 维基百科有一个相当详细的列表

Patterns for object creation include Builder, Factory, and Prototype. Wikipedia has a fairly detailed list.

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