software engineering :uml class diagrams

31
Software Engineering Principles Ajit K Nayak, Ph.D. [email protected] UML Class Diagram

Upload: ajit-nayak

Post on 22-Jan-2018

276 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: Software Engineering :UML class diagrams

Software Engineering Principles

Ajit K Nayak, Ph.D.

[email protected]

UML Class Diagram

Page 2: Software Engineering :UML class diagrams

Acknowledgements• Slides of Prof. Rajib Mall, IIT, KGP

Page 3: Software Engineering :UML class diagrams

Class Diagram• Entities with common features, i.e.

attributes and operations.

• Represented as solid outline rectanglewith compartments for name, attributes, and operations.

• Attribute and operation compartments are optional depending on the purpose of a diagram.

Window

size: Sizevisibility: boolean

display()hide()

• Java Syntax UML Syntax

• Date birthday Birthday:Date

• Public int duration = 100 +duration:int = 100

• Private Student students[MAX_Size]

Students[MAX_Size]:Student

Page 4: Software Engineering :UML class diagrams

Example: Class Diagram

LibraryMemberMember NameMembership NumberAddressPhone NumberE-Mail AddressMembership Admission Date

Membership Expiry Date

Books IssuedissueBook( );findPendingBooks( );findOverdueBooks( );returnBook( );findMembershipDetails( );

LibraryMember

issueBook( );findPendingBooks( );findOverdueBooks( );returnBook( );findMembershipDetails( );

LibraryMember

Different representations of the LibraryMember class

Page 5: Software Engineering :UML class diagrams

Visibility

Visibilty Java Syntax UML Syntax

public public +

protected protected #

package ~

private private -

Page 6: Software Engineering :UML class diagrams

Relationships Between Classes• Association

– Permanent, structural, “has a”

– Solid line (arrowhead optional)

• Aggregation– Permanent, structural, a whole created from parts

– Solid line with hollow diamond from whole

• Composition– Whole part relationship (solely owns the part)

– Solid line with solid diamond from whole

• Dependency– Temporary, “uses a”

– Dotted line with arrowhead

• Generalization– Inheritance, “is a”– Solid line with open (triangular) arrowhead

• Implementation– Dotted line with open (triangular) arrowhead

OR

Page 7: Software Engineering :UML class diagrams

Class RelationshipsRelation

AssociationGeneralization Dependency

Aggregation

Binary Association N-ary Association

Composition

Page 8: Software Engineering :UML class diagrams

Association

• Denotes permanent, structural relationship

• State of class A contains class B

• Represented by solid line (arrowhead optional)

• Example: Car and Engine classes know about each other

Page 9: Software Engineering :UML class diagrams

Associations with Navigation Information

• Can indicate direction of relationship

• Represented by solid line with arrowhead

• Gas Pedal class knows about Engine class.

• Engine class doesn’t know about Gas Pedal class

• “Gas Pedal “has an” Engine

• State of Gas Pedal class contains instance of Engine class can invoke its methods

Page 10: Software Engineering :UML class diagrams

1-1 Association – example

People

Rakesh Shukla

V. Ramesh

Tax_files

760901-1234

691205-5678

tax_file

People Tax_files1 1Associated with

Page 11: Software Engineering :UML class diagrams

Multiple Association – example

Kunti

Women People

Bhim

Arjun

Yudhistir

Women People1 *

Mother of

motherOf

Page 12: Software Engineering :UML class diagrams

UML Syntax: Association

• A Person works for a Company.

works forPerson Companyemployee employer

Association Name

Role

Member Book1 0..5borrowed

Lion Animal**eats

Class A Class Brole A

role B

Page 13: Software Engineering :UML class diagrams

Example 1: Association• A teacher teaches 1 to 3 courses (subjects)

• Each course is taught by only one teacher.

• A student can take between 1 to 5 courses.

• A course can have 10 to 300 students.

Teacher Courseteaches 1..31

Students takes

1..5

10..300

Page 14: Software Engineering :UML class diagrams

Example 2: Association• A Student can take up to five Courses.

• A student has to enroll in at least one course.

• Up to 300 students can enroll in a course.

• A class should have at least 10 students.

Student Coursecredits

10..3001..5

hasEnrolmentOfEnrols in

Student Coursecredits

10..300 1..5

hasEnrolmentOf

Enrols in

Student Coursecredits

10..300 1..5

hasEnrolmentOf

Enrols in

Page 15: Software Engineering :UML class diagrams

Recursive/Reflexive Association• A class is associated with itself

PersonFriend of

Computer

Connects to

*

*

LinkedListNode

next

previous

Page 16: Software Engineering :UML class diagrams

