Object Oriented Analysis and Design Using the UML Version 4.2

advertisement
EC-241 Object-Oriented
Programming
LECTURE 9
Objectives: Introduction to Object
Oriented Design
• Revise the basic principles of object
orientation
• Unified Modeling Language (UML)
• Understand the basic concepts and terms of
object orientation and associated UML
notation
Basic Principles of Object Orientation
Hierarchy
Modularity
Encapsulation
Abstraction
Object Orientation
What is Abstraction?
Salesperson
Not saying
Which
salesperson
– just a
salesperson
in general!!!
Customer
Manages Complexity
Product
What is Encapsulation?
• Hide implementation from clients
– Clients depend on interface
How does an object encapsulate?
What does it encapsulate?
Improves Resiliency
What is Modularity?
• The breaking up of something complex into
Order
manageable pieces
Entry
Order Processing
System
Order
Fulfillment
Billing
Manages Complexity
What is Hierarchy?
• Levels of abstraction
Increasing
abstraction
Asset
BankAccount
Decreasing
abstraction
Savings Checking
Security
Stock
Bond
Elements at the same level of the hierarchy
should be at the same level of abstraction
RealEstate
Introduction to Object Oriented Design
• Basic Principles of Object Orientation
• Unified Modeling Language (UML)
• Basic Concepts of Object Orientation
UML: Background
• OOA, OOD, and OOP
Object-oriented methods may be applied to different phases in the
software life-cycle
e.g., Analysis, Design, implementation, etc.
• OO Analysis (OOA) is a process of discovery where a development
team models and under-stands the requirements of the system
• OO Design (OOD) is a process of invention and adaptation where
the development team creates the abstractions and mechanisms
necessary to meet the system's behavioral requirements determined
during analysis
UML: Modeling
• Modeling is a way of thinking about the problems using models organized
around the real world ideas.
• A modeling method comprises a language and also a procedure for using
the language to construct models.
• modeling is the only way to visualize your design and check it against
requirements before your crew starts to code.
References
http://www.omg.org/gettingstarted/what_is_uml.htm
http://www.inconcept.com/JCM/April1998/halpin.html
Unified
Modeling
Language
(UML)
The Unified Modeling Language (UML) is a standard language for
Visualizing
Specifying
Business Modeling
Constructing
Documenting
Communications
UML: Different Views
Users
(Use Case Diagrams)
Designers
(Class Diagrams)
Analyzers
(State Transition
Diagrams)
UML: Different Views
Analyzers
(Activity Diagram)
Analyzers
(Sequence Diagrams)
Introduction to Object Oriented Design
• Basic Principles of Object Orientation
• Unified Modeling Language (UML)
• Basic Concepts of Object Orientation
Basic Concepts of Object Orientation
•
•
•
•
•
Object
Class
Attribute (Data Member)
Operation (Member Function)
Relationships
Basic Concepts of Object Orientation
•
•
•
•
•
Object
Class
Attribute
Operation
Relationships
What is an Object?
• Informally, an object represents an entity, either
physical, conceptual, or software
– Physical entity
Truck
– Conceptual entity
Chemical Process
– Software entity
Linked List
A More Formal Definition
• An object is a concept, abstraction, or thing
with sharp boundaries and meaning for an
application
• An object is something that has:
– State (values of data members)
– Behavior (member functions)
– Identity (name of object)
Representing Objects in UML
• An object is represented as rectangles with
underlined names
: Professor
a + b = 10
ProfessorClark
Class Name Only
Professor Clark
ProfessorClark :
Professor
Class and Object Name
Object Name Only
(stay tuned for classes)
Basic Concepts of Object Orientation
•
•
•
•
•
Object
Class
Attribute
Operation
Relationships
What is a Class?
• A class is a description of a group of objects
with common properties (attributes or data
members), behavior (operations or member
functions), and relationships
– An object is an instance of a class
• A class is an abstraction in that it:
– Emphasizes relevant characteristics
– Suppresses other characteristics
OO Principle: Abstraction
Sample Class
Class
Course
Properties
Name
Location
Days offered
Credit hours
Start time
End time
a + b = 10
Behavior
Add a student
Delete a student
Get course roster
Determine if it is full
Representing Classes in UML
• A class is represented using a compartmented
rectangle
a + b = 10
Professor
Professor Clark
Class Compartments in UML
• A class is comprised of three sections
– The first section contains the class name
– The second section shows the structure
(attributes or data members)
– The third section shows the behavior (operations
or member functions)
Class Name
Attributes
Operations
Professor
name
empID
create( )
save( )
delete( )
change( )
The Relationship Between Classes and Objects
• A class is an abstract definition of an object
– It defines the structure and behavior of each
object in the class
– It serves as a template for creating objects
• Objects are grouped into classes
Class
Objects
Professor
Professor Smith
Professor Mellon
Professor Jones
Basic Concepts of Object Orientation
•
•
•
•
•
Object
Class
Attribute
Operation
Relationships
What is an Attribute? In UML
Object
Class
Attribute
Attribute Value
:CourseOffering
CourseOffering
number = 101
startTime = 900
endTime = 1100
number
startTime
endTime
:CourseOffering
number = 104
startTime = 1300
endTime = 1500
Basic Concepts of Object Orientation
•
•
•
•
•
Object
Class
Attribute
Operation
Relationships
What is an Operation? In UML
CourseOffering
Class
Operation
addStudent
deleteStudent
getStartTime
getEndTime
Basic Concepts of Object Orientation
•
•
•
•
•
Object
Class
Attribute
Operation
Relationships
UML Class Diagrams
• A class diagram is used to show the existence of classes and
their relationships in the logical view of a system
Name
Attributes
Operations
Class diagrams show the classes
of the system, their
interrelationships (including
inheritance, aggregation, and
association), and the operations
and attributes of the classes.
Relations
• Associations
• Aggregation
• Generalization
UML Class Diagrams essentials
• Class Diagrams
• + Public
• - Private
• # protected
ClassName
+ attribute
: type
+ coursename : string
-operationt (): return type
addStudent (): int
• Thier basic relation ships
• Association
• Aggregation
• generalization
Relations Between Classes in UML
• Association -- a relationship between instances of the two
classes. There is an association between two classes if an
instance of one class must know about the other in order to
perform its work. In a diagram, an association is a link
connecting two classes.
• Aggregation (composition) -- an association in which one
class belongs to a collection. An aggregation has a diamond
end pointing to the part containing the whole.
• Generalization -- an inheritance link indicating one class is a
superclass of the other. A generalization has a triangle
pointing to the superclass.
Relationships: Association
Multiplicities
0..1
0..* or *
1
1..*
Meaning
zero or one instance. The notation n . . m indicates n to m instances.
no limit on the number of instances (including none).
exactly one instance
at least one instance
Relationships: Generalization
• A relationship among classes where one class
shares the structure and/or behavior of one or
more classes
• Defines a hierarchy of abstractions in which a
subclass inherits from one or more
superclasses
– Single inheritance
– Multiple inheritance
• Generalization is an “is-a-kind of” relationship
Example: Single Inheritance in UML
• One class inherits from another
Ancestor
Superclass
(parent)
Account
balance
name
number
Withdraw()
CreateStatement()
Generalization
Relationship
Subclasses
Checking
Savings
Withdraw()
GetInterest()
Withdraw()
Descendents
Example: Multiple Inheritance in UML
• A class can inherit from several other classes
FlyingThing
Animal
multiple
inheritance
Airplane
Helicopter
Bird
Wolf
Horse
Use multiple inheritance only when needed, and
always with caution !
Use case diagrams
Use case diagrams are used to depict the context of the
system to be built and the functionality provided by that
system. They depict who (or what) interacts with the
system. They show what the outside world wants the
system to do.
•Essentials of Use case Diagram
Actors, which represent users of a system, including
human users and other systems.
• Use Cases, which represent functionality or services
provided by a system to users.
Example Use Case diagram
System
boundary
Actor:
An actor is a person, organization, or external
system that plays a role in one or more
interactions with your system
System boundary:
indicates the scope of your system. Anything within the
box represents functionality that is in scope and anything
outside the box is not
Actor
Use case
Use case:
A use case describes a sequence of actions that
provide something of measurable value to an
actor and is drawn as a horizontal ellipse
Online shopping
39
Use case example
Sequence Diagram
Sequence Diagrams describe interactions among classes. These
interactions are modeled as exchange of messages. These
diagrams focus on classes and the messages they exchange to
accomplish some desired behavior. Sequence diagrams are a type
of interaction diagrams. Sequence diagrams contain the following
elements:
• Class roles, which represent roles that objects may play within the
interaction.
•Lifelines, which represent the existence of an object over a period
of time. (A dashed vertical line, called the lifeline, is drawn below
each object.)
• Activations, which represent the time during which an object is
performing an operation. Represented by rectangle
• Messages, which represent communication between objects.
The notation used for messages (i.e., the line type and arrowhead
type) indicates the type of message being used,
Sequence Diagram
• A sequence diagram is
– An interaction diagram that details how operations
are carried out.
– What messages are sent and when.
– Sequence diagrams are organized according to time
Example Sequence Diagram
Object: Class
Operations
Message
Lifeline
43
Activity Diagram
• Activity diagrams describe the workflow behaviour of a
system
• Activity diagrams provide visual depictions of the flow of
activities, whether in a system, business, workflow, or other
process.
• Essentials of activity Diagrams:
– Action nodes
– Control nodes
• initial and final
• decision and merge
• fork and joint
Example Activity Diagram : processing
an order.
Start
Fork
Branch
Merge
Joint
End
45
State Transition Diagram
• A State Transition diagram shows the possible states of
the object and the transitions that cause a change in
state.
• State machine diagrams are used to design and
understand time-critical systems (real time
processing) in which the consequences of
improper timing are severe
• Essential
– Initial final and simple states
Example State Transition Diagram
for an Order object
47
Example State Transition Diagram
Example: Elevator Problem
• A product is to be installed to control elevators in a building with m
floors. The problem concerns the logic required to move elevators
between floors according to the following constraints:
– Each elevator has a set of m buttons, one for each floor. These
illuminate when pressed and cause the elevator to visit the
corresponding floor. The illumination is cancelled when the elevator
visits the corresponding floor.
•
– Each floor, except the first floor and top floor has two buttons, one to
request an up-elevator and one to request a down-elevator. These
buttons illuminate when pressed. The illumination is cancelled when
an elevator visits the floor and then moves in the desired direction.
•
– When an elevator has no requests, it remains at its current floor with
its doors closed.
Use case Diagram
• A generalized description of how a system will
be used.
• Provides an overview of the intended
functionality of the system.
• Understandable by laymen as well as
professionals
• Elevator basic scenario that can be extracted from Use Case
Diagram:
–
–
–
–
–
–
–
–
–
–
Passenger pressed floor button
Elevator system detects floor button pressed
Elevator moves to the floor
Elevator doors open
Passenger gets in and presses elevator button
Elevator doors closes
Elevator moves to required floor
Elevator doors open
Passenger gets out
Elevator doors closes
Class Diagram
• Class diagrams show the static structure of the object,
their internal structure, and their relationships.
Sequence Diagram
Sequence Diagram for Serving Elevator Button
Sequence Diagram for Serving Door Button
Detail Class Diagram
Reference
• http://www.coderanch.com/howto/content/uml-example.html
• Recommended books
– Object Oriented Analysis and Design with
Application By Grady Booch , Third Edition
(Chapter 5)
– How to Program c++ By Dietel (our Text book)
Other Examples
Simple Digital Watch
• A simple digital watch has a display and two buttons to
set it, the A button and the B button. Thewatch has
two modes of operation, display time and set time. In
the display time mode, hours andminutes are
displayed, separated by a flashing colon. The set time
mode has two modes, sethours and set minutes. The A
button is used to select modes. Each time A is pressed,
the modeadvances in sequence: display, set hours, set
minutes, display etc. Within the sub modes, the
Bbutton is used to advance the hours or minutes once
each time it is pressed. Buttons must be released
before they can generate another event. Prepare a
state diagram of the watch.
Download