CHAPTER ONE Problem Solving and the Object- Oriented Paradigm.
Introduction to the Object-Oriented Paradigm
-
Upload
lucian-thompson -
Category
Documents
-
view
183 -
download
21
description
Transcript of Introduction to the Object-Oriented Paradigm
Object-Oriented Paradigm and UML 1
Introduction to the Object-Oriented Paradigm
Object-Oriented Paradigm and UML 2
Objectives
• To explain the essential difference between the procedural and object-oriented (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 3
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.
Data
Operations
Object-Oriented Paradigm and UML 4
The Object-Oriented Paradigm• Data is packaged
(encapsulated) with the operations that manipulate it.
• Example: a stack object– Data store within a
stack object– Operations within the
stack object
Operations
Data
Object-Oriented Paradigm and UML 5
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 6
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 7
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 8
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 9
Objects
An object is an entity that has properties and exhibits behavior.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 10
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.
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 11
Attribute and Operation Examples
lane1
maxSpeed = 25length = 2.3
addVehicle()getClearDistance()getMaxSpeed()
lightA
NSgreenTime = 35EWGreenTime = 65amberTime = 10
tick()getAmberTime()getNSColor()
car54
color = redlocation = lane1distance = 1.22
tick()stop()go()
Object-Oriented Paradigm and UML 12
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 13
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 14
Classes
• 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.
An class is an abstraction of a set of objects
with common attributes and operations.
An class is an abstraction of a set of objects
with common attributes and operations.
Object-Oriented Paradigm and UML 15
Examples of Classes
Lane
maxSpeedlength
addVehicle()getClearDistance()getMaxSpeed()
TraficLight
NSgreenTimeEWGreenTimeamberTime
tick()getAmberTime()getNSColor()
Vehicle
colorlocationdistance
tick()stop()go()
Object-Oriented Paradigm and UML 16
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 17
UML Class and Object Diagrams
Object-Oriented Paradigm and UML 18
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 19
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 20
UML Class Symbol
Phone
numbercolor
java::util::Random
nextBoolean()nextDouble()nextFloat()nextInt()nextLong()setSeed()
Table
Book
authortitleISBN
ring()dial()redial()hangUp()
ExceptionsnoNumberExceptionlowPowerException
Object-Oriented Paradigm and UML 21
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 22
Examples
• weight
• weight : real = 40.0
• signal[*] : real = { 0.0 }
• charge[3,5,7..10]
• hour = 12
Object-Oriented Paradigm and UML 23
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 24
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 25
Attribute and Operation Examples
Player
roundScore : int = 0totalScore : int = 0words : String[*] = ()
resetScores()setRoundScore( in size : int )findWords( in board : Board )getRoundScore() : intgetTotalScore() : intgetWords() : String[*]
WaterHeaterController
mode : HeaterMode = OFFoccupiedTemp : int = 70emptyTemp : int = 55
setMode( newMode : Mode = OFF )setOccupiedTemp( newTemp : int )setEmptyTemp( newTemp : int )clockTick( out ack : Boolean )
Object-Oriented Paradigm and UML 26
Class Diagram Uses
• Central static modeling tool in object-oriented design
• Can be used throughout both the product and engineering design processes
Object-Oriented Paradigm and UML 27
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 28
Object Name Format
• 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-name : class-name [ stateList ]
Object-Oriented Paradigm and UML 29
Object Attribute Format
• attribute-name—simple name
• value—any string
• Any attribute and its current value may be suppressed together.
attribute-name = value
Object-Oriented Paradigm and UML 30
Examples of Object Symbols
:Rectangle
a1 t:Telephone
b:Book[checked out]
width = 720height = 320color = blue
title = “Ivanhoe”author = “Sir Walter Scott”
x = 10y = 14
number = 8792460color = blackstatus = ONHOOK
Object-Oriented Paradigm and UML 31
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 32
UML Diagram Tree
Diagram
StructureDiagram
BehaviorDiagram
Class Diagram
ComponentDiagram
Composite StructureDiagram
Deployment Diagram
Object Diagram
Package Diagram
Interaction Diagram Activity Diagram Use Case DiagramState Machine
Diagram
Sequence Diagram
CommunicationDiagram
Interaction OverviewDiagram
Timing Diagram
Object-Oriented Paradigm and UML 33
Procedural vs. Object-Oriented Thinking
Object-Oriented Paradigm and UML 34
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 35
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 36
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 37
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 38
Class Design
IntStack
store
push()pop()top()isEmpty()isFull()
Object-Oriented Paradigm and UML 39
C++ IntStack Implementation
• IntStack.h• IntStack.cpp• main.c
IntStack
int store[CAPACITY]topIndex = 0
push( v : int )pop() : inttop() : intisEmpty() : BooleanisFull() : Boolean
Object-Oriented Paradigm and UML 40
Example: Ph Monitor
• pH sensors in a vat are monitored
• A control chart is generated
Object-Oriented Paradigm and UML 41
Procedural Approach
• Make a context diagram
• Make data flow diagrams
• Make ERDs
• Make a structure chart
• Implement the code
Object-Oriented Paradigm and UML 42
pH Monitor Context Diagram
UserVat Sensors
control chart
chart parameters
Process pH Monitoring
System
pH readings
Object-Oriented Paradigm and UML 43
pH Monitor Data Flow Diagram
pH readings
1CollectSample
sample sample
window size
chart parameters
current chartparameters
control chart
new chartparameters
new sample sequence
Vat Sensors
2Form Sample
Sequence
Chart Parameters
Samples Sample-Sequence
3Create Control
Chart
sample sequence
User
4Update Chart Parameters
Object-Oriented Paradigm and UML 44
pH Monitor Structure Chart
main
collect sample
read sensor
create chart
display chart
get chart paramters
change chart
parameters
get sample
sequence
pH readingsensor num
window sizechart
parameters
sample sequence
control chart control
chart
Object-Oriented Paradigm and UML 45
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 46
Use Case Diagram
Change Chart
Display Chart
Collect Sample
Object-Oriented Paradigm and UML 47
pH Monitor Class Diagram
UserPanel
alterUCL( )alterLCL( )alterCL( )alterSize( )
ChartUCL : realLCL : realCL : realsize : integer
setUCL( )setLCL( )setCL( )setSize( )display( )
SysClock
getTime( )
SampleList
size : integer
append( )getSize( )getTail( )setToFirst( )getNext( )
SampleMgr
wakeUp( )
SensorphReading : real
getReading( )
Samplepoint : integer[*]time : TimeisFailed : Booleanmean : real
takeReadings( )getTime( )isFailed( )getMean( )
Object-Oriented Paradigm and UML 48
Thank you