How to represent a C# property in UML?

后端 未结 12 813
夕颜
夕颜 2020-12-08 19:03

Not quite an Attribute, not quite a Method. Stereotypes? <> <>?


I\'m retro-modelling an existin

12条回答
  •  情话喂你
    2020-12-08 19:45

    I've been using <> and <> stereotypes next to property names so they look like fields, yet allows you to differentiate the access modifiers for the get or set:

    +=============================+
    | ClassName                   |
    +-----------------------------+
    | +<> Id : int           |
    | -<> Id : int           |
    | +<> IsSomething : bool |
    +-----------------------------+
    | + Method1(arg1 : string)    |
    +=============================+
    

    Alternatively, if you don't want more than one occurrence of a property, this could work as well:

    +=============================+
    | ClassName                   |
    +-----------------------------+
    | +<> -<> Id : int  |
    

    And to reduce clutter, if the get and set have the same access modifier:

    +====================================+
    | ClassName                          |
    +------------------------------------+
    | +<> Description : string |
    | +<> -<> Id : int         |
    

    This clearly communicates whether a property has a get or set, and if it is readonly (by way of no <> existing in the class diagram). So basically what you said in your question.

    While properties are syntactic sugar for getter and setter methods, they are supposed to feel like fields, and I believe the UML diagram should reflect that fact, and at the same time also communicate what is public and what is private, and also whether a setter exists or not.

提交回复
热议问题