Software Engineering Principles
Ajit K Nayak, Ph.D.
ajitnayak@soauniversity.ac.in
UML Class Diagram
Acknowledgements
• Slides of Prof. Rajib Mall, IIT, KGP
Class Diagram
• Entities with common features, i.e.
attributes and operations.
• Represented as solid outline rectangle
with compartments for name, attributes,
and operations.
• Attribute and operation compartments
are optional depending on the purpose of
a diagram.
Window
size: Size
visibility: 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
Example: Class Diagram
LibraryMember
Member Name
Membership Number
Address
Phone Number
E-Mail Address
Membership Admission Date
Membership Expiry Date
Books Issued
issueBook( );
findPendingBooks( );
findOverdueBooks( );
returnBook( );
findMembershipDetails( );
LibraryMember
issueBook( );
findPendingBooks( );
findOverdueBooks( );
returnBook( );
findMembershipDetails( );
LibraryMember
Different representations of the
LibraryMember class
Visibility
Visibilty Java Syntax UML Syntax
public public +
protected protected #
package ~
private private -
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
Class Relationships
Relation
AssociationGeneralization Dependency
Aggregation
Binary Association N-ary Association
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
People
Rakesh Shukla
V. Ramesh
Tax_files
760901-1234
691205-5678
tax_file
People Tax_files
1 1
Associated with
Multiple Association – example
Kunti
Women People
Bhim
Arjun
Yudhistir
Women People
1 *
Mother of
motherOf
UML Syntax: Association
• A Person works for a Company.
works for
Person Companyemployee employer
Association Name
Role
Member Book1 0..5borrowed
Lion Animal**
eats
Class A Class B
role A
role B
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 Course
teaches 1..31
Students takes
1..5
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 Coursecredits
10..300
1..5
hasEnrolmentOf
Enrols in
Student Course
credits
10..300 1..5
hasEnrolmentOf
Enrols in
Student Coursecredits
10..300 1..5
hasEnrolmentOf
Enrols in
Recursive/Reflexive Association
• A class is associated with itself
Person Friend of
Computer
Connects to
*
*
LinkedListNode
next
previous
Implementation of Association
• Java Implementation
– Use a reference variable
of one class as an
attribute of another class
Member Book
1 1Borrowed
by
aBook
bookName: OOSD
author: Gamaa
ISBN: 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
Implementation contd.
• Java Implementation
– Use a reference variable
of one class as an
attribute of another class
Member Book
1 1Borrowed
by
abook
bookName: OOSD
author: Gamaa
ISBN: 12234434Book Reference
Book instance
bookName:
memberName: AKK
memberNumber: 412323
• Book Class
public class Book{
private Member member;
setLender(Member aLender){
member=aLender;
}
…
}//end class
Implementation Example 2
• Java Code
class Customer{
private ArrayList <Account> accounts =
new ArrayList<Account>();
public Customer(){
Account defaultAccount = new
Account();
accounts.add(defaultAccount);
}
}
Customer Account
1..*1
has
Ternary Association
• Decompose it to a set of
binary associations
Man Woman
1 1
Priest
1..3
Man Woman
Priest
Performed by
1 1
1..3
Marriage
Participates
in
1 1
Participates
in*
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..7
Hand Finger
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
Composition Implementation
public 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
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;
}
}
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 implements interface B
A «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

Software Engineering :UML class diagrams

  • 1.
    Software Engineering Principles AjitK Nayak, Ph.D. ajitnayak@soauniversity.ac.in UML Class Diagram
  • 2.
    Acknowledgements • Slides ofProf. Rajib Mall, IIT, KGP
  • 3.
    Class Diagram • Entitieswith common features, i.e. attributes and operations. • Represented as solid outline rectangle with compartments for name, attributes, and operations. • Attribute and operation compartments are optional depending on the purpose of a diagram. Window size: Size visibility: 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
  • 4.
    Example: Class Diagram LibraryMember MemberName Membership Number Address Phone Number E-Mail Address Membership Admission Date Membership Expiry Date Books Issued issueBook( ); findPendingBooks( ); findOverdueBooks( ); returnBook( ); findMembershipDetails( ); LibraryMember issueBook( ); findPendingBooks( ); findOverdueBooks( ); returnBook( ); findMembershipDetails( ); LibraryMember Different representations of the LibraryMember class
  • 5.
    Visibility Visibilty Java SyntaxUML Syntax public public + protected protected # package ~ private private -
  • 6.
    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
  • 7.
  • 8.
    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
  • 9.
    Associations with NavigationInformation • 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
  • 10.
    1-1 Association –example People Rakesh Shukla V. Ramesh Tax_files 760901-1234 691205-5678 tax_file People Tax_files 1 1 Associated with
  • 11.
    Multiple Association –example Kunti Women People Bhim Arjun Yudhistir Women People 1 * Mother of motherOf
  • 12.
    UML Syntax: Association •A Person works for a Company. works for Person Companyemployee employer Association Name Role Member Book1 0..5borrowed Lion Animal** eats Class A Class B role A role B
  • 13.
    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 Course teaches 1..31 Students takes 1..5 10..300
  • 14.
    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..300 1..5 hasEnrolmentOf Enrols in Student Course credits 10..300 1..5 hasEnrolmentOf Enrols in Student Coursecredits 10..300 1..5 hasEnrolmentOf Enrols in
  • 15.
    Recursive/Reflexive Association • Aclass is associated with itself Person Friend of Computer Connects to * * LinkedListNode next previous
  • 16.
    Implementation of Association •Java Implementation – Use a reference variable of one class as an attribute of another class Member Book 1 1Borrowed by aBook bookName: OOSD author: Gamaa ISBN: 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
  • 17.
    Implementation contd. • JavaImplementation – Use a reference variable of one class as an attribute of another class Member Book 1 1Borrowed by abook bookName: OOSD author: Gamaa ISBN: 12234434Book Reference Book instance bookName: memberName: AKK memberNumber: 412323 • Book Class public class Book{ private Member member; setLender(Member aLender){ member=aLender; } … }//end class
  • 18.
    Implementation Example 2 •Java Code class Customer{ private ArrayList <Account> accounts = new ArrayList<Account>(); public Customer(){ Account defaultAccount = new Account(); accounts.add(defaultAccount); } } Customer Account 1..*1 has
  • 19.
    Ternary Association • Decomposeit to a set of binary associations Man Woman 1 1 Priest 1..3 Man Woman Priest Performed by 1 1 1..3 Marriage Participates in 1 1 Participates in*
  • 20.
    Aggregation • A specialkind 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..7 Hand Finger
  • 21.
    Aggregation Implementation public classCar{ 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
  • 22.
    Composition Implementation public classCar{ 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
  • 23.
    Dependency • Denotes dependencebetween 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; } }
  • 24.
    Generalization • Denotes inheritancebetween classes • Can view as “is-a” relationship • Represented by line ending in (open) triangle Laptop, Desktop, PDA inherit state & behavior from Computers
  • 25.
    Implementation • Denotes classimplements Java interface • Represented by dotted line ending in (open) triangle A implements interface B A «B»
  • 26.
    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
  • 27.
    UML Example –II • 1 Bank associated with 0 or more Accounts • Checking, Savings, MoneyMarket are Accounts
  • 28.
    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
  • 29.
    Draw class diagramof 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.
  • 30.
    Class diagram ofthe WEB BROWSER
  • 31.