LECTURE 2: The Object Model
Ivan Marsic
Rutgers University
Topics
• Objects and Method Calls
• Interfaces
• UML Notation
• Object Relationships
• Process/Algorithm –Oriented vs. Object
Oriented Approaches
Objects, Calling & Answering Calls
elmer.areCoprimes( Prime factorization:
905, 1988 905 = 5 181
) 1988 = 2 2 7 71
Result:
YES!
Stu Elmer
Prime factorization of 905:
5181 (2 distinct factors)
Prime factorization of 1988:
22771 (4 factors, 3 distinct)
Two integers are said to be coprime or relatively prime if they have no common
factor other than 1 or, equivalently, if their greatest common divisor is 1.
Objects Don’t Accept Arbitrary Calls
Acceptable calls are defined by object “methods”
(a.k.a. Operations, Procedures, Subroutines, Functions)
Object:
method-1: method-2: method-3:
ATM machine
Accept card Read code Take selection
1234
5678
1
4 2
7 5 3
8 6
0 9
12345
1
4 2
7 5 3
8 6
0 9
Object Interface
Interface defines method “signatures”
Method signature: name, parameters, parameter types, return type
Interface
attributes
method-1 Object hides its
state (attributes).
method-2 The attributes
are accessible
only through the
method-3 interface.
Clients, Servers, Messages
Client
Client Object
Object Server
Server
Object
Object
Message
• Objects send messages by calling methods
• Client object: sends message and asks for service
• Server object: provides service” and returns result
Interfaces
• An interface is a set of functional properties
(services) that a software object provides or
requires.
• Methods define the “services” the server object
implementing the interface will offer
• The methods (services) should be created and
named based on the needs of client objects that will
use the services
• “On-demand” design—we “pull” interfaces and their implementations into
existence from the needs of the client, rather than “pushing” out the features
that we think a class should provide
Objects are Modules
Software Module
Inputs State Outputs
(e.g., force) (represented by (e.g., force)
state variables,
e.g.,
momentum,
mass, size, …)
Modules versus Objects
Modules are loose groupings of subprograms and data
“Promiscuous”
access to data
Subprograms often results in
(behavior) misuse
Data
(state)
Software Module 1 Software Module 2 Software Module 3
Objects encapsulate data
Attributes
/data
Methods (state)
(behavior)
Software Object 1 Software Object 2 Software Object 3
UML Notation for Classes
Software Interface Implementation
Software Class
«interface»
Inheritance
BaseInterface
relationship:
ClassName BaseInterface
+ operation()
is implemented
# attribute_1 : int by two classes
Three compartments: # attribute_2 : boolean
# attribute_3 : String
1. Classifier name
+ operation_1() : void
+ operation_2() : String Class1Implement Class2Implement
2. Attributes
+ operation_3(arg1 : int)
+ operation() + operation()
3. Operations
Object Relationships (1)
• Composition: using instance variables that are references to other objects
• Inheritance: inheriting common properties through class extension
Base Class A Derived Class B Base Class A
+ operation() + operation() + operation()
Composition
Derived Class B
B acts as “front-end” for A and uses services of A
+ operation()
(i.e., B may implement the same interface as A)
Inheritance
Object Relationships (2)
• Both inheritance and composition extend the
base functionality provided by another object
• INHERITANCE: Change in the “base” class
propagates to the derived class and its client
classes
– BUT, any code change has a risk of unintentional
introducing of bugs.
• COMPOSITION: More adaptive to change,
because change in the “base” class is easily
“contained” and hidden from the clients of the
front-end class
Object-Oriented versus
Process-Oriented Approaches
System
1
2
3
4
5
X
Y
Key
Key unlock() Lock
Lock turnOn() Light
Light
(b) Checker
Checker Ctrl
Ctrl Ctrl
Ctrl
Valid No
key
?
(a) Yes unlock() turnOn()
Unlock the
Key
Key Lock
Lock Light
Light
lock (c) Checker
Checker Ctrl
Ctrl Ctrl
Ctrl
Turn the
light on
Process oriented Object oriented
Object vs. Process-Oriented (1)
• Process-oriented is more intuitive because it is
person-centric
– thinking what to do next, which way to go
• Object-oriented may be more confusing
because of labor-division
– Thinking how to break-up the problem into tasks,
assign responsibilities, and coordinate the work
– It’s a management problem…
Object vs. Process-Oriented (2)
• Process-oriented does not scale to complex,
large-size problems
– Individual-centric, but…
• Large scale problems require organization of
people instead of individuals working alone
• Object-oriented is organization-centric
– But, hard to design well organizations…
How To Design Well OO
Systems?
• That’s the key topic of this course!
• Decisive Methodological Factors:
– Traceability
– Testing (Section 2.1.2)
– Measurement
– Security
Traceability (1)
Requirements Use Cases Concepts/Objects Source Code
UC-1 CO-1 Code-1
Req-1
CO-2 Code-2
UC-2
CO-3 Code-3
UC-M CO-S Code-W
Req-K
UC-N CO-T Code-X
Requirements
Engineering Use Cases OOA/OOD Implementation
(Section 2.2) (Section 2.3) (Sections 2.4 & 2.5) (Section (2.7)
It should be possible to trace the evolution of the system, step-by-step,
from individual requirements, through design objects, to code blocks.
Traceability (2)
Avoid inexplicable leaps!
…where did this come from?!
“Deus ex machina”
Testing (1)
• Test-Driven Development (TDD)
• Every step in the development process must
start with a plan of how to verify that the
result meets a goal
• The developer should not create a software
artifact (a system requirement, a UML diagram, or source
code) unless they know how it will be tested
But, testing is not enough…
Testing (2)
…it’s fragile—
A Rube Goldberg machine follows works correctly
Test-Driven Development (TDD) for one scenario
—the test case is always described
Automatic alarm clock Oversleeping cure
Measuring (1)
• We need tools to monitor 0 200 400 600
the product quality
1 : 10
1500 1400 1300 1200
• And tools to monitor the
developers productivity
45
90 45
But, measuring is not enough…
Measuring (2)
Maurits Escher designs, work under all scenarios (incorrectly)
—robust but impossible
Relativity Waterfall
Security
Conflicting needs
of computer security…
Microsoft Security Development Lifecycle (SDL)
http://www.microsoft.com/security/sdl/