Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
If you do not mind having so much code on a single line, the builder pattern might be useful here. Something along the lines: Note: the example is adapted based on an implementation I have done in...
Answer
#1: Initial revision
If you do not mind having so much code on a single line, [the builder pattern](https://howtodoinjava.com/design-patterns/creational/builder-pattern-in-java/) might be useful here. Something along the lines: Note: the example is adapted based on an implementation I have done in a .NET and deviates from the canonical example shown in the reference. ### Define the builder ```java public static class FooBuilder { private final Foo foo; public FooBuilder() { this.foo = new Foo(); } public FooBuilder withX(int x) { this.foo.setX(x); } public Foo build() { return foo; } } ``` ### Usage `new FooBuilder().withX(42).build().getY()`. The advantage of this pattern is that when `Foo`'s state gets larger, it is pretty easy to maintain the tests, especially when you need to build objects without setting all the properties (e.g. maybe X becomes irrelevant for some tests and only Y and Z are relevant).