26

I am required to draw a class diagram for my JSF application for a project documentation. So I have lots of classes as managed beans, with many attributes therefore many getters and setters.

When I draw the class diagram should I also include the getters & setters in the diagram or can I simply leave them?

Skully
  • 2,882
  • 3
  • 20
  • 31
Selvin
  • 12,333
  • 17
  • 59
  • 80

5 Answers5

20

It wouldn't be appropriate to include them. You can just add one line saying accessors methods

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    Could you explain who would you add that line? I'm inmersed in a UML and don't get the whole idea. –  Mar 23 '14 at 07:49
  • @JigarJoshi When I added accessors method, it automatically passed in an entity object as parameter. How do I fix this – QWERTY Apr 01 '16 at 02:38
  • 3
    If you are using a tool the best way would be to add a comment box such as "common accessors methods are omitted". – atorres Jan 12 '17 at 11:27
7

Including getters and setters would be a bad idea. They are wasting "real estate" to duplicate information that is already shown in the attribute / property section of the class.


Other answer suggest that the UML diagram needs to document "unusual" visibility of Java getters and setters, or "special" behavior in getters and setters.

I guess in some cases that could be justified. However, I would counter that:

  • A UML diagram doesn't need to show everything. Only the important things. Indeed, one of the signs of a good UML diagram is that it isn't cluttered up with unimportant things. So these details should only be included if they are really important.

  • The fine details of the abstraction boundaries are generally not the concern of the design. A Java programmer should just know the basics of how to implement abstraction / encapsulation when it is needed. Furthermore, the programmer will most likely have a better insight into situations where "porous" abstraction boundaries are needed; e.g. for performance reasons. (UML is not designed to express that kind of thing.)

  • The precise behavior of fields and methods is generally not the concern of the UML design documents. (Unless the designer is also going to go to the length of specifying methods' preconditions, postconditions and invariants in OCL!) However, if a UML diagram needs to say that a field can never be null, or that getting a field increments a counter, you should be able to describe that as comments (or OCL constraints) on the field.

Finally, the UML diagram should not be the only technical documentation for the software. The javadocs automatically document the access modifiers / visibility of methods and fields. Likewise, if the programmer has implemented getters and setters with "special" behavior that needs documenting, this should be described in the javadoc comments.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
3

You should not include getters and setters in your diagram until they do something special: null checking and so on. But it is a sign of bad design, so general answer is "No, you should not".

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • `until they do something special: null checking and so on. But it is a sign of bad design` did you mean is it bad to check for null in setter/getter ? – jmj Mar 09 '11 at 12:57
  • 1
    in some cases not the null check is bad but the way it is used. Suppose you have the following method in a hibernate entity: getPerson() { if (person == null) person = new Person(); return person; }. This method implicitly creates an entity, while it is not supposed to do this. – Vladimir Ivanov Mar 09 '11 at 13:02
  • 1
    yeah , it certainly depends on design and business need. – jmj Mar 09 '11 at 13:05
0

UML is a rather informal notation, the best would be to first set the rules you going to use at your project or organization. For instance, it is usual to hide getters and setters, but sometimes it is important to show all details. A rule could be such as:

if your property is implemented with a private variable and a pair of getters and setters with the same visibility, you just create a property with this visibility.

if your property is implemented with a private variable, but the getter and setter has distinct visibilities, such as a public getter and a protected setter, it would be advisable to show the getter and setter in the model.

and so son...

atorres
  • 402
  • 4
  • 11
0

It may be useful to make the getter/setter convention explicit for the properties concerned by creating your own «get/set» and «get» stereotypes to be used for categorizing these private properties. See my answer to Shortcut for denoting or implying getters and setters in UML class diagrams.

Community
  • 1
  • 1
Gerd Wagner
  • 5,481
  • 1
  • 22
  • 41