Week 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 82

Object-Oriented Design using UML

Rajib Mall
CSE Department
IIT KHARAGPUR

1
Introduction
• Object-oriented design (OOD) techniques are now extremely
popular:
–Inception in early 1980’s and nearing maturity.

–Widespread acceptance in industry and academics.

–Unified Modelling Language (UML) became an ISO standard


(ISO/IEC 19501) in 2004.
Object Modelling Using UML
• UML is a modelling language.
• Not a system design or development methodology

• Used to document object-oriented analysis


and design results.
• Independent of any specific design
methodology.
UML Origin
• OOD in late 1980s and early 1990s:
− Different software development houses were using different
notations.
− Methodologies were tied to notations.

• UML developed in early 1990s:


− To standardize the large number of object-oriented modelling
notations that existed.
UML Lineology
• Based Principally on:
− OMT [Rumbaugh 1991]
− Booch’s methodology[Booch 1991]
− OOSE [Jacobson 1992]
− Odell’s methodology[Odell 1992]
− Shlaer and Mellor [Shlaer 1992]
Different Object
OMT Modeling
Techniques in
UML
UML
Booch’s
OOSE
Methodology
UML as A Standard
• Adopted by Object Management Group (OMG) in 1997.
• OMG is an association of industries
• Promotes consensus notations and techniques
• UML also being used outside software development
area:
− Example car manufacturing
History of UML UML 2.0 Industrialization

Standardization
UML 1.1

public
feedbac UML
UML 0.9
0.9 && 0.91
0.91
Unification
k
Unified method 0.8

Booch'93 OMT-2

Other methods Booch'91 OMT-1 OOSE Fragmentation


Developments to UML
• UML continues to UML 2.0 2003
develop, due to: Application
to embedded
– Refinements systems
UML 1.X
– Making it applicable to
new contexts
UML 1.0 1997
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
Diagrams
Behavioural View
Structural View - Sequence Diagram
and views
in UML
- Class Diagram - Collaboration Diagram
- Object 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
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
Are All Views Required for Developing A
NO 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.
− When states are only one or two, state chart model becomes trivial

Deployment diagram:
− In case several hardware components used to develop the system.
Use Case Modelling

9/6/2018
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.
Are All Use Cases Independent?
• Use cases appear independent of each other
• However, Implicit dependencies may exist
• Example: In Library Automation System, renew-book and
reserve-book are independent use cases.
–But in actual implementation of renew-book--- A check is made to
see if any book has been reserved using reserve-book.
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

actor and use case by a line Play Move <<external


system>>
Tic-tac-toe game
Player
• 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 relationship Tic-tac-toe game

• 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
Another Example Use Case Diagram

Video Store Information


System

Rent
Videos
Clerk <<external system>>
Credit Authorization
. . . Service
Yet Another Use
Telephone Order System
Case Example
Check
Status Salesperson

Place
Order

Fill
Shipping
Orders
Customer Clerk

Establish
credit

Supervisor
Factoring Use Cases
• Two main reasons for factoring:
–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
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
• Video Store Information System supports the following
business functions: Example 1: Video Store Information System
– Recording information about videos the store owns
• This database is searchable by staff and all customers
– Information about a customer’s borrowed videos
• Access by staff and customer. It involves video database searching.
– Staff can record video rentals and returns by customers. It involves
video database searching.
– Staff can maintain customer and video information.
– Managers of the store can generate various reports.
Video Store Information System

Rent/Return
Videos
«include»

Staff
Customer
Search for
Videos
Maintain
Customers
«include»

Maintain Example 1:
Videos
Solution
Generate
Manager Reports
Name
Actors Use Case
Trigger Description
Preconditions
Post conditions

Mainline Scenario
Alistair Cockburn
“Writing
Effective Use
Alternatives flows Cases”
Start use case Use Case
Scenarios
Alternative flow 3 Alternative flow 1

Alternative flow 2

Alternative flow 4
end use case
end use case

end use case


44
ATM Money Withdraw Example
• Actors: Customer
• Pre Condition:
– ATM must be in a state ready to accept transactions
– ATM must have at least some cash it can dispense
– ATM must have enough paper to print a receipt
• Post Condition:
– The current amount of cash in the user account is the amount before withdraw
minus withdraw amount
– A receipt was printed on the withdraw amount
Actor Actions System Actions
1. Begins when a Customer arrives
at ATM
2. Customer inserts a Credit card 3. System verifies the customer ID
ATM Money
into ATM and status Withdraw
5. Customer chooses “Withdraw” 4. System asks for an operation
operation type Mainline
7. Customer enters the cash
amount
6. System asks for the withdraw
amount
Scenario
8. System checks if withdraw
amount is legal
9. System dispenses the cash
10. System deduces the withdraw
amount from account

