software engineering :uml class diagrams
TRANSCRIPT
Acknowledgements• Slides of Prof. Rajib Mall, IIT, KGP
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
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
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 RelationshipsRelation
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_files1 1Associated with
Multiple Association – example
Kunti
Women People
Bhim
Arjun
Yudhistir
Women People1 *
Mother of
motherOf
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
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
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
Recursive/Reflexive Association• A class is associated with itself
PersonFriend 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 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
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
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
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*
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
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 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
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