使用动态 JVM 语言创建存根数据结构的示例?
多年来,我想我已经看到并尝试了为复杂对象图生成存根数据结构(假数据)的所有可能方法。 在java中它总是变得毛茸茸的。
* * * *
A---B----C----D----E
(请原谅廉价的 UML)
关键问题是值之间存在一定的关系,因此 C 的某个实例可能暗示 E 的给定值。
我见过的任何尝试应用单个模式或一组模式来解决这个问题java最终会变得混乱。
我正在考虑 groovy 或任何动态虚拟机语言是否可以做得更好。 使用闭包应该可以使事情变得更加简单。
有人有任何用(最好是)groovy 或 scala 很好地解决这个问题的参考/例子吗?
编辑: 我不知道“对象母亲”是该模式的名称,但这是我遇到的麻烦:当对象母亲生成的对象结构足够复杂时,您总是会得到一个相当复杂的对象结构。对象母体本身内部的复杂内部结构(或通过组合多个对象母体)。 给定一个足够大的目标结构(比如 30 个类),找到实现对象母体的结构化方法确实很困难。 现在我知道了模式的名称,我可以更好地用谷歌搜索它;)
Over the years, I think I have seen and tried every conceivable way of generating stub data structures (fake data) for complex object graphs. It always gets hairy in java.
* * * *
A---B----C----D----E
(Pardon cheap UML)
The key issue is that there are certain relationships between the values, so a certain instance of C may imply given values for E.
Any attempt I have seen at applying a single pattern or group of pattens to solve this problem in java ultimately end up being messy.
I am considering if groovy or any of the dynamic vm languages can do a better job. It should be possible to do things significantly simpler with closures.
Anyone have any references/examples of this problem solved nicely with (preferably) groovy or scala ?
Edit:
I did not know "Object Mother" was the name of the pattern, but it's the one I'm having troubles with: When the object structure to be generated by the Object Mother is sufficiently complex, you'll always end up with a fairly complex internal structure inside the Object Mother itself (or by composing multiple Object Mothers). Given a sufficiently large target structure (Say 30 classes), finding structured ways to implement the object mother(s) is really hard. Now that I know the name of the pattern i can google it better though ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会发现 Object Mother 模式很有用。 我在当前的 Groovy/Grails 项目中使用了它来帮助我创建示例数据。
它不是特定于常规的,但动态语言通常可以更轻松地使用鸭子类型和闭包创建类似的东西。
You might find the Object Mother pattern to be useful. I've used this on my current Groovy/Grails project to help me create example data.
It's not groovy specific, but a dynamic language can often make it easier to create something like this using duck typing and closures.
我通常使用构建器模式创建对象母体。
然后可以这样使用:
正如 Ted 所说,这可以使用动态语言来改进/简化。
I typically create object mothers using the builder pattern.
Then it can be used like this:
As Ted said, this can be improved/simplified with a dynamic language.