Raghavaraju Kudari: Esign Atterns Evel
Raghavaraju Kudari: Esign Atterns Evel
1 A Presentation by
Raghavaraju Kudari
SOFTWARE COMPLEXITY
Most of the objects in the world exhibit great complexity e.g.
photosynthesis process, heart beating etc.
In the same way, Software is also complex, especially industry
strength software , such as air traffic controller, database
systems that maintain the integrity
Such complexity if too great for one person to comprehend
and this complexity can never be eliminated, although it can be
tamed.
2
WHY SOFTWARE IS COMPLEX?
GradyBooch derives four elements that are responsible for
the complexity of software.
I. Complexity of the Problem Domain
Unavoidable Domain Complexity
Impedance mismatch between Users & Developers
4
HOW TO RESOLVE COMPLEXITY?
9
ABSTRACTION EXAMPLE
10
ENCAPSULATION
Encapsulation hides the internal details of an object.
11
MODULARITY
Modularity packages abstractions into discrete units which are
developed independently.
12
HIERARCHY
In “is a” relationship, a subclass will inherit the structure and
behavior of its super class.
13
HIERARCHY: “IS A” RELATIONSHIP
14
HIERARCHY
Abstractions form a hierarchy.
In this example the toy mice
are composed of other objects
and signifies “part of ”
relationship.
15
HIERARCHY: “HAS A” RELATIONSHIP
16
POLYMORPHISM EXAMPLE
All movable objects can Move
All objects that Move can be represented by the super class
“MovableObject”
The exact object represented need not be known.It will
respond automatically to the Move command, and in its own
way.
18
POLYMORPHISM
OO languages represent Polymorphism in two ways.
1. Compile time Polymorphism or Parametric
Polymorphism
Means that types will be resolved at compile
time.
Example is overloading
22
CASE STUDY
23
CASE STUDY
24
CASE STUDY
25
CASE STUDY
26
THE SINGLE RESPONSIBILITY PRINCIPLE
There should never be more than one reason for a class to
change
Responsibility is defined as “a reason for change”. If a class
have more than one reason to change, then the class has more
than one responsibility.
It is important to separate the responsibilities in separate
classes, so that the change in one does not manifest itself into
other.
If a class has more than one responsibility, then the
responsibilities become coupled. This leads to a fragile design.
Therefore if a class has more than one responsibility that can
change in future, separate it and encapsulate it (in another
class). 27
CASE STUDY
28
CASE STUDY
29
SOLUTION
30
THE OPEN CLOSED PRINCIPLE
A module should be open for extension, but closed for modification
This
is most important design principle.
Modules implementing OCP have two attributes.
They are open for extension and their behavior can be changed
through extension. In other words , we can change what the module
does through extension.
They are closed for modification. Extending the behavior of the
module does not result in a change to the source or binary of the
module.
The question that comes up is how to change the behavior of
a module without modifying it? The answer is to create
abstraction that represents a group of possible behaviors.
Through polymorphism, the client can use any of the
subclasses of the abstraction without depending directly on 31
them.
THE OPEN CLOSED PRINCIPLE
34
EXAMPLE
35
EXAMPLE
36
CONCLUSION
The Open-Closed Principle is at the heart of object
oriented design. Liskov Substitution Principle is an
important feature of all programs that conform to
Open - Closed Principle.
It is only when derived types are completely
substitutable for their base types that functions that
use those base types can be reused without problems.
This means existing code will work without any
changes with the future subclasses.
It enables us to change the derived types without
effecting the other parts of the application. 37
DEPENDENCY INVERSION PRINCIPLE
Program to an interface, not an implementation
The powerful interpretation of DIP is simple:
"Depend upon abstractions". This means that you
should not depend on a concrete class.
No variable should hold a reference to a concrete
class
No class should derive from a concrete class
DIP states the primary mechanism, if OCP states the
goal of OO architecture.
Abstractionsserve as "hinge points" where the design
can be bent or extended without themselves being 38
modified.
DEPENDENCY INVERSION PRINCIPLE
Consider an application that regulates the
temperature of a furnace.
The regulator reads the temperature from the
thermometer & regulates the temperature of the
furnace
39
CASE STUDY
The high level modules contain the important policy
decisions and business models of an application and
represent the identity of the application.
In such high level modules depend on low level
modules, then any change to the low level modules
will cause the high level modules to change.
This dependency needs to be inverted so that the low
level modules are dependent on the high level
modules.
40
APPLYING DIP
This is how the application
looks after applying DIP.
Regulator can now be used
with any kind of sensor &
heater.
The low level modules
(Thermometer, Furnace) are
now dependent on high level
module (Regulator)
The dependency has been
inverted 41
CASE STUDY
A programmer is creating a library
for I/O
Depending on the requirement, the
clients can use either TextFile or the
StreamFile
The design follows OCP,LSP &
DIP
Clients use the classes through an
interface
A function taking IFile reference will
still work if passed a TextFile or
StramFile object
More classes can be added without 42
44
CASE STUDY
The following function will work fine as long as a TextFile or
StreamFile objects are passed to it.
But it would fail if a Socket or Pipe object is passed to it
because it will throw Exception. So its violates LSP.
The client is not wrong in assuming the correctness of
getSize() method for derivates of IFile, it is the IFile
interface that is violating the part of the contract.
It contains functions which are not applicable to all of its
derived classes.
Such interface is called as a fat interface contains methods
which are not useful to some of its clients.
45
INTERFACE SEGREGATION PRINCIPLE
Many client specific interfaces are better than one general
purpose interface
Clients should not be forced to depend on methods they
do not use.
This principal deals with the disadvantages of fat interfaces
It suggests that interfaces can be broken up into groups of
functions and each group will serve a different set of clients.
If ever, a client exerts a change one the interface, other clients
will be unaffected and the impact of change is minimized.
46
APPLYING ISP
Application of this principle leads to interfaces that are
clean, small and easily maintainable
Such principles do not violate any of the OO Principles and
lead to a better design.
47
48