Implementation of Association• Java Implementation

– Use a reference variable

of one class as an

attribute of another class

Member Book1 1Borrowed

by

aBookbookName: OOSD

author: GamaaISBN: 12234434Book Reference

Book instance

bookName: memberName: AKK

memberNumber: 412323

• Member Class

public class Member{

private Book book;

public issueBook(Book aBook){

setBook(aBook);

abook.setLender(this);}

setBook(Book aBook){

book=aBook; }

…}//end class

Page 17: Software Engineering :UML class diagrams

Implementation contd.• Java Implementation

– Use a reference variable

of one class as an

attribute of another class

Member Book1 1Borrowed

by

abookbookName: OOSD

author: GamaaISBN: 12234434Book Reference

Book instance

bookName: memberName: AKK

memberNumber: 412323

• Book Classpublic class Book{

private Member member;

setLender(Member aLender){

member=aLender;

}

}//end class

Page 18: Software Engineering :UML class diagrams

Implementation Example 2

• Java Code

class Customer{

private ArrayList <Account> accounts =

new ArrayList<Account>();

public Customer(){

Account defaultAccount = new

Account();

accounts.add(defaultAccount);

}

}

Customer Account1..*1

has

Page 19: Software Engineering :UML class diagrams

Ternary Association• Decompose it to a set of

binary associationsMan Woman

1 1

Priest

1..3

Man Woman

Priest

Performed by

1 1

1..3

MarriageParticipates in

1 1

Participates in*

Page 20: Software Engineering :UML class diagrams

Aggregation• A special kind of association

• Models whole-part relationship between things

• Whole is usually referred to as composite

• the child can exist independently of the parent.

• Composition

– child cannot exist independent of the

parent

Library

Books

1 0..7Hand Finger

Page 21: Software Engineering :UML class diagrams

Aggregation Implementation

public class Car{private Wheel wheels[4];public Car (Wheel w[4]){

wheels[0] = w[0];wheels[1] = w[1];wheels[2] = w[2];wheels[3] = w[3];

}}

Car Wheel1 4

Page 22: Software Engineering :UML class diagrams

Composition Implementationpublic class Car{

private Wheel wheels[4];public Car (){

wheels[0] = new Wheel();wheels[1] = new Wheel();wheels[2] = new Wheel();wheels[3] = new Wheel();

}}

Car Wheel1 4

Page 23: Software Engineering :UML class diagrams

Dependency

• Denotes dependence between classes

• Always directed (Class A depends on B)

A B

• Represented by dotted line with arrowhead

• Caused by class methods

– Method in Class A temporarily “uses a” object of type Class B

– Change in Class B may affect class A

• Dependence may be caused by

– Local variable

– Parameter

– Return value

class A {B Foo(B x) {

B y = new B();return y;

}}

Page 24: Software Engineering :UML class diagrams

Generalization• Denotes inheritance between classes

• Can view as “is-a” relationship

• Represented by line ending in (open) triangle

Laptop, Desktop, PDA inherit state & behavior

from Computers

Page 25: Software Engineering :UML class diagrams

Implementation• Denotes class implements Java interface

• Represented by dotted line ending in (open) triangle

A implements interface B

A «B»

Page 26: Software Engineering :UML class diagrams

UML Example – I• Read the UML Diagrams

– 1 or more Pets associated with 1 PetOwner

– 1 CPU associated with 0 or more Controllers

– 1-4 DiskDrives associated with 1 SCSIController

– SCSIController is a (specialized) Controller

Page 27: Software Engineering :UML class diagrams

UML Example – II

• 1 Bank associated with 0 or more Accounts

• Checking, Savings, MoneyMarket are Accounts

Page 28: Software Engineering :UML class diagrams

Example - III

• Each Thermostat has 1 Room

• Each Thermostat associated with 0 or more Heaters

• ElectricHeater is a specialized Heater

• AubeTH101D is a specialized Thermostat

Page 29: Software Engineering :UML class diagrams

Draw class diagram of A Web Browser• The user can enter a URL and as a result, the

corresponding file is fetched and its contents is displayed in the main window.

• At each moment in time, only one file is open, whose URL is written in the status bar.

• There are two types of files, HTML files and image files; each type has its own display method.

• An HTML file may in turn have references to image files. • The display method of both types of files takes a canvas to

draw the contents on it, and when the browser wants to display a file, the canvas is the main window of the browser.

• There is a “home” button on the browser, which takes the browser to a pre-set URL. The “home” URL can be set at any time by the user.

Page 30: Software Engineering :UML class diagrams

Class diagram of the WEB BROWSER

Page 31: Software Engineering :UML class diagrams

Thank You