24
Class diagram

Class diagram. Purpose of Structural Models Class diagrams represent –Generic Categories (classes) of things (objects) –Define relationships among Generic

Embed Size (px)

Citation preview

Class diagram

Purpose of Structural Models

• Class diagrams represent

– Generic Categories (classes) of things (objects)

– Define relationships among Generic Categories (classes) of things (objects)

Classes

• Generic type of thing• Objects are the individual things• Classes are templates for creating instances or

objects– Concrete– Abstract

• Typical examples:– Application domain, user interface, data structure, file

structure, operating environment, document, and multimedia classes

Attributes

• Facts related to a type of thing– Attributes of a class– Class – Car Attribute may include: Make,

Model, Engine size, etc

• Only attributes important to the task should be included

Operations

• Behavior types of things do– Class – Car, Operation – accelerate(),

break(), etc

• Action that instances/objects can take – defined by the class

• Focus on relevant problem-specific operations (at this point)

• Quick Exercise – in 1 minute

Exercise 1 - Classes

• A Cinema shows films. Each film has a title, a producer, a director, a length, certificate and a genre. Patrons watch films at the cinema. Patrons are either adults or children and have a name and an age. To see a film a patron has to buy a ticket. A ticket has a price, a time and is for a film.

Exercise 1 – Solution Template

Class Attributes

Class Diagram

• Mechanism for visualising classes• Became truly central within all object oriented

methods• A Class Diagram shows

– Classes• Attributes• Operations

– Relationships• Which are consistent over time• Shows the structure of things and ideas in a

system

Basic Diagram

• Identify Classes

• Draw a rectangle for each class

• Name it

Add attributes and operations

• For each class identified

• Identify attributes– Facts associated with classes

• Identify operations/methods– Actions objects of the classes do

• Re-factor– Are the attributes in the right class?– Are the operations in the right class?

Simple Class Diagram

Attributes

Operations

Class

Studentnameage

takesNotes()doHomework()sitExam()gotoLibrary()gotoStudentUnion()

Relationships

• Some Classes can be related

• Relationships have difference types• Generalization / Specialization• Aggregation• Composition• Association

Relationships

• Classes do not exist in isolation

• It is important to consider how they – Interact– Relate

• Tend to be VERBS

• Five principal types of relationships

Type One - Dependency

• Where one class must know about another• Example:

– Prospective student knows of a course

• Not commonly used• Used more commonly in design / implementation

– Java – import javax.swing.*;

Car Wheel

Type Two - Association

• Classes associated with / related to one another

• Example– Student studies a course

• Very Commonly used

• “Default” type of relationship

Student Coursestudies►

Type Three - Aggregation

• Classes made up of other classes

• “Part of” relationship

• Objects of classes that can exist in their own right

• Example– Student is a member of a seminar group

SeminarStudent memberOf ►

Type Four - Composition

• Class is an integral part of another

• “Integral part of” relationship

• Object of classes that cannot exist without the other

• Example:– Seminar is delivered for a Module

Seminar ModuledeliveredFor ►

Type Five Inheritance

• Specialisation / generalisation between two classes

• Example– Part Time Student is a type of Student

StudentPartTimeStudent

Visibility

Visibility Description Symbol

Public Is directly accessible by an instance of any class

+

Private May only be used by an instance of the class that includes it

-

Protected

May be used either by an instance of the class that includes it or by a subclass of that class

#

Package Is directly accessible only by instances of a class in the same package

~

Association Classes

• Sometimes the way in which objects of a class are associated is just as important as the objects of a class themselves

• Consider the association between Student and Module. Where should our system record the student’s marks on for each student on each module?

Student Module1..* 6is taking

is takingmark : Integer

associationclass

Constraints

• a constraint is a condition that has to be satisfied • Use with care

– Too much text on a diagram– Can be over used

RentalCar Customerage

0..*1..* 0..*

rents

1..* {customer.age > 25}

Recursive Association - is allowed, and can be useful !

• an employee, as a manager, may manage other employees; each employee may be managed by another employee

employee

manages

0..*

0..1

Class v. Object DiagramStaff

staffNamestaffIDstaffstartdatestaffdept

AkhtarAli:Staff

staffName=Akhtar Ali staffID=313staffstartdate=140800staffdept=SDE

full symbol for an object is a box with 3 compartments• class name• attributes• operations

an instance symbol shows bothinstance and class name underlinedwith a colon to separate

in an instance symbol, both attributenames and values are given

ที่��มา• IS0514, Business Systems Development

Tools and Techniques, Northumbria University : Dr. M. Akhtar Ali