jma
jma

Reputation: 89

C#: Help understanding <<property>> in UML Class Diagrams

I am currently doing a project where we have to make code from UML diagrams. I understand the anatomy of a UML class diagram but I'm having trouble understanding what <<property>> means and how to implement it into my code.

enter image description here

Upvotes: 4

Views: 6901

Answers (2)

Flydog57
Flydog57

Reputation: 7111

Since you tagged this as [C#], you should know that property is a first-class part of the C# language. Classes can have properties of any type. The getters and setters can have different access levels (the getter, public, for example, while the setter is private). Read-only properties (no setter) and write-only (no getter) properties are available. If the property has a trivial definition (the getter and setter simply access a private backing field), then you can use an auto-property with a simple, easy to express and understand syntax.

class MyClass {
    //this is a simple property with a backing field
    private int _someInt = 0;
    public int SomeInt {
        get { return _someInt; }
        set { _someInt = value; }    //"value" is a keyword meaning the rhs of a property set expression
    }

    //this is a similar property as an "auto property", the initializer is optional
    public int OtherInt { get; set; } = 0;

    //this is an auto-property with a public getter, but a protected setter
    public string SomeString { get; protected set; }
}

If the setter (or getter) is omitted, the property becomes read-only (or write-only).

Upvotes: 1

qwerty_so
qwerty_so

Reputation: 36313

<<property>> is a stereotype (like most things in UML embraced by << >>). In this case it indicates that you shall implement getters and setters for the accordingly named privately owned attributes of the class. E.g. for the Status you would implement getStatus and setStatus (or whatever is used in the target language for that purpose). As there's also the constraint { readonly } for Name you shall just implement getName. You probably have to guess that the attribute's name is _bookName.

Upvotes: 7

Related Questions