Doubt visibility of attributes in class diagram

Asked

Viewed 192 times

2

We recently had a discussion in class on the question of the visibility of attributes in the class diagram. Some have stated that in the diagram (as well as in the code) all attributes must be private. However, the teacher made the following observation: in the class diagram, we represent the visibility of the attribute PROPERTY. Here is an example (purely didactic):

    private string _CPF;

    **public** string CPF {
        get { return _CPF; }
        private set
        {
            if (value.Length == 11)
                _CPF = value;
        }
    } 

In this case, according to him, the correct one would put the attribute (in the diagram) as public (because the property is public (in bold)). However, in this case, the get is public and the set therefore, as I represent the visibility of the attribute _CPF in the class diagram?

All the materials I’ve read tell me to put all the attributes in the private diagram, so I should use them all as private or should I put the visibility of the property?

  • I believe this is a peculiarity of C# (it is the one used in the question, right?) that allows the definition of properties. That is, you can do cpf = obj.CPF, as if accessing an attribute directly, but in fact it is the method get which will be executed. Thus, the property CPF acts (so to speak) as a public attribute and indicates the attribute _CPF as private would be redundancy. Languages that do not support this, like PHP, the attribute should be put in the diagram as private and its methods get and set as public.

  • Thanks Anderson, clarified very much of my doubts! Hug.

  • I didn’t put it as an answer because I don’t think I have the authority to answer the question clearly, especially in C#. If no one answers (decently), I draw up an answer (if what I say makes sense).

  • @T.Borges take a look here to learn about attribute x property. If the property is public you can let it publish in your class diagram, remembering that a property can be read-only published in some cases.

2 answers

1

I believe that if you wish your property to be publicly accessed, the diagram should be marked as public. As best practices, the set should always be private (or protected use inheritance). Having the set as private, you ensure that some business rule is executed before set the property, avoiding validation error.

-1

A class diagram is an abstraction of how things are going to be implemented. The amount of detail depends on the tool used and does not always (in fact almost never) accurately represent how the code will actually be built.

That said: given a team of X people who will work on a diagram with Y classes, the amount of different interpretations for the same diagram will have the same order as Xy (before we cut the redundancies). Just from what you said I’ve been able to think of four possibilities:

  • His teacher understands that all attributes are private, and that the internal logics of the classes should not be represented. The diagram should contain only properties, which are public by definition;

  • Your teacher understands that all attributes should be represented as private, and all properties as public;

  • Idem above, but with read-only notation for the property;

  • Your teacher just wants to provoke an argument and see the consensus that the class reaches.

So I have two pieces of advice:

1-) Just as students and OS users have different views on the problem, different teachers can give different answers as well. As the teacher is the authority in the classroom, he has Minerva’s vote on what is right. Consult him, and not the OS, to know what the answer is to correct for your classroom.

2-) Don’t worry too much about which answer is the most pedantically correct in this case, as later you will see that a class diagram is for the corresponding source code like this:

inserir a descrição da imagem aqui

Is for this:

inserir a descrição da imagem aqui

Browser other questions tagged

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