使用动态 JVM 语言创建存根数据结构的示例?

发布于 2024-07-08 18:03:12 字数 556 浏览 4 评论 0原文

多年来,我想我已经看到并尝试了为复杂对象图生成存根数据结构(假数据)的所有可能方法。 在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 技术交流群。

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

发布评论

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

评论(2

霊感 2024-07-15 18:03:13

您可能会发现 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.

下壹個目標 2024-07-15 18:03:13

我通常使用构建器模式创建对象母体。

public class ItineraryObjectMother
{
    Status status;
    private long departureTime;

    public ItineraryObjectMother()
    {
        status = new Status("BLAH");
        departureTime = 123456L;
    }
    public Itinerary build()
    {
        Itinerary itinerary = new Itinerary(status);
        itinerary.setDepartureTime(departureTime);
        return itinerary;
    }
    public ItineraryObjectMother status(Status status)
    {
        this.status = status;
        return this;
    }
    public ItineraryObjectMother departs(long departureTime)
    {
        this.departureTime = departureTime;
        return this;
    }

}

然后可以这样使用:

Itinerary i1 = new ItineraryObjectMother().departs(1234L).status(someStatus).build();
Itinerary i2 = new ItineraryObjectMother().departs(1234L).build();

正如 Ted 所说,这可以使用动态语言来改进/简化。

I typically create object mothers using the builder pattern.

public class ItineraryObjectMother
{
    Status status;
    private long departureTime;

    public ItineraryObjectMother()
    {
        status = new Status("BLAH");
        departureTime = 123456L;
    }
    public Itinerary build()
    {
        Itinerary itinerary = new Itinerary(status);
        itinerary.setDepartureTime(departureTime);
        return itinerary;
    }
    public ItineraryObjectMother status(Status status)
    {
        this.status = status;
        return this;
    }
    public ItineraryObjectMother departs(long departureTime)
    {
        this.departureTime = departureTime;
        return this;
    }

}

Then it can be used like this:

Itinerary i1 = new ItineraryObjectMother().departs(1234L).status(someStatus).build();
Itinerary i2 = new ItineraryObjectMother().departs(1234L).build();

As Ted said, this can be improved/simplified with a dynamic language.

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