SlideShare une entreprise Scribd logo
1  sur  44
Object Oriented Design : Basic principlesPresented by : M. M.Al-Farooque Shubho http://bd.linkedin.com/in/thisisshubho
A classic quote “ Walking on water and developing software from a specification are easy if both are frozen. “ - Edward V. Berard
How a typical development is carried out? We Get and analyze requirement. We develop Some typical layers (DAL, BLL, UI etc.). We Implement each functional requirement in each layer (From UI towards data storage) as is expected. We test and deliver.
What do we do if there is a change? We re-factor codes, and, re-factor a lot. And, what if we are asked to change again? We re-factor again!
Why do we need to re-factor? Because, our codes are not developed in a “Flexible”, “Re-usable” and “Extensible” manner.
What is the universal truth of software world? “Your software is bound to change” Why? Because, your software solves a real life business problem, and, the real life business process evolves and changes - always. Your software does what it is supposed to do today and does it good enough. But, is this smart enough to support “Change”? If not, you don’t have a smartly ddsigned software.
What is a “Smartly designed software? If your software can absorb changes in minimal effort and re-factor, you have a smartly designed software.  Your software does what it is supposed to do today and does it good enough. But, is this smart enough to support “Change”? If not, you don’t have a smartly developed software. A smartly developed software can adjust change easily, it can be extended and it is re-usable.
What defines customer satisfaction? You may satisfy your customer for the time being with a software that does what it is supposed to do only.  But, “Change” is the most obvious thing in business and when the software requires the change and you can’t implement that easily, you are in risk of losing the customer.
What defines customer satisfaction?...continued So, to retain long running and growing business, you need to have satisfied customers. You have to understand How they are satisfied. Customers are satisfied if your software: Works, Keeps working (Important), Can be upgraded easily, Can be re-used to build other software, Flexible.
How to design/develop your software to satisfy your customers? Design your software that meets the criteria mentioned already. How to achieve such design? Apply Object Oriented Design (OOD) to develop your software.
Wait a minute! “You are talking about Object Oriented Design. But, that’s what we do, no? We use classes and objects everywhere. So, what is the big deal?” OK, using classes and objects doesn’t mean that you are really using an Object Oriented Design.
When your design is Object Oriented? You are doing Object Oriented Design if your codes are: Of course, Object Oriented. Re-usable, Can be changed with minimal effort. Can be extended without changing existing codes.
You are not alone Many people have strived already to achieve good Object Oriented Design. They already have identified some basic principles for doing Object Oriented Designs (Some basic inspirations).  They also have identified some common design patterns that are applicable to some common scenario (Based upon the basic principles).
Object Oriented Design : Basic principles There are many design principles out there, but, at the basic level, there are 5 principles, which are abbreviated as the SOLID principles. S = Single Responsibility Principle O = Opened Closed Principle L = Liscov Substitution Principle I = Interface Segregation Principle D = Dependency Inversion Principle
Single Responsibility principle
Single Responsibility principle : Definition  “THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE.” Or, differently said, A class should have one and only one type of responsibility.
Single Responsibility principle : Explained If you have a class that has more than one reason to change, you need to split the class into multiple classes, based upon their responsibility. (Note : That doesn’t mean that, multiple methods cannot be there in a single class. That means, methods in a class should be implemented with a single purpose) Why splitting is important? Because: --Each responsibility is an axis of change. --Codes become coupled if classes have more than one responsibility. So, one change effect another.
Single Responsibility principle : Example Consider the following Rectangle class Two applications are using this Rectangle class:  -Computational Geometry Application uses this class to calculate the Area -Graphical Application uses this class to draw a Rectangle in the UI
Single Responsibility principle : Example This violates SRP design principle as the Rectangle class has two responsibilities: --It calculates the value of the Rectangular area. --It renders the Rectangle in the UI. So, what is the problem? --We must include the GUI in the computational geometry application. While deployment of the geometry application, we must include GUI library. --A change to the Rectangle class for Graphical application may lead to change, build and test the Computational geometry application.
Single Responsibility principle : Example So, what to do? Separate the responsibilities into two different  classes, such as: Rectangle : Defines the area() method. RectangleUI : Inherits the Rectangle class and Defines the Draw() method.
Open-Closed Principle
Open-Closed Principle “SOFTWARE ENTITIES (CLASSES, MODULES, FUNCTIONS, ETC.) SHOULD BE OPEN FOR EXTENSION, BUT CLOSED FOR MODIFICATION.” At the most basic level, that means : You should be able to extend a classes behavior, without modifying it. 1. “Open For Extension” : This means that the behavior of the module/class can be extended and we can make the module behave in new and different ways as the requirements changes, or to meet the needs of new applications. 2. “Closed for Modification” : The source code of such a module is inviolate. No one is allowed to make source code changes to it. It would seem that these two
Open-Closed Principle : Explained Following is an example that doesn’t support the Open-Closed principle In this example, the Client and Server classes are concrete.  So, if for any reason, the Server implementation/class is changed, the Client also needs change.
Open-Closed Principle : Explained Following is the corrected example that supports the Open-Closed principle In this example, there is an Abstract Server class added and client holds reference to the Abstract class. The Concrete Server class implements the Abstract Server class. So, if for any reason, the Server implementation is changed, the Client is likely not to require any change. The Abstract Server class here is closed for modification and the Concrete class implementations here are Open for extension.
Likov's Substitution Principle
Likov's Substitution Principle : Definition “SUBTYPES MUST BE SUBSTITUTABLE FOR THEIR BASE TYPES” Or, differently Said, “Functions that use references to base classes must be able to use objects of derived classes without knowing it”
Likov's Substitution Principle : Explanation In basic Object Oriented Principle, “Inheritance” is usually described as an "is a" relationship. If a “Developer” is a “SoftwareProfessional”, then, the “Developer” class should inherit the “SoftwareProfessional” class. Such “Is a” relationship is very important in class designs, but, its easy to get carried away and end up in wrong design with bad inheritance. The “Likov's Substitution Principle” is a way of ensuring that inheritance is used correctly.
Likov's Substitution Principle : Example Take a look at the following class hierarchy example: Here, the KingFisher class extends the Bird base class and hence, inherits the Fly() method.
Likov's Substitution Principle : Example continued Now take a look at the following example: Ostrich is also a Bird (Definitely it is!) and hence it inherits the Bird class. Now, can it fly? No! Here the design violates the LSP. So, even if in real world this seems natural, in the class design, Ostrich should not inherit the Bird class and there should be a separate class for birds that can’t really fly and Ostrich should inherit that.
Likov's Substitution Principle : Example continued Why The LSP is so important? 1. If LSP is not maintained, class hierarchies would be a mess and if subclass instance was passed as parameter to methods method, strange behavior might occur.  2. If LSP is not maintained, unit tests for the Base classes would never succeed for the subclass.
The Interface segregation principle
The Interface segregation principle : Defined “CLIENTS SHOULD NOT BE FORCED TO DEPEND UPON INTERFACES THAT THEY DO NOT USE.”
The Interface segregation principle : Explained Interfaces with too many methods are less re-usable.  Such "fat interfaces“ with additional useless methods lead to inadvertent coupling between classes. Doing this also introduce unnecessary complexity and reduces maintainability or robustness in the system. The ISP ensures that, Interfaces are developed so that, each of them have their own responsibility and thus they are re-usable.
The Interface segregation principle : Example The following interface is a “Fat interface” which violates the Interface Segregation principle Note that, the IBird Interface have many bird behaviour defined along with the Fly() behaviour. Now, if a Bird class (Say, Ostrich) implements this interface, it has to implement the Fly() behaviour unnecessarily (Because, Ostrich doesn’t fly)
The Interface segregation principle : Example continued.. The “Fat Interface” is broken down into two different interfaces IBird and IFlyingBird where the IFlyingBird inherits the IBird. If there is a bird that can’t fly, (Say, Ostrich), they would implement the IBird interface. And, if there is a bird that can fly (Say, KingFisher), they would implement the IFlyingBird interface.
The Dependency Inversion Principle
The Dependency Inversion Principle : Defined “HIGH LEVEL MODULES SHOULD NOT DEPEND UPON LOW LEVEL MODULES. BOTH SHOULD DEPEND UPON ABSTRACTIONS.”
The Dependency Inversion Principle : Explained Lets consider a real world example Your car is composed of lots of objects like the Engine, the wheels, the Air Conditioner and others things. None of these things are rigidly built within a single thing, rather, each of these things are “Pluggable” so that, When the Engine, or the wheel has problem, you can repair it (Without repairing other things) and you can replace it. While replacement, you just have to ensure that, the Engine/Wheel conforms to the car’s design (Say, the card would accept any 1500 cc engine and will run on any 18 inch wheel) Also, the car might allow you to put a 2000 CC engine in place  of the 1500 CC given the fact that, the manufacturer (Say, Toyota) is the same.
The Dependency Inversion Principle : Explained What if the different parts of your car were not built in such “Pluggable nature” That would be horrible! In that case, if your car’s engine was out of order, you had to fix the whole car or purchase a new one!
The Dependency Inversion Principle : Explained How the “Pluggable nature” is to be achieved? “Abstraction” is the key. In the real world, the Car is the Higher level module/entity and it depends on the Lower level modules/entities like the Engines or Wheels. But, rather than directly depending on the Engines or Wheels, the car depends on the Abstraction of some specification of Engine or Wheels so that, if any Engine or Wheel conforms to the abstraction, these could be assembled with the car and the card would run.
The Dependency Inversion Principle : Example In the above Car  class, notice that, there are two properties and both of these are of abstract type (Interface) , rather than concrete type. So, the Engine and Wheels are pluggable because, the card would accept any object implementing the declared interfaces and that will not require any change in the Car class.
The Dependency Inversion Principle : Benefits If Dependency inversion is not implemented in the code, you run the risk of --Damaging the Higher level codes that uses the lower level classes. --Requiring too much time and effort to change and higher level codes when a change is occurred in the lower level classes. --Producing less-reusable codes.
Other principles There are many other Object Oriented Principles, other than the SOLID principle. Some are: “Composition over Inheritance” : Favor composition over inheritance. “Principle of least knowledge” : The less your class knows, the better. “The Common Closure Principle” : Related classes should be package together “The Stable Abstractions Principle” : The more stable a class is, the more it must consist of abstract class. …
Design patterns The design patterns (Commonly suggested design suggestions in common recurring situation)  are mainly inspired by the Object Oriented Design principles. The Patterns can be thought of “Frameworks” and the OOD principles can be thought of “Specifications” Suggested Study : Design Patterns

