SlideShare ist ein Scribd-Unternehmen logo
1 von 19
OO Design Principles
Key OO Design Principles
 Object Oriented programming’s goal is to make it
possible to write code that is maintainable, easy to
understand, and reusable.
 The principle goal of the OO Principles and Design
patterns is to come up with designs that can, as
easily as possible, be modified when the program
requirements change.
 Following these OO Design Principles will help you
write better OO code.
COUPLING & COHESION
 Is a measure of strength
of association established
by a connection from one
object or s/w component
to another.
 It is a binary
relationship.
 Increase with increase in
complexity of interfaces.
 Is the interaction within
a single object or
software component.
 Reflects the single –
purposeness of an object.
 Highly cohesive
components can lower
coupling.
COUPLING
Interaction coupling.
This involves the
amount and complexity
of messages between
components.
These have only little
interactions.
Inheritance coupling.
Coupling between
super class and subclass
Subclass is coupled
with super class in terms
of attributes and
methods.
Important OO Design Principles
1. Open-Closed (open for extension; closed for
modification) Principle
2. Liskov Substitution Principle (LSP)
3. Dependency Inversion Principle (DIP)
4. Single Responsibility Principle (SRP)
5. Interface Segregation Principle (ISP)
Open-Closed Principle
 Software entities should be open for extension,
but closed for modification.
 Open for extension: behavior can be changed to
suite new requirements
 Closed for modification: module itself doesn’t
change
 Whenever a software system must support a set
of alternatives, ideally only one class in the
system knows the entire set of alternatives
 The Open-Closed Principle (OCP) says that we
should attempt to design modules that never
need to be changed.
 To extend the behavior of the system, we add new
code. We do not modify old code.
 Modules that conform to the OCP meet two
criteria:
 Open For Extension - The behavior of the module can be
extended to meet new requirements
 Closed For Modification - the source code of the module is
not allowed to change
 How can we do this?
 Abstraction, Polymorphism, Inheritance, and Interfaces
OCP
If new shapes are needed,
Circle Square
DrawAllShapes Shape
OCP
Circle Square
DrawAllShapes
Triangle Pentagon
Shape
OCP Violation
If new shapes are needed,
Square
Circle
ShapeDrawAllShapes
OCP Violation
Circle
Square
DrawAllShapes
Triangle
Pentagon
Shape
OCP Summary
 Open for extension
 Add new code for new functionality, don't modify existing
working code
 Concrete implementations of interfaces somewhere
 Closed for modification
 Need to anticipate likely modifications to be able to plan
ahead in the design . e.g. ordering shapes? No closure
against this requirement ... but could be added in a design-
preserving way .
Liskov Substitution Principle (LSP)
 If for each object o1 of type S there is an object
o2 of type T such that for all programs P
defined in terms of T, the behavior of P is
unchanged when o1 is substituted for o2 then S
is a subtype of T.
 Modules that use references to base types must
be able to use references to derived types
without knowing the difference
LSP
 Derived classes must fully substitute base class
 Violating LSP may break OCP
Dependency Inversion Principle (DIP)
 Depends upon abstract, not upon concrete classes
 No variable should hold a reference to a concrete class
 No class should derive from a concrete class
 No method should override an implemented method of its base class
(if it is something that can be changed, separate it into its own class).
 High-level modules should not depend on low-level modules.
DIP Summary
 Use DI to avoid:
 deriving from concrete classes
 associating to or aggregating concrete classes
 dependency on concrete components
Single Responsibility Principle
 There should never be more than one reason for
a class to change.
 Plan your classes based on what is likely to
change. If there is more than one thing that may
change, split the class until there is only one
thing likely to change in each class.
Interface Segregation Principle
 Some classes with good informational cohesion
support many services/operations
 Most clients only need subset of services; this
is like “tramp data!”
 So, split class interface into multiple interfaces
such that most clients use all the operations
they see, and don’t see operations they don’t
need
ISP
 When a class (Server) collects interfaces for various
purposes (Clients)
 Use separate interfaces to hide parts of the Server interface for
Clients
 Similar to data hiding
 Or split the Server in several parts

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (19)

Is your code solid
Is your code solidIs your code solid
Is your code solid
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
Principle of OOD
Principle of OODPrinciple of OOD
Principle of OOD
 
