How to interpret the composition connector in a class diagram?

Asked

Viewed 93 times

1

The teacher asked to make a relationship between parents, state and city. From what I understood of this relationship I made this diagram.

Meu diagrama

Only he said that the diamond side is on the wrong side and right would be that way:

Professor fez

Only I still don’t understand why it’s this way. Can someone help me, please?

  • 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.

  • 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, 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.

  • @Williamteixeira yes, I understood by your explanation, thank you, it helped me a lot.

1 answer

1


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

Exemplo 1

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:

  • first example:

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.

  • second example:

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);
    ...
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.