java - Building big, immutable objects without using constructors having long parameter lists -


I have some large (more than 3 field) objects that can be irreversible and may be every time I In case I run, I make Constructor's abominations with long list of parameters. It does not seem right, it is hard to use and readability suffers.

This field is worse than some type of archive type, such as lists, a simple add sibling (s) makes the object creation very less, but makes the object unstable.

What do you guys use in such cases? I am on Scala and Java, but I think the problem language is agnostic, unless language is not oriented.

The solution I can think is:

  1. "constructor
  2. builder pattern

thanks for your input !

Good, do you want that once easy to read and irreversible thing is both ready ?

I think a fluent interface corrected will help you.

It looks like this (purely created example):

  Final Fu Renewal Y = FooFactory.create () .whereRangeConstraintsAre (100,300) .withcolor (Color.BLUE) .withArea (234) .withInterspacing (12) .build ();  

I'm in bold "Done correctly" because most Java programmers wrong interface and corrupt your object with the method required to create an object, which is absolutely wrong.

The trick is that the build () method actually makes a fu (so you can be FU unchanging).

FooFactory.create () , Where XXX (..) and with XXX (..) all create "something else".

This may be some more FooFactory, here's a way to do this ....

You will see the foofactors like this:

  // Private FooFritter Manufacturer Personal Notice FooFactory () {} Create Public FooFactory Public () {New FooFactory Returns}; } With the public FooFactory color (final color color) {this.color = color; This return; } Public Fu Build () {Return new FooImpl (color, and, all, others, parameters, here); }  

Comments