How to construct a Class whose variables have almost same value for majority of the objects

Nisantasi :

What is the best way to construct an Object which has only one variable being changed for multiple invocations.

public class Brand {

    final private Long id;
    final private String code;
    final private String version;
    final private String type;
    final private List<String> listThatChanges;
}

Most of the times I have to change the listThatChanges and put the object inside another Collection. Im thinking having a four argument constructor like below

   public Brand(id,code,version,type) {
        this.id = id,
        this.code = code;
        this.version = version;
        this.type = type;
    } 

and a setter for listThatChanges, but this doesn't help me in any memory Optimization as I have to create a new Object anyways.Is there a better way to efficiently create a Class so that dont have reconstruct the same(except one attribute) Object multiple times

Sean Patrick Floyd :

First of all: don't optimize unless you have a need to.

Second:

If only one part changes, then that should in fact be the only thing that change, and you can reflect that by embracing composition and introducing a wrapper object.

class BrandAttributes{
  final Brand brand; // this object always stays the same
  final List<String> attributes; // this one changes
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=400482&siteId=1