You are on page 1of 86

UNIT 3

Object-Oriented Design using UML

1
UML

• UML stands for “Unified Modeling Language”

• It is a industry-standard graphical language for specifying, visualizing,

constructing, and documenting the artifacts of software systems

• The UML uses mostly graphical notations to express the OO analysis and

design of software projects.

• Simplifies the complex process of software design


Why are UML Models Required?
⚫ Modelling is an abstraction mechanism:
− Capture only important aspects and ignores the rest.
− Different models obtained when different aspects are
ignored.
− An effective mechanism to handle complexity.
⚫ UML is a graphical modelling technique
⚫ Easy to understand and construct
UML Diagrams
⚫Nine diagrams in UML1.x :
− Used to capture 5 different views of a system.
⚫Views:
− Provide different perspectives of a software system.
⚫Diagrams can be refined to get the actual implementation
of a system.
⚫Views of a system:
UML
− User’s view Model
− Structural view Views

− Behavioral view
− Implementation view
− Environmental view
9 Diagrams and
5 views in UML

Structural View Behavioural View


-Class Diagram -Sequence Diagram
-Object Diagram -Collaboration Diagram
-State-chart Diagram
-- Activity Diagram

User’s View
-Use Case Diagram

Implementation View Environmental View


- Component Diagram - Deployment Diagram
• Class Diagram
Structural
– set of classes and their relationships.
Diagrams
• Object Diagram
– set of objects (class instances) and their relationships
•Component Diagram
– logical groupings of elements and their relationships
•Deployment Diagram
– set of computational resources (nodes) that host each component.
• Use Case Diagram
– high-level behaviors of the system, user Behavioral
goals, external entities: actors
• Sequence Diagram Diagrams
– focus on time ordering of messages
• Collaboration Diagram
– focus on structural organization of objects
and messages
• State Chart Diagram
– event driven state changes of system
• Activity Diagram
– flow of control between activities
Are All Views Required for Developing
⚫ NO A Typical System?

⚫ For a simple system:


− Use case diagram, class diagram and one of the interaction diagrams
only.
⚫ State chart diagram:
− when class has significant states.
⚫ Deployment diagram:
− In case several hardware components used to develop the system.
Some Insights on Using UML
• “UML is a large and growing beast, but you don’t need all
of it in every problem you solve…”
– Martin Fowler
• “…when learning the UML, you need to be aware that
certain constructs and notations are only helpful in
detailed design while others are useful in requirements
analysis …” Brian Henderson-Sellers
Use Case Modelling
Use Case Model Behavioural View
Structural View -Sequence Diagram

• Consists of a set of “use cases” - Class Diagram


- Object Diagram
-Collaboration Diagram
-State-chart Diagram
- Activity Diagram
User’s View
-Use Case
Diagram

• It is the central model: Implementation View


- Component Diagram
Environmental View
- Deployment Diagram

–Other models must conform to this model

–Not really an object-oriented model, it is a functional model


of a system
A Use Case
• A case of use: A way in which a system can be used by the
users to achieve specific goals

• Corresponds to a high-level requirement.


• Defines external behavior without revealing internal structure
of system

• Set of related scenarios tied together by a common goal.


–Use cases for a Library information system

•issue-book
•query-book
•return-book Example
Use Cases
•create-member
•add-book, etc.
An Example Use Case Diagram

Play Move

Tic-tac-toe game
Player

Use case model


Why Develop A Use Case Diagram?
• Serves as requirements specification
• How are actor identification useful in software
development?
–Identifies different categories of users:
• Helps in implementing appropriate interfaces for each category
of users.
• Helps in preparing appropriate documents (e.g. users’
manual).
• Represented in a use case diagram Representation of
• A use case is represented by an ellipse Use Cases

• System boundary is represented by a rectangle


• Users are represented by stick person icons (actor)
• Communication relationship between Backup

Play Move
actor and use case by a line <<external
system>>
Player Tic-tac-toe game

