Object-Oriented Design Principles: The Pillars of The Paradigm
Object-Oriented Design Principles: The Pillars of The Paradigm
Design Principles
Encapsulation
Hierarchy
Association, Aggregation
Inheritance
Polymorphism
OOP- 2
What’s OO?
Is it using Objects?
OOP- 3
Measuring Quality
of an Abstraction
OOP- 4
Metrics for class design
Coupling
Cohesion
Law of Demeter
“Methods of a class should not depend in any
way on the structure of any class, except the
immediate structure of their own class.
Further, each method should send messages to
objects belonging to a very limited set of
classes only.”
OOP- 7
Failing LoD
=> drives away shiny new
PaperBoy’s method does: Jaguar!
customer.waller.totalMoney;
OOP- 8
Train Wreck Coding
You may find code that continues to call on objects
returned by methods in sequence
customer.getAddress().getCity().getCounty()...
Hard to understand
Hard to debug
Lacks Cohesion
Bad design
Perils of a bad design
YAGNI
You Aren’t Going To Need It
Application where
Execution UI did business
Engine Font end
validation.
Start out due to flaw in
(chokes on Application Engine.
Once flaw was rectified,
certain took us weeks to fix
names of the UI due to duplication
of validation logic.
objects) OOP- 13
DRY
Some times hard to realize this
Poor maintainability
SRP…
Alarm
Control
UI +alert()
System
+display(Panel)
Related topics:
MVC Alarm
Analysis model stereotypes : Control
+alert() System
Control Entity Boundary
AlarmUI
UI +display(Panel)
OOP- 16
SRP at Module Level
Can be extended to module level as well
Component
GUI Framework GUI Framework
V 1.0 Development V 1.1
Utilities
Throw it in there
GUI Framework
Forced to accept V 1.2
Irrelevant change
User
OOP- 17
OOP- 19
OCP…
Bertrand Meyer:
OOP- 20
OCP…
Characteristics of a poor design:
OOP- 21
OCP…
Software Modules must
OOP- 22
OCP…
Piston
Car
Engine
OOP- 23
OCP…
Abstract
Car
Engine
Piston
Engine
A class must not
Abstraction & Polymorphism depend on a
are the Key Concrete class; it
must depend on an
abstract class OOP- 24
OCP…
Strategic Closure:
OCP…
Heuristics and Conventions that arise from OCP
OOP- 27
Liskov Substitution
Principle
Inheritance is used to realize Abstraction
LSP:
public/is-a
B
!B publicly inherits from (“is-a”) A means
!Every object of type B is also object of type A
!What’s true of object of A is also of object of B
!A represents a more general concept than B
!B represents more specialized concept than A
!anywhere an object of A can be used, an object of B can be used
OOP- 29
Behavior
Advertised Behavior of an object
OOP- 30
Design by Contract
Design by Contract
OOP- 31
LSP
OOP- 32
LSP in Java?
LSP is being used in Java at least in two places
OOP- 33
OOP- 34
Ramifications
Depends
Controller Clock
for Alarm
Alternate Design
Controller IAlarm
Clock Timer
OOP- 37
Inheritance Vs.
Delegation...
OOP- 38
DIP
OOP- 39
The Founding
Principles
The three principles are closely related
Growth of an interface
IMicrowave
C1 +cook(time) C2
+stop()
A few days later,
A few days later,
Client C1 wants it to notify MicrowaveImpl Client C2 wants it to chime
(workaholic client?!)
IMicrowave
C1 +cook(time) C2
+stop()
Clients are forced to
+chime()
use interfaces they
+notify(…)
do not care about. All implementations
May result in greater
must carry the weights
coupling, dependency MicrowaveImpl
to more libraries
OOP- 42
ISP
Interface Segregation Principle
IMicrowave
C1 C2
+cook(time)
+stop()
INotify IChimer
+notify(…) +chime()
MicrowaveImpl1 MicrowaveImpl2
OOP- 43
Reuse/Release
Equivalency Principle
OOP- 44
Reuse/Release
Equivalency Principle
Release
OOP- 45
Reuse/Release
Equivalency Principle
Tracking
OOP- 47
OOP- 49
OOP- 50
Acyclic Dependence Principle
OOP- 51
OOP- 52
Stable Dependency Principle
OOP- 53
Stable Dependency
Principle
A component can never be more stable than the one
it depends upon
where
0"I"1
OOP- 55
Stable Abstraction
Principle
OOP- 56
Stable Abstraction
Principle
Abstraction of a Component
0"A"1
Stability Vs.
Abstractness
A
(0, 1)
Th (1,1)
Highly
eM
Abstractness
instable &
ain abstract
Maximally
Se (Rigid)
stable qu
& abstract en
ce
(1, 0)
0 Instability
Highly
I
Maximally
stable &
instable
Concrete
& concrete
(Rigid) OOP- 58
Distance from the
main sequence
D = |(A + I - 1) / #2 |
Creating objects
Drawing UML
OOP- 61