3

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

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
jma
  • 79
  • 1
  • 6

2 Answers2

6

<<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.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • Thank you, I believe this is the answer, I just couldn't wrap my head around it. One last question I just faced; When I come across a method in Book() like ReturnBook() and it gives the data types Timespan, int, and decimal. How is it possible to assign 3 datatypes to a method? – jma Feb 10 '19 at 22:16
  • That's a bit too difficult to answer in a comment. Please ask a new question for that. I'll have a look tomorrow (unless someone else answers earlier). – qwerty_so Feb 10 '19 at 22:50
  • 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 – Flydog57 Feb 10 '19 at 23:11
1

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).

Flydog57
  • 6,851
  • 2
  • 17
  • 18