•External system by adding a stereotype


What is a Connection?
• A connection is an association between an actor
and a use case.
Play Move

•Depicts a usage Tic-tac-toe game


relationship
• Connection does not indicate data flow
Relationships between Use Cases and Actors
• Association relation indicates that the actor and the
corresponding use case communicate with one another.

update
grades

faculty
Yet Another Use
Order System
Case Example
Check
Status
Salesperson

Place
Order

Fill
Shipping
Orders
Customer Clerk

Establish
credit

Supervisor
Factoring Use
• Two main reasons for factoring: Cases
–Complex use cases need to be factored into simpler use cases
–Helps represent common behavior across different use cases
• Three ways of factoring:
–Generalization
–Include
–Extend
Generalization
• The child use case inherits the
parent
behavior of the parent use case.
–The child may add to or override some
child
of the behavior of its parent.
Registration
Generalization
Example 1

Under-graduate Graduate
registration registration
Factoring Use Cases Using Generalization

Pay membership fee

Pay through credit card Pay through library pay card


<<include>> Common
Base use case use
case Factoring Use
Cases Using
Include
Base use case 2 Base use case 1

<<include>>
<<include>>
<<include>>
<<include>>

Common use case 1 Common use case 2 Common use case 3


Issue Book Renew Book

<<include>>
<<include>>

Example of Factoring
Use Cases Using
Check Reservation Include
Example Factoring A Use Case Using Extend

Order <<extend>> Show


Item Catalog
29
• Use case name should begin with a verb.
• While use cases do not explicitly imply timing:
– Order use cases from top to bottom to imply timing -- it improves readability.

• The primary actors should appear in the left.


• Actors are associated with one or more use cases.
• Do not use arrows on the actor-use case relationship.
• To initiate scheduled events include an actor called “time”, or “calendar”
• Do not show actors interacting with each other.
• Use cases should be named and organized from the perspective of
the users.

• Use cases should start off simple and at as much high view as
possible.
Effective Use Case
– Can be refined and detailed further. Modelling
• Use case diagrams represent functionality:
– Should focus on the "what" and not the "how".
Extension Point
• The base use case may include/extend other use cases:
– At certain points, called extension points.

• Note the direction of the arrow

<<extend>>
Perform Sale Product is a gift
Gift wrap
After checkout Product
Order
Supply Product
Customer Data Arrange
Payment
<<include>>
<<include>>
<<include>>
Place Order
Cash Credit
Payment Payment
Sales Person
<<extend>>
Salesperson asks for catalog

Use Case Relationships Request


Catalog
34
35
36
37
38
39
40
41
Identification of Use
1. Actor-based: Cases

- Identify the actors related to a system or organization.


- For each actor, identify the processes they initiate or participate
in.

2. Event-based
- Identify the external events that the system must respond to.
- Relate the events to actors and use cases.
Course Management Software

Register Example 2:
Course
offering
See
Model Solution
Course
Professor Show List
registration
<<Extend>>

Register <<External>>
Billing System
course
Student

Drop
Course

Calendar
Too many use cases
at any level should
be avoided!
Use Case
Packaging
Accounts

Print
Query balance
Balance sheet

Receive Make
grant payments
Class Diagram
• Classes:
– Entities with common features, i.e. attributes and operations.
– Represented as solid outline rectangle with compartments.
– Compartments for name, attributes, and operations.
– Attribute and operation compartments are optional depending on
the purpose of a diagram.
UML Class Representation
• A class represents a set of objects having similar attributes,
operations, relationships and behavior.
Class Name
Window
A class can
size: Size implicitly
Attributes visibility: boolean have a few
association
attributes
display() hide()
Operations
Different representations of the LibraryMember class

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( ); Example UML


findPendingBooks( );
findOverdueBooks( ); Classes
returnBook( );
findMembershipDetails( );
What are the Different Types of Relationships
Among Classes?
• Four types of relationships:
– Inheritance
– Association
– Aggregation/Composition
– Dependency
Inheritance
LibraryMember Base Class