Contenu connexe

Tendances

Design principles - SOLID
Design principles - SOLIDDesign principles - SOLID
Design principles - SOLIDPranalee Rokde
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoringkim.mens
 
Introduction to SOLID Principles
Introduction to SOLID PrinciplesIntroduction to SOLID Principles
Introduction to SOLID PrinciplesGanesh Samarthyam
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design PrinciplesSamuel Breed
 
Java Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web ApplicationJava Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web ApplicationIMC Institute
 
Software evolution and maintenance basic concepts and preliminaries
Software evolution and maintenance   basic concepts and preliminariesSoftware evolution and maintenance   basic concepts and preliminaries
Software evolution and maintenance basic concepts and preliminariesMoutasm Tamimi
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#Aditya Kumar Rajan
 
Ch2-Software Engineering 9
Ch2-Software Engineering 9Ch2-Software Engineering 9
Ch2-Software Engineering 9Ian Sommerville
 
SOLID Design Principles applied in Java
SOLID Design Principles applied in JavaSOLID Design Principles applied in Java
SOLID Design Principles applied in JavaIonut Bilica
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General IntroductionAsma CHERIF
 
Object Oriented Design Principles
Object Oriented Design PrinciplesObject Oriented Design Principles
Object Oriented Design PrinciplesThang Tran Duc
 
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 DesignIrwansyah Irwansyah
 

