The Object-Oriented Paradigm

advertisement
Introduction to the ObjectOriented Paradigm
Object-Oriented Paradigm and UML
1
Objectives
• To explain the essential difference
between the procedural and objectoriented (OO) paradigms
• To summarize the advantages and
disadvantages of the OO paradigm
• To explain the basics concepts of the
OO paradigm: objects, classes,
attributes, and operations
Object-Oriented Paradigm and UML
2
The Procedural Paradigm
• Operations are
separate from data
but manipulate it.
• Example: A stack data
type
– Data store (array,
linked list)
– Separate operations
for pushing, popping,
etc.
Object-Oriented Paradigm and UML
Data
Operations
3
The Object-Oriented Paradigm
• Data is packaged
(encapsulated) with
the operations that
manipulate it.
• Example: a stack
object
Data
Operations
– Data store within a
stack object
– Operations within the
stack object
Object-Oriented Paradigm and UML
4
Encapsulation Advantages 1
• Problem and solution similarity—
Software objects are like real-world
things.
• Abstraction—Data structures and
algorithm details are ignored in design.
• Information hiding—Implementations
can be truly hidden.
(Shielding the internal details of a program
unit’s processing from other program units)
Object-Oriented Paradigm and UML
5
Encapsulation Advantages 2
• Coupling—Operations are not related
through shared data structures.
(The degree of connection between pairs of
modules)
• Cohesion—It’s easier to keep related
operations and data together.
(The degree to which a module’s parts are
related to one another)
Object-Oriented Paradigm and UML
6
Encapsulation Advantages 3
• Reusability—It’s easier to reuse objects
than collections of operations and
various data structures.
• Scalability—Objects can be of arbitrary
size.
• New features—The object-oriented
paradigm offers new and powerful ways
of doing things, as we will see.
Object-Oriented Paradigm and UML
7
Problems with the OO Paradigm
• It’s a lot more complicated than the
procedural paradigm.
– Harder to learn
– Harder to use properly
Object-Oriented Paradigm and UML
8
Objects
An object is an entity that has
properties and exhibits behavior.
• Things are encapsulated when they are
packaged together in a container.
• Objects encapsulate properties and behavior.
• Ideally, objects model things in the world.
– Example: Traffic simulation
Object-Oriented Paradigm and UML
9
Attributes and Operations
An attribute is a named data value
held by an object or a class.
An operation is a named
object or class behavior.
A feature is an attribute or operation.
Object-Oriented Paradigm and UML
10
Attribute and Operation Examples
lane1
maxSpeed = 25
length = 2.3
addVehicle()
getClearDistance()
getMaxSpeed()
lightA
car54
NSgreenTime = 35
EWGreenTime = 65
amberTime = 10
color = red
location = lane1
distance = 1.22
tick()
getAmberTime()
getNSColor()
tick()
stop()
go()
Object-Oriented Paradigm and UML
11
Object Characteristics
• Every object has its own attributes and
operations, though these may be the
same as some other objects.
• Every object has a unique identity.
– Two objects with the same attributes
holding the same values and behaviors are
distinct.
Object-Oriented Paradigm and UML
12
Messages
• Objects communicate by sending one
another messages.
• Messages are abstractions of function
calls, signals, exceptions, etc.
• Ideally, objects send messages that
model real-world communications
between things.
– Example: Traffic simulation
Object-Oriented Paradigm and UML
13
Classes
An class is an abstraction of a set of objects
with common attributes and operations.
• Classes have attributes and operations.
• A class is not a set of objects—it is more
like a blueprint or a specification.
• Objects are said to be instances of classes.
Object-Oriented Paradigm and UML
14
Examples of Classes
Lane
maxSpeed
length
addVehicle()
getClearDistance()
getMaxSpeed()
Object-Oriented Paradigm and UML
TraficLight
Vehicle
NSgreenTime
EWGreenTime
amberTime
color
location
distance
tick()
getAmberTime()
getNSColor()
tick()
stop()
go()
15
Classes and Abstract Data Types
• ADT: a set of values (the carrier set) and
a collection of operations to manipulate
them
• A class forms a type but not an ADT
• Mathematical entities that can be
implemented procedurally or using the
OO paradigm
• OO techniques provide an excellent way
to implement ADTs
Object-Oriented Paradigm and UML
16
UML Class and Object
Diagrams
Object-Oriented Paradigm and UML
17
UML
• Unified Modeling Language
• Developed by Grady Booch, Ivar
Jacobson, and James Rumbaugh (the
three amigos) at Rational Software
Corporation (1994)
– Goal to produce a standard OO analysis
and design notation
• Now a standard controlled by the Object
Management Group (OMG)
Object-Oriented Paradigm and UML
18
UML Names
• A name in UML is character string that
identifies a model element.
– Simple name: sequence of letters, digits, or
punctuation characters
– Composite name: sequence of simple names
separated by the double colon (::)
• Examples
– Java::util::Vector
– veryLongNameWithoutPunctuationCharacters
– short_name
Object-Oriented Paradigm and UML
19
UML Class Symbol
Table
Book
author
title
ISBN
Phone
number
color
ring()
dial()
redial()
hangUp()
java::util::Random
nextBoolean()
nextDouble()
nextFloat()
nextInt()
nextLong()
setSeed()
Exceptions
noNumberException
lowPowerException
Object-Oriented Paradigm and UML
20
Attribute Specification Format
name : type [ multiplicity ] = initial-value
• name—simple name, cannot be suppressed
• type—any string, may be suppressed with the :
• multiplicity—number of values stored in attribute
–
–
–
–
–
–
list of ranges of the form n..k, such that n <=k
k may be *
n..n is the same as n
0..* is the same as *
1 by default
if suppressed, square brackets are omitted
• initial-value—any string, may be suppressed along
with the =
Object-Oriented Paradigm and UML
21
Examples
•
•
•
•
•
weight
weight : real = 40.0
signal[*] : real = { 0.0 }
charge[3,5,7..10]
hour = 12
Object-Oriented Paradigm and UML
22
Operation Specification Format
name( parameter-list ) : return-type-list
• name—simple name, cannot be suppressed
• parameter-list
–
–
–
–
–
direction param-name : param-type = default-value
direction—in, out, inout, return; in when suppressed
param-name—simple name; cannot be suppressed
param-type—any string; cannot be suppressed
default-value—any string; if suppressed, so is =
• return-type-list—any comma-separated list of
strings; if omitted (with :) indicates no return value
• The parameter-list and return-type-list may be
suppressed together.
Object-Oriented Paradigm and UML
23
Examples
•
•
•
•
•
•
rotate()
rotate( degrees : real )
rotate( in degrees : real = 0.0 ) : Boolean
rotate( inout degrees : real )
print( s : String[*] ) : void
split( s : String ) : int, String[*]
Object-Oriented Paradigm and UML
24
Attribute and Operation Examples
Player
WaterHeaterController
roundScore : int = 0
totalScore : int = 0
words : String[*] = ()
mode : HeaterMode = OFF
occupiedTemp : int = 70
emptyTemp : int = 55
resetScores()
setRoundScore( in size : int )
findWords( in board : Board )
getRoundScore() : int
getTotalScore() : int
getWords() : String[*]
setMode( newMode : Mode = OFF )
setOccupiedTemp( newTemp : int )
setEmptyTemp( newTemp : int )
clockTick( out ack : Boolean )
Object-Oriented Paradigm and UML
25
Class Diagram Uses
• Central static modeling tool in objectoriented design
• Can be used throughout both the
product and engineering design
processes
Object-Oriented Paradigm and UML
26
Object Diagrams
• Object diagrams are used much less
often than class diagrams.
• Object symbols have only two
compartments:
– Object name
– Attributes (may be suppressed)
Object-Oriented Paradigm and UML
27
Object Name Format
object-name : class-name
[ stateList ]
• object-name—simple name
• class-name—a name (simple or composite)
• stateList—list of strings; if suppressed, the
square brackets are omitted
• The object-name and class-name may both
be suppressed, but not simultaneously.
Object-Oriented Paradigm and UML
28
Object Attribute Format
attribute-name = value
• attribute-name—simple name
• value—any string
• Any attribute and its current value
may be suppressed together.
Object-Oriented Paradigm and UML
29
Examples of Object Symbols
:Rectangle
width = 720
height = 320
color = blue
a1
x = 10
y = 14
Object-Oriented Paradigm and UML
b:Book
[checked out]
title = “Ivanhoe”
author = “Sir Walter Scott”
t:Telephone
number = 8792460
color = black
status = ONHOOK
30
Object Diagram Uses
• Show the state of one or more objects
at a moment during execution
• Dynamic models as opposed to class
diagrams, which are static models
(A static model represents aspects of a
program that do not change during
execution.)
(A dynamic model represents aspects of a
program that change during execution.)
Object-Oriented Paradigm and UML
31
UML Diagram Tree
Diagram
Structure
Diagram
Behavior
Diagram
Class Diagram
Interaction Diagram
Activity Diagram
Component
Diagram
Sequence Diagram
Composite Structure
Diagram
Communication
Diagram
Deployment Diagram
Interaction Overview
Diagram
Object Diagram
Timing Diagram
Package DiagramParadigm and UML
Object-Oriented
Use Case Diagram
State Machine
Diagram
32
Procedural vs. Object-Oriented
Thinking
Object-Oriented Paradigm and UML
33
Stack of int ADT
• Carrier set: set of all stacks of ints
– Empty stack
– #0, #1, #2, …
– #00, #01, #10, …
–…
• Operations: push(), pop(), top(),
isEmpty(), isFull(), …
Object-Oriented Paradigm and UML
34
Procedural Approach
• Determine a way to represent the
elements of the carrier set (a data
structure)
• Determine how to implement the
operations (procedures)
• Implement the data structure and
procedures in code
Object-Oriented Paradigm and UML
35
C stackOfInt Implementation
struct StackOfInt
int store[CAPACITY]
topIndex = 0
createStackOfInt()
deleteStackOfInt()
push()
pop()
top()
isEmpty()
isFull()
• stackOfInt.h
• stackOfInt.c
• main.c
Object-Oriented Paradigm and UML
36
Object-Oriented Approach
• Encapsulate the carrier set and the
operations in a class
• Determine how to implement the carrier
set and operations
• Implement the class in code
Object-Oriented Paradigm and UML
37
Class Design
IntStack
store
push()
pop()
top()
isEmpty()
isFull()
Object-Oriented Paradigm and UML
38
C++ IntStack Implementation
IntStack
int store[CAPACITY]
topIndex = 0
• IntStack.h
• IntStack.cpp
• main.c
push( v : int )
pop() : int
top() : int
isEmpty() : Boolean
isFull() : Boolean
Object-Oriented Paradigm and UML
39
Example: Ph Monitor
• pH sensors in a vat are monitored
• A control chart is generated
Object-Oriented Paradigm and UML
40
Procedural Approach
•
•
•
•
•
Make a context diagram
Make data flow diagrams
Make ERDs
Make a structure chart
Implement the code
Object-Oriented Paradigm and UML
41
pH Monitor Context Diagram
chart
parameters
Vat
Sensors
pH readings
Process pH
Monitoring
System
User
control chart
Object-Oriented Paradigm and UML
42
pH Monitor Data Flow
Diagram
current chart
parameters
Vat
Sensors
4
Update Chart
Parameters
new chart
parameters
User
Chart Parameters
pH readings
1
Collect
Sample
sample
chart parameters
control chart
window size
2
Form Sample
Sequence
sample
new sample
sequence
Samples
Object-Oriented Paradigm and UML
3
Create Control
Chart
sample
sequence
Sample-Sequence
43
pH Monitor Structure Chart
main
control
chart
collect
sample
control
chart
create
chart
chart
parameters
sensor num
display
chart
change
chart
parameters
window size
pH reading
sample
sequence
read
sensor
get chart
paramters
Object-Oriented Paradigm and UML
get
sample
sequence
44
How to describe Requirement
Result on OO?
• Use UML diagram
• Sometime you can use Use Case
diagram as the first diagram.
• Move into another diagram as needed.
Object-Oriented Paradigm and UML
45
Use Case Diagram
Change Chart
Display Chart
Collect Sample
Object-Oriented Paradigm and UML
46
pH Monitor Class Diagram
Chart
UCL : real
LCL : real
CL : real
size : integer
setUCL( )
setLCL( )
setCL( )
setSize( )
display( )
SampleList
size : integer
append( )
getSize( )
getTail( )
setToFirst( )
getNext( )
Object-Oriented Paradigm and UML
UserPanel
alterUCL( )
alterLCL( )
alterCL( )
alterSize( )
SampleMgr
wakeUp( )
SysClock
getTime( )
Sample
point : integer[*]
time : Time
isFailed : Boolean
mean : real
Sensor
phReading : real
getReading( )
takeReadings( )
getTime( )
isFailed( )
getMean( )
47
Thank you
Object-Oriented Paradigm and UML
48
Download