Composition
is a strong all-party relationship. It indicates that the whole is composed of the parts. The tip of the arrow indicates what is the whole in the relationship. In practice, the class whole keeps the reference for the class part.
Here in the documentation it is clearer the use
We have that a folder (Folder) is composed of files (File).
Problem
Your teacher probably got confused. It takes a lot of attention to correctly interpret the diagrams. Analyzing the ones you sent:
A country is composed of states that are composed of cities. Implemented as:
public class Pais {
...
// um pais pode ter varios estados
private ArryList<Estado> estados = new ArrayList<>();
...
}
public class Estado {
...
// um estado pode ter varias cidades
private ArryList<Cidade> cidades = new ArrayList<>();
...
}
public class Cidade {
...
...
}
Here we want that in order to create a country, there must be at least one state which implies the existence of at least one city.
A city is composed of at least one state that is composed of at least one country. Implemented as:
public class Pais {
...
...
}
public class Estado {
...
// um estado possui apenas um país
private ArryList<Pais> paises = new ArrayList<>(1);
...
}
public class Cidade {
...
// um cidade possui apenas um estado
private ArryList<Estado> estados = new ArrayList<>(1);
...
}
You have to be aware of the meaning of the diamond and the difference between it being on one side or the other. Note that A country has many states and a state is located in a single country, a state has many cities and a city is located in a single state.
– anonimo
from what I understand, the diamond is on the side of the whole class. A country contains several states, so the diamond would be on the side of the country class.
– priscylam
@priscylam, what determines the side of the diamond is the requirement, it will command. But you should keep in mind that the side on which she appears that will reference the relationship. If you send it as written it is better to interpret the problem. In my reply I sent the two possible interpretations and explained how each of them is implemented.
– William Teixeira
@Williamteixeira yes, I understood by your explanation, thank you, it helped me a lot.
– priscylam