Tendances (20)

Solid principles
Solid principlesSolid principles
Solid principles
 
SOLID principles
SOLID principlesSOLID principles
SOLID principles
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
Design principles - SOLID
Design principles - SOLIDDesign principles - SOLID
Design principles - SOLID
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
Introduction to SOLID Principles
Introduction to SOLID PrinciplesIntroduction to SOLID Principles
Introduction to SOLID Principles
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
 
SOLID
SOLIDSOLID
SOLID
 
Solid principles
Solid principlesSolid principles
Solid principles
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
Java Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web ApplicationJava Web Programming [1/9] : Introduction to Web Application
Java Web Programming [1/9] : Introduction to Web Application
 
Software evolution and maintenance basic concepts and preliminaries
Software evolution and maintenance   basic concepts and preliminariesSoftware evolution and maintenance   basic concepts and preliminaries
Software evolution and maintenance basic concepts and preliminaries
 
Waterfall model in SDLC
Waterfall model in SDLCWaterfall model in SDLC
Waterfall model in SDLC
 
Learning solid principles using c#
Learning solid principles using c#Learning solid principles using c#
Learning solid principles using c#
 
Design patterns tutorials
Design patterns tutorialsDesign patterns tutorials
Design patterns tutorials
 
Ch2-Software Engineering 9
Ch2-Software Engineering 9Ch2-Software Engineering 9
Ch2-Software Engineering 9
 