• Allows to define a new class (derived class)


by extending an existing class (base class).
Derived
Faculty Students Staff Classes

–Represents generalization-specialization

–Allows redefinition of the existing methods


(method overriding).
UnderGrad PostGrad Research
Inheritance Example

Animal

Dog Cat
Inheritance
• Lets a subclass LibraryMember Base Class
inherit attributes
and methods from
Derived
a base class. Faculty Students Staff
Classes

UnderGrad PostGrad Research


Multiple LibraryMember Base Class LibraryMember Base Class

Inheritance

Derived
Faculty Students Staff Faculty Students Staff
Classes

Multiple
Inheritance

UnderGrad PostGrad Research UnderGrad PostGrad Research


Inheritance Implementation in Java
Inheritance is declared using the "extends" keyword
Even when no inheritance defined, the class implicitly extends a class called Object.
Person
class Person{ - name: String
private String name; - dob: Date
private Date dob;
...
}
class Employee extends Person{
private int employeeID; Employee
- employeeID: int
private int salary; - salary: int
private Date startDate; - startDate: Date
...
} Employee anEmployee = new Employee();
Objects myRectangle and myBox Rectangle

Rectangle myRectangle = new Rectangle(5, 3);


Box
Box myBox = new Box(6, 5, 4);

length 5.0 length 6.0


myRectangle
myBox

width 3.0 width 5.0

height 4.0
Object
Reference
Mammal Quadruped

More
Generalization
Feline Lizard
Examples…

Tiger Lion Puma


Inheritance Library
Example Book

issuable reference

Issuable Reference
Issuable Reference
Single Single
BookSet BookSet
Volume Book Volume Book

Discriminator: allows one to group subclasses into


clusters that correspond to a semantic category.
Inheritance Pitfalls
• Inheritance certainly promotes reuse.

• Indiscriminate use can result in poor quality


programs.
• Base class attributes and methods visible in derived
class…
– Leads to tight coupling
Association Relationship
• How implemented in program?
• Enables objects to communicate with each other:
–One object must “know” the ID of the corresponding
object in the association.
• Usually binary:
–But in general can be n-ary.
Association - More Examples

Library Member
1 borrowed by *..5 Book

* eats * Human
Lion

Multiplicity: The number of objects from one class that


relate with a single object in an associated class.
Navigability

opens 0..5
Key * Door
Quiz: Draw Class Diagram
• A Student can take up to five Courses.

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

• Up to 300 students can enroll in a course.

• An offered subject in a semester should have at least 10


registered students.
Student credits hasEnrolmentOf Course
10..300 Enrols 1..5
in
Student hasEnrolmentOf
Course
credit Enrols in
Identify as s
10..300 1..5

Correct or
Wrong Student credits hasEnrolmentOf
Course
10..300 Enrols in 1..5

Student credits hasEnrolmentOf


Course
10..300 Enrols in 1..5

Student credits hasEnrolmentOf


Course
10..300 Enrols in 1..5
Quiz: Read the Diagram

Teacher 1..2 teaches 0..3


Course

opens 0..5
* Door
Key
Example of
Class diagram

LibraryMember LibraryMember LibraryMember


Member Name Member Name
Membership Number Membership Number
Address Address
Phone Number Phone Number
E-Mail Address E-Mail Address
Membership Admission Date Membership Admission Date
Membership Expiry Date Membership Expiry Date
Books Issued Books Issued
issueBook( );
findPendingBooks( );
findOverdueBooks( );
returnBook( );
findMembershipDetails( );

Different representations of the LibraryMember class


66
Association Relationship

1 borrowed by *
Library Member Book

Association between two classes

67
Aggregation Relationship
 Represent a whole-part relationship
 Represented by diamond symbol at the composite end
 Cannot be reflexive(i.e. recursive)
 Not symmetric
 It can be transitive

68
Aggregation Relationship

1 * 1
Document Paragraph * Line

