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.
Lojasis 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 oflojasat any instance ofCentroComercialwho has this list?– skidils
You need to explain better what you want. So that it is not possible to modify Arraylist
lojasit 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
CentroComercialbut must be implemented by composition. so that if I modify the list in any way in another class, any instance ofCentroComercialdid 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
lojais also immutable.– ramaral
That constructor that accepts a Centrocomercial would perhaps be preferable to replace it with an implementation of the method
clone()– ramaral