Doubt visibility of attributes in the class diagram

2

We recently had an in-class discussion on the issue of attribute visibility 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 PROPERTY of the attribute. 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 above, according to him, the correct would be to put the attribute (in the diagram) as public (because the property will be public (in bold)). However, in this case, get is public and set private, so how do I represent the visibility of the _CPF attribute in the class diagram?

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

    
asked by anonymous 13.04.2017 / 02:44

2 answers

1

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

    
03.07.2017 / 03:57
-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 not always (actually almost never) accurately represents 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 number of different interpretations for the same diagram will have the same order as X y (before we cut the redundancies). Just from what you said I've already been able to think of four possibilities:

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

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

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

  • Your teacher just wants to provoke a discussion and see the consensus that the class comes to.

So I have two advices:

1 -) Just as OS students and 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. Check it, not the OS, to find out which answer is the correct for your classroom.

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

It'sforthis:

    
03.07.2017 / 15:11