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 methodget
which will be executed. Thus, the propertyCPF
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 methodsget
andset
as public.– Woss
Thanks Anderson, clarified very much of my doubts! Hug.
– T. Borges
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).
– Woss
@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.
– gato