Examples for creating stub data structures with dynamic JVM Languages?

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 ;)


Asked by: Alford925 | Posted: 21-01-2022






Answer 1

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.

Answered by: Madaline743 | Posted: 22-02-2022



Answer 2

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.

Answered by: Miller441 | Posted: 22-02-2022



Similar questions

java - Which languages support Lenses or similar way to update immutable nested structures?

While immutability praised by many, I found it hard to maintain in mainstream programming. In my experience, programmers sooner than later will make fields mutable again to avoid refactoring large piece of code that would have to pass updated object along with return value. Scala has some support with copy constructors but I know no satisfactory solution for updating complex object structures. I might have missed s...


data structures - Where to get more information on Dictionary ADT and Skip List for Java?

I'm trying to go deep into Dictionary ADT and Skip List for Java. My textbook doesn't cover a lot about this and whatever it has covered is very complicated. Which is the best online site to get more information on Dictionary ADT and Skip List for Java. I'm looking for the one which talks visually and gives a lot of examples.


java - Data structures in JDK, under what scenario which one to use?

Closed. This question does not meet Stack Overflow guid...


data structures - Efficient persistent storage for simple id to table of values map for java

I need to store some data that follows the simple pattern of mapping an "id" to a full table (with multiple rows) of several columns (i.e. some integer values [u, v, w]). The size of one of these tables would be a couple of KB. Basically what I need is to store a persistent cache of some intermediary results. This could quite easily be implemented as simple sql, but there's a couple of problems, namely I need to co...


sql - Querying Java Data Structures

Is there any way to perform SQL Like Queries or Filtering on Java Data Structures? I want to filter objects in an ArrayList and a HashMap by fields of the objects contained within.


Fastest way to compare two data structures in java

I would like to know what is the fastest way in java 1.5 to compare two data structures. My data structure represents a tree that can be pretty big. I can traverse the whole data structure and compare the 2 node by node (which I guess will be slow). Or I can compute a hash of the data structure to do it faster, right ? What is the best (efficient and not too long) way to compute this hash ? I would...


java - JAXB: How should I marshal complex nested data structures?

I have several complex data structures like Map< A, Set< B > > Set< Map< A, B > > Set< Map< A, Set< B > > > Map< A, Map< B, Set< C > > > and so on (more complex data structures) Note: In my case it doesn't really matter if I use Set or List. Now I know that JAXB let me define XmlAdapter's, that's ...


java - Regex for tree structures?

Are there regular expression equivalents for searching and modifying tree structures? Concise mini-languages (like perl regex) are what I am looking for. Here is an example that might clarify what I am looking for. <root> <node name="1"> subtrees .... </node> <node name="2"> <node name="2.1"> data </node> other subtrees... </node&gt...


java - array of structures, or structure of arrays?

Hmmm. I have a table which is an array of structures I need to store in Java. The naive don't-worry-about-memory approach says do this: public class Record { final private int field1; final private int field2; final private long field3; /* constructor & accessors here */ } List<Record> records = new ArrayList<Record>(); If I end up using a large number (> 106


java - How do I print the class structures in a jar file using the javap tool?

I want to list the methods of the class files in the jar using the javap tool. How do I do it so that it lists the methods and members of all the class files in the jar. Right now I am able to do it for just one class at a time. I am expecting something like if I say javap java.lang.* it should enlist the methods and members of all the classes in java.lang package. If javap is not ...


Java Data Structures Reference

Can anyone give me references of a web site containing a summary of the main Java data structures, and their respective complexity in time (for some given operations like add, find, remove), e.g. Hashtables are O(1) for finding, while LinkedLists are O(n). Some details like memory usage would be nice too. This would be really helpful for thinking in data structures for algorithms.






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top