Object Oriented Design SOLID Principles


Published on

Object Oriented Design - Discussion on basic principles

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Object Oriented Design SOLID Principles

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