SOLID Design Principles applied in Java
SOLID Design Principles applied in JavaSOLID Design Principles applied in Java
SOLID Design Principles applied in Java
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
 
Object Oriented Design Principles
Object Oriented Design PrinciplesObject Oriented Design Principles
Object Oriented Design Principles
 
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
 

En vedette

Advanced Object-Oriented/SOLID Principles
Advanced Object-Oriented/SOLID PrinciplesAdvanced Object-Oriented/SOLID Principles
Advanced Object-Oriented/SOLID PrinciplesJon Kruger
 
SOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented DesignSOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented DesignRiccardo Cardin
 
"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design PrinciplesSerhiy Oplakanets
 
Open Closed Principle kata
Open Closed Principle kataOpen Closed Principle kata
Open Closed Principle kataPaul Blundell
 
Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo designConfiz
 
SOLID Principles of Refactoring Presentation - Inland Empire User Group
SOLID Principles of Refactoring Presentation - Inland Empire User GroupSOLID Principles of Refactoring Presentation - Inland Empire User Group
SOLID Principles of Refactoring Presentation - Inland Empire User GroupAdnan Masood
 
The SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design PatternsThe SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design PatternsHayim Makabee
 
SOLID Principles and Design Patterns
SOLID Principles and Design PatternsSOLID Principles and Design Patterns
SOLID Principles and Design PatternsGanesh Samarthyam
 
Solid principles
Solid principlesSolid principles
Solid principlesViet Vu
 
Open Close Principle
Open Close PrincipleOpen Close Principle
Open Close PrincipleThaichor Seng
 
Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)Simone Chiaretta
 
Implementing The Open/Closed Principle
Implementing The Open/Closed PrincipleImplementing The Open/Closed Principle
Implementing The Open/Closed PrincipleSam Hennessy
 
The Solid Principles
The Solid PrinciplesThe Solid Principles
The Solid PrinciplesLuke Smith
 
Patagonia Inc: A Benefit Corp Case Study
Patagonia Inc: A Benefit Corp Case StudyPatagonia Inc: A Benefit Corp Case Study
Patagonia Inc: A Benefit Corp Case StudyLauren Zahringer
 
Success by Challenging Assumptions (Part 2)
Success by Challenging Assumptions (Part 2)Success by Challenging Assumptions (Part 2)
Success by Challenging Assumptions (Part 2)LaDonna Coy
 
Make yourself replaceable at DevOpsCon 2016 Berlin
Make yourself replaceable at DevOpsCon 2016 BerlinMake yourself replaceable at DevOpsCon 2016 Berlin
Make yourself replaceable at DevOpsCon 2016 BerlinErno Aapa
 

En vedette (20)

Advanced Object-Oriented/SOLID Principles
Advanced Object-Oriented/SOLID PrinciplesAdvanced Object-Oriented/SOLID Principles
Advanced Object-Oriented/SOLID Principles
 
SOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented DesignSOLID - Principles of Object Oriented Design
SOLID - Principles of Object Oriented Design
 
"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles
 
Open Closed Principle kata
Open Closed Principle kataOpen Closed Principle kata
Open Closed Principle kata
 
Solid principles of oo design
Solid principles of oo designSolid principles of oo design
Solid principles of oo design
 
SOLID Principles of Refactoring Presentation - Inland Empire User Group
SOLID Principles of Refactoring Presentation - Inland Empire User GroupSOLID Principles of Refactoring Presentation - Inland Empire User Group
SOLID Principles of Refactoring Presentation - Inland Empire User Group
 
SOLID Principles part 1
SOLID Principles part 1SOLID Principles part 1
SOLID Principles part 1
 
The SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design PatternsThe SOLID Principles Illustrated by Design Patterns
The SOLID Principles Illustrated by Design Patterns
 
SOLID Principles and Design Patterns
SOLID Principles and Design PatternsSOLID Principles and Design Patterns
SOLID Principles and Design Patterns
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Open Close Principle
Open Close PrincipleOpen Close Principle
Open Close Principle
 
