25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set...

28
Lecture 12 – Class Diagrams (UML) 25/2/16

description

Software Design (UML) ClassName attributes operations The name of the class is the only required tag in the graphical representation of a class. It always appears in the top-most compartment.

Transcript of 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set...

Page 1: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Lecture 12 – Class Diagrams (UML)

25/2/16

Page 2: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Classes

ClassName

attributes

operations

A class is a description of a set of objects that share the same attributes,Operations and relationships

Graphically, a class is rendered as a rectangle, usually including its name,attributes, and operations in separate,designated compartments.

Page 3: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Class Names

ClassName

attributes

operations

The name of the class is the only required tag in the graphical representation of a class. It always appears in the top-most compartment.

Page 4: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Class Attributes

Person

name : Stringaddress : Addressbirthdate : Datessn : Id

An attribute is a named property of a class that describes the object being modeled.In the class diagram, attributes appear in the second compartment just below the name-compartment.

Page 5: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Class Attributes (Cont’d)

Person

name : Stringaddress : Addressbirthdate : Date/ age : Datessn : Id

Attributes are usually listed in the form:

attributeName : Type

A derived attribute is one that can becomputed from other attributes, butdoesn’t actually exist. For example,a Person’s age can be computed from his birth date. A derived attribute is designated by a preceding ‘/’ as in:

/ age : Date

Page 6: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Class Attributes (Cont’d)

Person

+ name : String# address : Address# birthdate : Date/ age : Date- ssn : Id

Attributes can be:+ public# protected- private/ derived

Page 7: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Class Operations

Person

name : Stringaddress : Addressbirthdate : Datessn : Id

eatsleepworkplay

Operations describe the class behavior and appear in the third compartment.

Page 8: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Depicting Classes

Person

name : Stringbirthdate : Datessn : Id

eat()sleep()work()play()

When drawing a class, you needn’t show attributes and operation in every diagram.

Person

Person

nameaddress

birthdate

Person

eatplay

Person

Page 9: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Class Responsibilities

A class may also include its responsibilities in a class diagram.

A responsibility is a contract or obligation of a class to perform a particular service.

SmokeAlarm

Responsibilities

-- sound alert and notify guard station when smoke is detected.

-- indicate battery state

Page 10: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Find classes – ◦ A good rule of thumb is that you should look for the three-to-

five main classes right away

Find responsibilities - ◦ Ask yourself what a class does as well as what information you

wish to maintain about it.

Define collaborators - ◦ A class often does not have sufficient information to fulfil its

responsibilities. It must collaborate (work) with other classes to get the job done. Collaboration will be in one of two forms: a request for information or a request to perform a task.

More on CRC

Page 11: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Sample CRC Card

Page 12: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

CRC card (samples)UG Degree

Collaborators

Responsibilities

Degree

• Applicant • •

• Based on course• Requires exams

PG Course

ResponsibilitiesCourse

Collaborators • Application • Degree

• Knows its name• Knows its description• Has requirements

• UG Degree(s)• Exam

• Has applications

SuperclassSuperclass

Page 13: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Relationships

In UML, object interconnections (logical or physical), are modeled as relationships.

There are three kinds of relationships in UML:

• dependencies

• generalizations

• associations

Page 14: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Dependency Relationships

CourseSchedule

add(c : Course)remove(c : Course)

Course

A dependency indicates a relationship between two ormore elements. The dependency from CourseSchedule to Course exists because Course is used in both the add and remove operations of CourseSchedule.

Page 15: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Generalization Relationships

PersonA generalization connects a subclassto its superclass. It denotes an inheritance of attributes and behaviorfrom the superclass to the subclass andindicates a specialization in the subclassof the more general superclass.

Student

Page 16: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Association Relationships

If two classes in a model need to communicate with each other, there must be link between them.

An association denotes that link.

InstructorStudent

Page 17: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Association Relationships (Cont’d)

We can indicate the multiplicity of an association by adding multiplicity adornments to the line denoting the association.

The example indicates that a Student has one or more Instructors:

InstructorStudent 1..*

Page 18: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Association Relationships (Cont’d)

The example indicates that every Instructor has one or more Students:

InstructorStudent 1..*

Page 19: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Association Relationships (Cont’d)

We can also indicate the behavior of an object in an association (i.e., the role of an object) using rolenames.

InstructorStudent1..*1..*

learns fromteaches

Page 20: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Association Relationships (Cont’d)

We can also name the association.

TeamStudentmembership

1..* 1..*

Page 21: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Association Relationships (Cont’d)

We can specify dual associations.

TeamStudent

member of1..*

president of1 1..*

1..*

Page 22: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Association Relationships (Cont’d)We can constrain the association relationship by defining the navigability of the association. Here, a Router object requests services from a DNS object by sending messages to (invoking the operations of) the server. The direction of the association indicates that the server has no knowledge of the Router.

Router DomainNameServer

Page 23: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Association Relationships (Cont’d)Associations can also be objects themselves, called link classes or an association classes.

WarrantyProduct

Registration

modelNumberserialNumberwarrentyCode

Page 24: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Association Relationships (Cont’d)

A class can have a self association.

LinkedListNode

next

previous

Page 25: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Association Relationships (Cont’d)We can model objects that contain other objects by way of special associations called aggregations and compositions.

An aggregation specifies a whole-part relationship between an aggregate (a whole) and a constituent part, where the part can exist independently from the aggregate. Aggregations are denoted by a hollow-diamond adornment on the association.

CarEngine

Transmission

Page 26: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

Software Design (UML)

Association Relationships (Cont’d)A composition indicates a strong ownership and coincident lifetime of parts by the whole (i.e., they live and die as a whole). Compositions are denoted by a filled-diamond adornment on the association.

Window

Scrollbar

Titlebar

Menu

1

1

1

1

1

1 .. *

Page 27: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

27

Class diagram example

DVD Movie VHS Movie Video Game

Rental Item

Rental Invoice

1..*1

Customer

Checkout Screen

0..1

1

Simple Association

Class

AbstractClass

Simple Aggregation

GeneralizationComposition

Multiplicity

Page 28: 25/2/16. Software Design (UML) ClassName attributes operations A class is a description of a set of…

28

Class diagram example 2

StudentBody

+ main (args : String[])

+ toString() : String

1 100 Student- firstName : String- lastName : String- homeAddress : Address- schoolAddress : Address

+ toString() : String

- streetAddress : String- city : String- state : String- zipCode : long

Address