You are on page 1of 31

Software Engineering Principles

UML Class Diagram

Ajit K Nayak, Ph.D.


ajitnayak@soauniversity.ac.in
Acknowledgements
• Slides of Prof. Rajib Mall, IIT, KGP
Class Diagram
• Entities with common features, i.e. Window
attributes and operations.
• Represented as solid outline rectangle size: Size
with compartments for name, attributes, visibility: boolean
and operations.
• Attribute and operation compartments display()
are optional depending on the purpose of hide()
a diagram.
• Java Syntax UML Syntax
• Date birthday Birthday:Date
• Public int duration = 100 +duration:int = 100
• Private Student students[MAX_Size]
Students[MAX_Size]:Student
Example: Class Diagram
LibraryMember LibraryMember LibraryMember
Member Name issueBook( );
Membership Number findPendingBooks( );
Address findOverdueBooks( );
Phone Number returnBook( );
E-Mail Address findMembershipDetails( );
Membership Admission Date
Membership Expiry Date
Books Issued
issueBook( ); Different representations of the
findPendingBooks( ); LibraryMember class
findOverdueBooks( );
returnBook( );
findMembershipDetails( );
Visibility

Visibilty Java Syntax UML Syntax

public public +

protected protected #

package ~

private private -
Relationships Between Classes
• Association OR
– 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
Class Relationships
Relation

Generalization Association Dependency

Binary Association N-ary Association

Aggregation

Composition
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
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
1-1 Association – example
tax_file
Rakesh Shukla
760901-1234

V. Ramesh
691205-5678

People Tax_files

1 1
People Associated with Tax_files
Multiple Association – example

Bhim
motherOf
Kunti Yudhistir

Arjun
Women People

1 *
Women Mother of People
UML Syntax: Association
role B
Class A Class B
role A
• A Person works for a Company.
Role
employee employer
Person works for
Company
Association Name
Member 1 borrowed 0..5
Book
eats
Lion * * Animal
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.

1
teaches 1..3
Teacher Course
1..5
takes
Students
10..300
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 credits hasEnrolmentOf
1..5
Course
10..300 Enrols in

credits hasEnrolmentOf
Student Course
10..300 Enrols in 1..5

Student credits hasEnrolmentOf Course


10..300 Enrols in 1..5
Recursive/Reflexive Association
• A class is associated with itself

Person Friend of
Connects to

*
Computer
*
next

LinkedListNode
previous
Implementation of Association
• Java Implementation 1 Borrowed 1
Member Book
by
– Use a reference variable
of one class as an
attribute of another class bookName: OOSD
aBook author: Gamaa
• Member Class ISBN: 12234434
Book Reference
public class Member{
Book instance
private Book book;
public issueBook(Book aBook){
setBook(aBook); bookName:
memberName: AKK
abook.setLender(this);} memberNumber: 412323
setBook(Book aBook){
book=aBook; }
…}//end class
Implementation contd.
• Java Implementation 1 Borrowed 1
Member Book
by
– Use a reference variable
of one class as an
attribute of another class bookName: OOSD
abook author: Gamaa
• Book Class Book Reference ISBN: 12234434
public class Book{ Book instance
private Member member;
setLender(Member aLender){
bookName:
member=aLender; memberName: AKK
} memberNumber: 412323

}//end class
Implementation Example 2
has Account
• Java Code Customer
1 1..*
class Customer{
private ArrayList <Account> accounts =
new ArrayList<Account>();
public Customer(){
Account defaultAccount = new
Account();
accounts.add(defaultAccount);
}
}
Ternary Association
1 1
• Decompose it to a set of Man Woman
binary associations
1..3
Priest

1 1 1 1
Man Marriage Woman
Participates Participates
* in
in

Performed by
1..3
Priest
Aggregation
Library
• A special kind of association
• Models whole-part relationship
between things
Books
• Whole is usually referred to as
composite
• the child can exist independently of the
parent.
• Composition
– child cannot exist independent of the
parent
Hand Finger
1 0..7
Aggregation Implementation

public class Car{ 1 4 Wheel


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];
}
}
Composition Implementation
public class Car{
private Wheel wheels[4]; Car 1 4 Wheel
public Car (){
wheels[0] = new Wheel();
wheels[1] = new Wheel();
wheels[2] = new Wheel();
wheels[3] = new Wheel();
}
}
Dependency
• Denotes dependence between
classes A B
• Always directed (Class A depends
on 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 class A {
– Local variable B Foo(B x) {
B y = new B();
– Parameter
return y;
– Return value }
}
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
Implementation
• Denotes class implements Java interface
• Represented by dotted line ending in (open) triangle

A «B»

A implements interface B
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
UML Example – II

• 1 Bank associated with 0 or more Accounts


• Checking, Savings, MoneyMarket are Accounts
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
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.
Class diagram of the WEB BROWSER
Thank You

You might also like