jeas_0816_4883
jeas_0816_4883jeas_0816_4883
jeas_0816_4883
 
Dependency Inversion Principle
Dependency Inversion PrincipleDependency Inversion Principle
Dependency Inversion Principle
 
Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)
 
Implementing The Open/Closed Principle
Implementing The Open/Closed PrincipleImplementing The Open/Closed Principle
Implementing The Open/Closed Principle
 
The Solid Principles
The Solid PrinciplesThe Solid Principles
The Solid Principles
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Patagonia Inc: A Benefit Corp Case Study
Patagonia Inc: A Benefit Corp Case StudyPatagonia Inc: A Benefit Corp Case Study
Patagonia Inc: A Benefit Corp Case Study
 
Success by Challenging Assumptions (Part 2)
Success by Challenging Assumptions (Part 2)Success by Challenging Assumptions (Part 2)
Success by Challenging Assumptions (Part 2)
 
Make yourself replaceable at DevOpsCon 2016 Berlin
Make yourself replaceable at DevOpsCon 2016 BerlinMake yourself replaceable at DevOpsCon 2016 Berlin
Make yourself replaceable at DevOpsCon 2016 Berlin
 

Similaire à Object Oriented Design SOLID Principles

Software design principles - jinal desai
Software design principles - jinal desaiSoftware design principles - jinal desai
Software design principles - jinal desaijinaldesailive
 
An ultimate guide to SOLID Principles, developers must know.
An ultimate guide to SOLID Principles, developers must know.An ultimate guide to SOLID Principles, developers must know.
An ultimate guide to SOLID Principles, developers must know.ONE BCG
 
DesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatternsDesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatternsBasavaraj Patil
 
SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionKnoldus Inc.
 
Inversion of Control
Inversion of ControlInversion of Control
Inversion of ControlShuhab Tariq
 
Dependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and PatternsDependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and PatternsJuan Lopez
 
SOLID Software Principles with C#
SOLID Software Principles with C#SOLID Software Principles with C#
SOLID Software Principles with C#Ken Burkhardt
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principlesdeonpmeyer
 
Design poo my_jug_en_ppt
Design poo my_jug_en_pptDesign poo my_jug_en_ppt
Design poo my_jug_en_pptagnes_crepet
 
S.O.L.I.D. principles of software development
S.O.L.I.D. principles of software developmentS.O.L.I.D. principles of software development
S.O.L.I.D. principles of software developmentAmanSoni129
 
Design Principles to design Patterns
Design Principles to design PatternsDesign Principles to design Patterns
Design Principles to design PatternsFaizan Haider
 
Design principle vs design patterns
Design principle vs design patternsDesign principle vs design patterns
Design principle vs design patternsPrabhakar Sharma
 
Clearing confusion about IoC (Inversion of Control)
Clearing confusion about IoC (Inversion of Control)Clearing confusion about IoC (Inversion of Control)
Clearing confusion about IoC (Inversion of Control)Mohammed Salah Eldowy
 

Similaire à Object Oriented Design SOLID Principles (20)

Software design principles - jinal desai
Software design principles - jinal desaiSoftware design principles - jinal desai
Software design principles - jinal desai
 
Solid
SolidSolid
Solid
 
An ultimate guide to SOLID Principles, developers must know.
An ultimate guide to SOLID Principles, developers must know.An ultimate guide to SOLID Principles, developers must know.
An ultimate guide to SOLID Principles, developers must know.
 
DesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatternsDesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatterns
 
SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test Automaion
 
OO Design Principles
OO Design PrinciplesOO Design Principles
OO Design Principles
 
Inversion of Control
Inversion of ControlInversion of Control
Inversion of Control
 
Dependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and PatternsDependency Injection, Design Principles and Patterns
Dependency Injection, Design Principles and Patterns
 
Soild principles
Soild principlesSoild principles
Soild principles
 
Android architecture
Android architectureAndroid architecture
Android architecture
 
Solid principles
Solid principlesSolid principles
Solid principles
 
SOLID Software Principles with C#
SOLID Software Principles with C#SOLID Software Principles with C#
SOLID Software Principles with C#
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
Solid principle
Solid principleSolid principle
Solid principle
 
Design poo my_jug_en_ppt
Design poo my_jug_en_pptDesign poo my_jug_en_ppt
Design poo my_jug_en_ppt
 