11. System prints a receipt


13. Customer takes the cash and 12. System ejects the cash card
the receipt
ATM Money Withdraw (cont.)
• Alternative flow of events:
– Step 3: Customer authorization failed. Display an error message, cancel the
transaction and eject the card.
– Step 8: Customer has insufficient funds in its account. Display an error
message, and go to step 6.
– Step 8: Customer exceeds its legal amount. Display an error message, and go to
step 6.
• Exceptional flow of events:
– Power failure in the process of the transaction before step 9, cancel the
transaction and eject the card.
 Actors: traveler Use Case Description: Change Flight
 Preconditions:
• Traveler has logged on to the system and selected ‘change flight itinerary’
option
 Basic course
1. System retrieves traveler’s account and flight itinerary from client account
database
2. System asks traveler to select itinerary segment she wants to change; traveler
selects itinerary segment.
3. System asks traveler for new departure and destination information; traveler
provides information.
4. If flights are available then
5. …
6. System displays transaction summary.
 Alternative courses
4. If no flights are available then …
Guidelines for Effective Use Case Writing
System Actor
• Use simple sentence Actor asks for money

System asks for amount

• Do not have both system and actor Actor gives the amount

doing something in a single step System produce the money

– Bad: “Get the amount from the user and give him the receipt.”

• Any step should lead to some tangible progress:


– Bad: “User clicks a key”
Identification of Use Cases
1. Actor-based:
- 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.
Example 2: Use Case Model for Course Management Software
• At the beginning of each semester,
– Each professor shall register the courses that he is going to teach.
• A student can select up to four-course offerings.
– During registration a student can request a course catalogue showing course offerings for the
semester.
– Information about each course such as professor, department and prerequisites would be
displayed.
– The registration system sends information to the billing system, so that the students can be
billed for the semester.
• For each semester, there is a period of time during which dropping of courses is
permitted.
• Professors must be able to access the system to see which students signed up for each
of their course offerings.
Course Management Software

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

<<External>>
Register
Billing System
course
Student

Drop
Course

Calendar
• 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.


Style Notes
Actors are associated with one or more use cases.

(Ambler, 2005)
• 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.
• <<include>> should rarely nest more than 2 levels deep.
• 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".
Too many use cases
at any level should
be avoided!
Use Case Packaging

Accounts

Print
Query balance
Balance sheet

Receive Make
grant payments
Ordering

Query balance

More accetable!
Customer
Maintenance

Query balance
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 have a few
visibility: boolean association
attributes
display()
Operations hide()
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 “A Dog ISA Animal”
“A Cat ISA 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

Object height 4.0


Reference
Mammal Quadruped

More
Generalization
Feline Lizard
Examples…

Tiger Lion Puma


Any problems? Car
attributes
operations

Wrong
Generalization Chassis Engine Door
--- attributes attributes attributes

violates “is a” operations operations operations

or “is a kind
of” heuristic

69
Inheritance Example Library
Book

issuable reference

Issuable Reference
Issuable Reference
Single Volume Single Volume
BookSet BookSet
Book 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 – example
• In a home theatre system,
– A TV object has an association with a VCR object
• It may receive a signal from the VCR
– VCR may be associated with remote
• It may receive a command to record
1 1 1 1
Remote commands VCR Connected to TV
1-1 Association tax_file
– example
Rakesh Shukla 760901-1234

V. Ramesh
691205-5678
Keshab Parhi

People Tax_files
1 1
People Associated Tax_files
with
motherOf
Multiple
Bhim
Association – motherOf
example Kunti Yudhistir

motherOf
Arjun
Woman Person

1 *
Woman Mother of Person
Association UML Syntax
role B
Class A role A Class B

• A Person works for a Company. Role

Person employee employer Company


works for

Association Name
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
Association – Multiplicity
• 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
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 in 1..5
Student credits hasEnrolmentOf Course
Identify as 10..300 Enrols in 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

1..2 teaches 0..3


Teacher Course

opens 0..5
* Door
Key
Association and Link
• A link:
– An instance of an association
– Exists between two or more objects
– Dynamically created and destroyed as the run of a
system proceeds
• For example:
– An employee joins an organization.
– Leaves that organization and joins a new organization.

You might also like