这是一种模式、一个坏主意还是您知道如何改进?
给定一个像这样的类层次结构: 包树 有一个帮助创建对象的类是个好主意吗? 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看起来并不是特别复杂。 在选择之前,您可能需要研究一下如何进行单元测试——工厂会让正确注入模拟变得非常困难。
另外,我还没有看到其他人列出依赖注入——我也会研究 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.
类层次结构没有说明是否需要工厂方法或任何其他专门的方法来创建对象。 你想用这些课程来做什么? 使用起来会很困难吗? 延期会不会很困难?
创建工厂或类似的设施是否会改善这种情况?
如果您无法下定决心,请创建一些使用类的示例(请使用现实的示例),然后重构它们以使用您的工厂。
练习之后,您应该能够自己做出决定。 或者带着一些更具体的问题回来。
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.
对象创建有两种或三种模式。 最常见的是工厂,它似乎适用于这个层次结构。 我不太记得其他的(也不记得有多少),但我认为另一个称为原型。
如果您关心这些事情,请阅读有关设计模式的书。
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.
它们实际上是一种设计模式——创造模式。 维基百科对这些模式类型有合理的描述 - 源自 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
对象创建的模式包括
Builder
、Factory
和Prototype
。 维基百科有一个相当详细的列表。Patterns for object creation include
Builder
,Factory
, andPrototype
. Wikipedia has a fairly detailed list.