Examples of creating stub data structures with dynamic JVM languages?

Over the years, I think I've seen and tried every conceivable way to create stub data structures (fake data) for complex object graphs. There is always hairy in java.

   *    *    *    *
A---B----C----D----E

      

(Pardon cheap UML)

The key issue is that there is a certain relationship between values, so a specific instance of C can mean set values ​​for E.

Any attempt I've seen using a single pattern or group of patterns to solve this problem in java ends up being a mess.

I am considering if groovy or any of the dynamic vm languages ​​might perform better. It should be possible to make things much easier with a closure.

Does anyone have any links / examples of this problem that were solved nicely (preferably) by groovy or scala?

Edit: I didn't know that "Object Mother" is the name of the template, but this is the one with which I am having problems: when the structure of the object that will be created by the matter of the object is complex enough, you will always find yourself a rather complex internal structure within the matter itself object (or by creating multiple parent objects). Given the fairly large target structure (say, 30 classes), it is really difficult to find structured ways to implement the object's motherhood (s). Now that I know the template name I can google better though;)

+1


source to share


2 answers


You can find Object Mother template . I used this in my current Groovy / Grails project to help me create sample data.



It's not groovy specific, but a dynamic language can often make it easier to create something like this with duck input and closure.

+2


source


I usually create parent objects 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.

+1


source







All Articles