The Solid Principles
The Solid PrinciplesThe Solid Principles
The Solid Principles
 
The Open-Closed Principle - the Original Version and the Contemporary Version
The Open-Closed Principle - the Original Version and the Contemporary VersionThe Open-Closed Principle - the Original Version and the Contemporary Version
The Open-Closed Principle - the Original Version and the Contemporary Version
 
Microservices
MicroservicesMicroservices
Microservices
 
Solid
SolidSolid
Solid
 
SOLID Design Principles applied in Java
SOLID Design Principles applied in JavaSOLID Design Principles applied in Java
SOLID Design Principles applied in Java
 
Open Close Principle
Open Close PrincipleOpen Close Principle
Open Close Principle
 
ALMOsT.js: an Agile Model to Model and Model to Text Transformation Framework
ALMOsT.js: an Agile Model to Model and Model to Text Transformation FrameworkALMOsT.js: an Agile Model to Model and Model to Text Transformation Framework
ALMOsT.js: an Agile Model to Model and Model to Text Transformation Framework
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
OO Inheritance - Not Always Evil - Refactoring to Open-Closed with Inheritance
OO Inheritance - Not Always Evil - Refactoring to Open-Closed with InheritanceOO Inheritance - Not Always Evil - Refactoring to Open-Closed with Inheritance
OO Inheritance - Not Always Evil - Refactoring to Open-Closed with Inheritance
 
Implementing the Adapter Design Pattern
Implementing the Adapter Design PatternImplementing the Adapter Design Pattern
Implementing the Adapter Design Pattern
 
Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo design
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Geecon09: SOLID Design Principles
Geecon09: SOLID Design PrinciplesGeecon09: SOLID Design Principles
Geecon09: SOLID Design Principles
 
Cvl
CvlCvl
Cvl
 
Solid OO & Clean Coding is essential to successful Agile development
Solid OO & Clean Coding is essential to successful Agile developmentSolid OO & Clean Coding is essential to successful Agile development
Solid OO & Clean Coding is essential to successful Agile development
 
Design Patterns - 04 Adapter and Facade Pattern
Design Patterns - 04 Adapter and Facade PatternDesign Patterns - 04 Adapter and Facade Pattern
Design Patterns - 04 Adapter and Facade Pattern
 

Ähnlich wie OO Design Principles

SOLID principles-Present
SOLID principles-PresentSOLID principles-Present
SOLID principles-Present
Quang Nguyen
 

Ähnlich wie OO Design Principles (20)

DesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatternsDesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatterns
 
Android architecture
Android architectureAndroid architecture
Android architecture
 
Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principles
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
Solid
SolidSolid
Solid
 
Inversion of Control
Inversion of ControlInversion of Control
Inversion of Control
 
Design principles to modularise a monolith codebase.pptx
Design principles to modularise a monolith codebase.pptxDesign principles to modularise a monolith codebase.pptx
Design principles to modularise a monolith codebase.pptx
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Design Principles to design Patterns
Design Principles to design PatternsDesign Principles to design Patterns
Design Principles to design Patterns
 
Dependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and PatternsDependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and Patterns
 
Solid
SolidSolid
Solid
 
SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test Automaion
 
Software Design Principles (SOLID)
Software Design Principles (SOLID)Software Design Principles (SOLID)
Software Design Principles (SOLID)
 
Single Responsibility Principle
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility Principle
 
Solid principle
Solid principleSolid principle
Solid principle
 
SOLID principles-Present
SOLID principles-PresentSOLID principles-Present
SOLID principles-Present
 
Solid principes
Solid principesSolid principes
Solid principes
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshop
 
Clean Code .Net Cheetsheets
Clean Code .Net CheetsheetsClean Code .Net Cheetsheets
Clean Code .Net Cheetsheets
 
Solid principles, Design Patterns, and Domain Driven Design
Solid principles, Design Patterns, and Domain Driven DesignSolid principles, Design Patterns, and Domain Driven Design
Solid principles, Design Patterns, and Domain Driven Design
 

Mehr von Anju Kanjirathingal

Mehr von Anju Kanjirathingal (9)

Java withrealworldtechnology
Java withrealworldtechnologyJava withrealworldtechnology
Java withrealworldtechnology
 
resolution in the propositional calculus
resolution in the propositional calculusresolution in the propositional calculus
resolution in the propositional calculus
 