S.O.L.I.D. principles of software development
S.O.L.I.D. principles of software developmentS.O.L.I.D. principles of software development
S.O.L.I.D. principles of software development
 
Design Principles to design Patterns
Design Principles to design PatternsDesign Principles to design Patterns
Design Principles to design Patterns
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
Design principle vs design patterns
Design principle vs design patternsDesign principle vs design patterns
Design principle vs design patterns
 
Clearing confusion about IoC (Inversion of Control)
Clearing confusion about IoC (Inversion of Control)Clearing confusion about IoC (Inversion of Control)
Clearing confusion about IoC (Inversion of Control)
 

Plus de rainynovember12

Model View Presenter (MVP) In Aspnet
Model View Presenter (MVP) In AspnetModel View Presenter (MVP) In Aspnet
Model View Presenter (MVP) In Aspnetrainynovember12
 
Dependency Injection Inversion Of Control And Unity
Dependency Injection Inversion Of Control And UnityDependency Injection Inversion Of Control And Unity
Dependency Injection Inversion Of Control And Unityrainynovember12
 
Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005rainynovember12
 

Plus de rainynovember12 (8)

Model View Presenter (MVP) In Aspnet
Model View Presenter (MVP) In AspnetModel View Presenter (MVP) In Aspnet
Model View Presenter (MVP) In Aspnet
 
Dependency Injection Inversion Of Control And Unity
Dependency Injection Inversion Of Control And UnityDependency Injection Inversion Of Control And Unity
Dependency Injection Inversion Of Control And Unity
 
Introduction To REST
Introduction To RESTIntroduction To REST
Introduction To REST
 
Sql Basics And Advanced
Sql Basics And AdvancedSql Basics And Advanced
Sql Basics And Advanced
 
Mvc Brief Overview
Mvc Brief OverviewMvc Brief Overview
Mvc Brief Overview
 
Sql Server Basics
Sql Server BasicsSql Server Basics
Sql Server Basics
 
Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005Optimizing Data Accessin Sq Lserver2005
Optimizing Data Accessin Sq Lserver2005
 
Aspnet Caching
Aspnet CachingAspnet Caching
Aspnet Caching
 

