1
I understand how to compose but I’ve never done with lists, although the principle should be the same, I don’t understand how to do.
For starters I have a class CentroComercial
who owns a ArrayList
calling for lojas
which will pass as attribute in construtor
.
Lojas
is a ArrayList
which stores objects of the type Loja
. this super-class is divided into several subclasses. And I have two doubts.
How should I do the
public void setLojas(ArrayList<Loja> lojas) {}
what is the best way to instill the Arraylist?
private ArrayList<Loja> lojas;
private ArrayList<Loja> lojas = new ArrayList<Loja>();
or otherwise until?
but this ensures that stores are by composition? I modifying
lojas
, do not change the list oflojas
at any instance ofCentroComercial
who has this list?– skidils
You need to explain better what you want. So that it is not possible to modify Arraylist
lojas
it will have to be passed to the builder and the methodsetLojas()
cannot exist. If so say that I add to the answer.– ramaral
The Arraylist can and must be in the constructor, is an attribute of
CentroComercial
but must be implemented by composition. so that if I modify the list in any way in another class, any instance ofCentroComercial
did not change his list. this is the code I have and I’m having doubts. https://snag.gy/ifSbpP.jpg– skidils
Now I understand. I think its implementation is correct since it keeps a new instance and also returns a new instance. You just have to make sure that an object
loja
is also immutable.– ramaral
That constructor that accepts a Centrocomercial would perhaps be preferable to replace it with an implementation of the method
clone()
– ramaral