The propositional calculus
The propositional calculusThe propositional calculus
The propositional calculus
 
microprocessor
microprocessormicroprocessor
microprocessor
 
int 21 h for screen display
int 21 h for screen displayint 21 h for screen display
int 21 h for screen display
 
int 21,16,09 h
int 21,16,09 hint 21,16,09 h
int 21,16,09 h
 
Intel 80286
Intel 80286Intel 80286
Intel 80286
 
TCP/IP
TCP/IPTCP/IP
TCP/IP
 
2 3 tree
2 3 tree2 3 tree
2 3 tree
 

Kürzlich hochgeladen

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Kürzlich hochgeladen (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 

OO Design Principles

  • 2. Key OO Design Principles  Object Oriented programming’s goal is to make it possible to write code that is maintainable, easy to understand, and reusable.  The principle goal of the OO Principles and Design patterns is to come up with designs that can, as easily as possible, be modified when the program requirements change.  Following these OO Design Principles will help you write better OO code.
  • 3. COUPLING & COHESION  Is a measure of strength of association established by a connection from one object or s/w component to another.  It is a binary relationship.  Increase with increase in complexity of interfaces.  Is the interaction within a single object or software component.  Reflects the single – purposeness of an object.  Highly cohesive components can lower coupling.
  • 4. COUPLING Interaction coupling. This involves the amount and complexity of messages between components. These have only little interactions. Inheritance coupling. Coupling between super class and subclass Subclass is coupled with super class in terms of attributes and methods.
  • 5. Important OO Design Principles 1. Open-Closed (open for extension; closed for modification) Principle 2. Liskov Substitution Principle (LSP) 3. Dependency Inversion Principle (DIP) 4. Single Responsibility Principle (SRP) 5. Interface Segregation Principle (ISP)
  • 6. Open-Closed Principle  Software entities should be open for extension, but closed for modification.  Open for extension: behavior can be changed to suite new requirements  Closed for modification: module itself doesn’t change  Whenever a software system must support a set of alternatives, ideally only one class in the system knows the entire set of alternatives
  • 7.  The Open-Closed Principle (OCP) says that we should attempt to design modules that never need to be changed.  To extend the behavior of the system, we add new code. We do not modify old code.  Modules that conform to the OCP meet two criteria:  Open For Extension - The behavior of the module can be extended to meet new requirements  Closed For Modification - the source code of the module is not allowed to change  How can we do this?  Abstraction, Polymorphism, Inheritance, and Interfaces
  • 8. OCP If new shapes are needed, Circle Square DrawAllShapes Shape
  • 10. OCP Violation If new shapes are needed, Square Circle ShapeDrawAllShapes
  • 12. OCP Summary  Open for extension  Add new code for new functionality, don't modify existing working code  Concrete implementations of interfaces somewhere  Closed for modification  Need to anticipate likely modifications to be able to plan ahead in the design . e.g. ordering shapes? No closure against this requirement ... but could be added in a design- preserving way .
  • 13. Liskov Substitution Principle (LSP)  If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.  Modules that use references to base types must be able to use references to derived types without knowing the difference
  • 14. LSP  Derived classes must fully substitute base class  Violating LSP may break OCP
  • 15. Dependency Inversion Principle (DIP)  Depends upon abstract, not upon concrete classes  No variable should hold a reference to a concrete class  No class should derive from a concrete class  No method should override an implemented method of its base class (if it is something that can be changed, separate it into its own class).  High-level modules should not depend on low-level modules.
  • 16. DIP Summary  Use DI to avoid:  deriving from concrete classes  associating to or aggregating concrete classes  dependency on concrete components
  • 17. Single Responsibility Principle  There should never be more than one reason for a class to change.  Plan your classes based on what is likely to change. If there is more than one thing that may change, split the class until there is only one thing likely to change in each class.
  • 18. Interface Segregation Principle  Some classes with good informational cohesion support many services/operations  Most clients only need subset of services; this is like “tramp data!”  So, split class interface into multiple interfaces such that most clients use all the operations they see, and don’t see operations they don’t need
  • 19. ISP  When a class (Server) collects interfaces for various purposes (Clients)  Use separate interfaces to hide parts of the Server interface for Clients  Similar to data hiding  Or split the Server in several parts