Object Oriented Design SOLID Principles

  • 1. Object Oriented Design : Basic principlesPresented by : M. M.Al-Farooque Shubho http://bd.linkedin.com/in/thisisshubho
  • 2. A classic quote “ Walking on water and developing software from a specification are easy if both are frozen. “ - Edward V. Berard
  • 3. How a typical development is carried out? We Get and analyze requirement. We develop Some typical layers (DAL, BLL, UI etc.). We Implement each functional requirement in each layer (From UI towards data storage) as is expected. We test and deliver.
  • 4. What do we do if there is a change? We re-factor codes, and, re-factor a lot. And, what if we are asked to change again? We re-factor again!
  • 5. Why do we need to re-factor? Because, our codes are not developed in a “Flexible”, “Re-usable” and “Extensible” manner.
  • 6. What is the universal truth of software world? “Your software is bound to change” Why? Because, your software solves a real life business problem, and, the real life business process evolves and changes - always. Your software does what it is supposed to do today and does it good enough. But, is this smart enough to support “Change”? If not, you don’t have a smartly ddsigned software.
  • 7. What is a “Smartly designed software? If your software can absorb changes in minimal effort and re-factor, you have a smartly designed software. Your software does what it is supposed to do today and does it good enough. But, is this smart enough to support “Change”? If not, you don’t have a smartly developed software. A smartly developed software can adjust change easily, it can be extended and it is re-usable.
  • 8. What defines customer satisfaction? You may satisfy your customer for the time being with a software that does what it is supposed to do only. But, “Change” is the most obvious thing in business and when the software requires the change and you can’t implement that easily, you are in risk of losing the customer.
  • 9. What defines customer satisfaction?...continued So, to retain long running and growing business, you need to have satisfied customers. You have to understand How they are satisfied. Customers are satisfied if your software: Works, Keeps working (Important), Can be upgraded easily, Can be re-used to build other software, Flexible.
  • 10. How to design/develop your software to satisfy your customers? Design your software that meets the criteria mentioned already. How to achieve such design? Apply Object Oriented Design (OOD) to develop your software.
  • 11. Wait a minute! “You are talking about Object Oriented Design. But, that’s what we do, no? We use classes and objects everywhere. So, what is the big deal?” OK, using classes and objects doesn’t mean that you are really using an Object Oriented Design.
  • 12. When your design is Object Oriented? You are doing Object Oriented Design if your codes are: Of course, Object Oriented. Re-usable, Can be changed with minimal effort. Can be extended without changing existing codes.
  • 13. You are not alone Many people have strived already to achieve good Object Oriented Design. They already have identified some basic principles for doing Object Oriented Designs (Some basic inspirations). They also have identified some common design patterns that are applicable to some common scenario (Based upon the basic principles).
  • 14. Object Oriented Design : Basic principles There are many design principles out there, but, at the basic level, there are 5 principles, which are abbreviated as the SOLID principles. S = Single Responsibility Principle O = Opened Closed Principle L = Liscov Substitution Principle I = Interface Segregation Principle D = Dependency Inversion Principle
  • 16. Single Responsibility principle : Definition “THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE.” Or, differently said, A class should have one and only one type of responsibility.
  • 17. Single Responsibility principle : Explained If you have a class that has more than one reason to change, you need to split the class into multiple classes, based upon their responsibility. (Note : That doesn’t mean that, multiple methods cannot be there in a single class. That means, methods in a class should be implemented with a single purpose) Why splitting is important? Because: --Each responsibility is an axis of change. --Codes become coupled if classes have more than one responsibility. So, one change effect another.
  • 18. Single Responsibility principle : Example Consider the following Rectangle class Two applications are using this Rectangle class: -Computational Geometry Application uses this class to calculate the Area -Graphical Application uses this class to draw a Rectangle in the UI
  • 19. Single Responsibility principle : Example This violates SRP design principle as the Rectangle class has two responsibilities: --It calculates the value of the Rectangular area. --It renders the Rectangle in the UI. So, what is the problem? --We must include the GUI in the computational geometry application. While deployment of the geometry application, we must include GUI library. --A change to the Rectangle class for Graphical application may lead to change, build and test the Computational geometry application.
  • 20. Single Responsibility principle : Example So, what to do? Separate the responsibilities into two different classes, such as: Rectangle : Defines the area() method. RectangleUI : Inherits the Rectangle class and Defines the Draw() method.
  • 22. Open-Closed Principle “SOFTWARE ENTITIES (CLASSES, MODULES, FUNCTIONS, ETC.) SHOULD BE OPEN FOR EXTENSION, BUT CLOSED FOR MODIFICATION.” At the most basic level, that means : You should be able to extend a classes behavior, without modifying it. 1. “Open For Extension” : This means that the behavior of the module/class can be extended and we can make the module behave in new and different ways as the requirements changes, or to meet the needs of new applications. 2. “Closed for Modification” : The source code of such a module is inviolate. No one is allowed to make source code changes to it. It would seem that these two
  • 23. Open-Closed Principle : Explained Following is an example that doesn’t support the Open-Closed principle In this example, the Client and Server classes are concrete. So, if for any reason, the Server implementation/class is changed, the Client also needs change.
  • 24. Open-Closed Principle : Explained Following is the corrected example that supports the Open-Closed principle In this example, there is an Abstract Server class added and client holds reference to the Abstract class. The Concrete Server class implements the Abstract Server class. So, if for any reason, the Server implementation is changed, the Client is likely not to require any change. The Abstract Server class here is closed for modification and the Concrete class implementations here are Open for extension.
  • 26. Likov's Substitution Principle : Definition “SUBTYPES MUST BE SUBSTITUTABLE FOR THEIR BASE TYPES” Or, differently Said, “Functions that use references to base classes must be able to use objects of derived classes without knowing it”
  • 27. Likov's Substitution Principle : Explanation In basic Object Oriented Principle, “Inheritance” is usually described as an "is a" relationship. If a “Developer” is a “SoftwareProfessional”, then, the “Developer” class should inherit the “SoftwareProfessional” class. Such “Is a” relationship is very important in class designs, but, its easy to get carried away and end up in wrong design with bad inheritance. The “Likov's Substitution Principle” is a way of ensuring that inheritance is used correctly.
  • 28. Likov's Substitution Principle : Example Take a look at the following class hierarchy example: Here, the KingFisher class extends the Bird base class and hence, inherits the Fly() method.
  • 29. Likov's Substitution Principle : Example continued Now take a look at the following example: Ostrich is also a Bird (Definitely it is!) and hence it inherits the Bird class. Now, can it fly? No! Here the design violates the LSP. So, even if in real world this seems natural, in the class design, Ostrich should not inherit the Bird class and there should be a separate class for birds that can’t really fly and Ostrich should inherit that.
  • 30. Likov's Substitution Principle : Example continued Why The LSP is so important? 1. If LSP is not maintained, class hierarchies would be a mess and if subclass instance was passed as parameter to methods method, strange behavior might occur. 2. If LSP is not maintained, unit tests for the Base classes would never succeed for the subclass.
  • 32. The Interface segregation principle : Defined “CLIENTS SHOULD NOT BE FORCED TO DEPEND UPON INTERFACES THAT THEY DO NOT USE.”
  • 33. The Interface segregation principle : Explained Interfaces with too many methods are less re-usable. Such "fat interfaces“ with additional useless methods lead to inadvertent coupling between classes. Doing this also introduce unnecessary complexity and reduces maintainability or robustness in the system. The ISP ensures that, Interfaces are developed so that, each of them have their own responsibility and thus they are re-usable.
  • 34. The Interface segregation principle : Example The following interface is a “Fat interface” which violates the Interface Segregation principle Note that, the IBird Interface have many bird behaviour defined along with the Fly() behaviour. Now, if a Bird class (Say, Ostrich) implements this interface, it has to implement the Fly() behaviour unnecessarily (Because, Ostrich doesn’t fly)
  • 35. The Interface segregation principle : Example continued.. The “Fat Interface” is broken down into two different interfaces IBird and IFlyingBird where the IFlyingBird inherits the IBird. If there is a bird that can’t fly, (Say, Ostrich), they would implement the IBird interface. And, if there is a bird that can fly (Say, KingFisher), they would implement the IFlyingBird interface.
  • 37. The Dependency Inversion Principle : Defined “HIGH LEVEL MODULES SHOULD NOT DEPEND UPON LOW LEVEL MODULES. BOTH SHOULD DEPEND UPON ABSTRACTIONS.”
  • 38. The Dependency Inversion Principle : Explained Lets consider a real world example Your car is composed of lots of objects like the Engine, the wheels, the Air Conditioner and others things. None of these things are rigidly built within a single thing, rather, each of these things are “Pluggable” so that, When the Engine, or the wheel has problem, you can repair it (Without repairing other things) and you can replace it. While replacement, you just have to ensure that, the Engine/Wheel conforms to the car’s design (Say, the card would accept any 1500 cc engine and will run on any 18 inch wheel) Also, the car might allow you to put a 2000 CC engine in place of the 1500 CC given the fact that, the manufacturer (Say, Toyota) is the same.
  • 39. The Dependency Inversion Principle : Explained What if the different parts of your car were not built in such “Pluggable nature” That would be horrible! In that case, if your car’s engine was out of order, you had to fix the whole car or purchase a new one!
  • 40. The Dependency Inversion Principle : Explained How the “Pluggable nature” is to be achieved? “Abstraction” is the key. In the real world, the Car is the Higher level module/entity and it depends on the Lower level modules/entities like the Engines or Wheels. But, rather than directly depending on the Engines or Wheels, the car depends on the Abstraction of some specification of Engine or Wheels so that, if any Engine or Wheel conforms to the abstraction, these could be assembled with the car and the card would run.
  • 41. The Dependency Inversion Principle : Example In the above Car class, notice that, there are two properties and both of these are of abstract type (Interface) , rather than concrete type. So, the Engine and Wheels are pluggable because, the card would accept any object implementing the declared interfaces and that will not require any change in the Car class.
  • 42. The Dependency Inversion Principle : Benefits If Dependency inversion is not implemented in the code, you run the risk of --Damaging the Higher level codes that uses the lower level classes. --Requiring too much time and effort to change and higher level codes when a change is occurred in the lower level classes. --Producing less-reusable codes.
  • 43. Other principles There are many other Object Oriented Principles, other than the SOLID principle. Some are: “Composition over Inheritance” : Favor composition over inheritance. “Principle of least knowledge” : The less your class knows, the better. “The Common Closure Principle” : Related classes should be package together “The Stable Abstractions Principle” : The more stable a class is, the more it must consist of abstract class. …
  • 44. Design patterns The design patterns (Commonly suggested design suggestions in common recurring situation) are mainly inspired by the Object Oriented Design principles. The Patterns can be thought of “Frameworks” and the OOD principles can be thought of “Specifications” Suggested Study : Design Patterns