Let's think about this class, where ImmutablePart
is an immutable pure data Object
:
public class Clazz1 {
private final ImmutablePart immu ;
private String f1;
private boolean f2;
private int f3;
/** some constructor building up the immu and
setting the fields to init values
...
...
**/
public ImmutablePart getImmu(){return immu;}
public String getF1(){return f1;}
public boolean getF2(){return f2;}
public int getF3(){return f3;}
public void setF1(String f1){this.f1=f1;}
public void setF2(boolean f2){this.f2=f2;}
public void setF3(int f3){this.f3=f3;}
}
Is it good practice to use composition at this stage, to make it more clear that one part is mutable, the other immutable, or is it just boilerplate? Like this (with MutablePart
having getters and setters for the fields of Clazz1
).
public class Clazz2 {
private final ImmutablePart immu ;
private final MutablePart mutable;
/** some constructor building up the two final objects
...
...
**/
public ImmutablePart getImmu(){return immu;}
public MutablePart getMutable(){return mutable;}
}
EDIT1 : okay to be more specific, the immutable part is build from a file containing some sort of metainfo, and therefore is not meant to change. The mutable part will evolve regarding runtime. Let's imagine I'm working on a p2p application. The immutable part will contain all informations regarding the set of files my client will share with peers, and some hashkey to insure the authenticity of received packets. Whereas the mutable part will contain informations regarding the state of the sharing, ie some states, number of bytes uploaded, number of bytes downloaded etc... The immutable part could easily be called "metaInfo", and the mutable part "context", if you want more meaningful names. But my question was more about the utility of such design. Thanks for your answers!
EDIT2 : the names I have chosen are just here for abstraction purpose, I would give better names in real implementation. That is to say, and I agree it might be confusing at first glance, that by saying "explicit" I mean a harsh distinction between both part into two different objects, not naming explicitly one part "Immutable" and the other part "mutable".