Representation of aggregation

69
Composition Relationship

 Life of item is same as the order

1 *
Order Item

Representation of composition

70
Class Dependency

Dependent Class Independent Class

Representation of dependence between class

71
Object diagram

LibraryMember LibraryMember LibraryMember

Mritunjay Mritunjay
B10028 B10028
C-108, Laksmikant Hall C-108, Laksmikant Hall
1119 1119
Mrituj@cse Mrituj@cse
25-02-04 25-02-04
25-03-06 25-03-06
NIL NIL

IssueBook( );
findPendingBooks( );
findOverdueBooks( );
returnBook( );
findMembershipDetails( );

Different representations of the LibraryMember object


72
Interaction diagram

 Models how groups of objects collaborate to realize some behaviour

 Typically each interaction diagram realizes behaviour of a single use case

73
Interaction diagram

 Two kinds: Sequence & Collaboration

 Two diagrams are equivalent but portrays


different perspective

 These diagram play a very important role in the


design process

74
Sequence diagram

 Shows interaction among objects as two-dimensional chart


 Objects are shown as boxes at top
 If object created during execution then shown at appropriate place
 Objects existence are shown as dashed lines (lifeline)
 Objects activeness, shown as rectangle on lifeline

75
Sequence diagram

 Messages are shown as arrows


 Message labelled with message name
 Message can be labelled with control information
 Two types of control information: condition ([]) & an
iteration (*)

76
Example of
Sequence diagram
:Library
:Library
:Library Book :Library
Book :Book
Boundary Renewal Member
Register
Controller

renewBook find MemberBorrowing


displayBorrowing
selectBooks bookSelected
* find
[reserved]
[reserved] apology
update
apology

confirm

confirm
updateMemberBorrowing

Sequence Diagram for the renew book use case 77


Collaboration diagram
 Shows both structural and behavioural aspects
 Objects are collaborator, shown as boxes
 Messages between objects shown as a solid line
 Message is shown as a labelled arrow placed near the link
 Messages are prefixed with sequence numbers to show relative
sequencing

78
Example of
Collaboration diagram
6: * find
:Library
Book :Book
[reserved] Register
9: update
8: apology 5: book 10: confirm
Selected
1: renewBook :Library [reserved]
:Library Book 7: apology
Boundary 3: display Renewal
Borrowing Controller

4: selectBooks
2: findMemberBorrowing

12: confirm
:Library
Member

updateMemberBorrowing

Collaboration Diagram for the renew book use case


79
Activity diagram
 New concept, possibly based on event diagram of Odell [1992]

 Represent processing activity, may not correspond to methods

 Activity is a state with an internal action and one/many outgoing


transition

80
Activity diagram

 Can represent parallel activity and synchronization aspects

 Swim lanes enable to group activities based on who is performing


them

 Example: academic department vs. hostel

81
Activity diagram

 Normally employed in business process modelling

 Carried out during requirement analysis and specification

 Can be used to develop interaction diagrams

82
Example of
Activity diagram
Academic Section Accounts Section Hostel Office Hospital Department

check
student
records
receive
fees

allot create
hostel hospital
record
register
receive
in
fees
course
conduct
allot medical
room examination

issue
identity card
Activity diagram for student admission procedure at IIT 83
State Chart diagram
 Based on the work of David Harel [1990]

 Model how the state of an object changes in its lifetime

 Based on finite state machine (FSM) formalism

84
State Chart diagram

 Elements of state chart diagram


 Initial State: Filled circle
 Final State: Filled circle inside larger circle
 State: Rectangle with rounded corners
 Transitions: Arrow between states, also boolean logic condition
(guard)

85
Example of
State Chart diagram
order received
Unprocessed
Order
[reject] checked [accept] checked

Rejected Accepted
Order Order
[some items available]
[some items not processed / deliver
available] processed

[all items
Pending available] Fulfilled
Order newsupply Order

Example: State chart diagram for an order object 86

You might also like