OO Design Principles                                                                João Miguel Pereira                   ...
OO Design Principles   •    Single Responsibility Principle (SRP)   •    Open Closed Principle (OCP)   •    Liskov Substit...
OO Design Principles   •    Single Responsibility Principle (SRP)   •    Open Closed Principle (OCP)   •    Liskov Substit...
Single Responsibility Principle   • Tom DeMarco and Meilir Page-Jones call it     Cohesion          – The Practical Guide ...
Single Responsibility Principle      There shouldn’t never be more than one           reason for a class to change!       ...
Single Responsibility Principle              Responsability                                Class implementing             ...
Single Responsibility Principle                             Change in responsability              Responsability          ...
Single Responsibility Principle              Ball has one reason to change!              Responsability                   ...
Single Responsibility Principle                             Change in responsability              Responsability          ...
Single Responsibility Principle                Player has one reason to change!              Responsability               ...
Single Responsibility Principle                             Change in responsability              Responsability          ...
Single Responsibility Principle         ScoreBoard has one reason to change!            Ball has TWO reasons to change!   ...
Single Responsibility Principle   • Avoid responsibility coupling by giving a     class/module a single reason to change  ...
Single Responsibility Principle     public class EmployeeBoard {       public void showListOfEmployeeNames(List<Employee> ...
Single Responsibility Principle     public class EmployeeBoard {       public void showListOfEmployeeNames(List<Employee> ...
Single Responsibility Principle   • Read the article:          – http://www.objectmentor.com/resources/articles/          ...
OO Design Principles   •    Single Responsibility Principle (SRP)   •    Open Closed Principle (OCP)   •    Liskov Substit...
Open Close Principle   • Classes should be closed for modification but     open for extension          public class       ...
Open Close Principle   • Classes should be closed for modification but     open for extension          public class Calcul...
Open Close Principle                                                                                       Extreme        ...
Open Close Principle                                                                           Hey! I want to             ...
Hey! We                                                                                                 increased the   Op...
Open Close Principle                                                   “How can we create designs that                    ...
Open Close Principle   • An object model that follows this     principle, show two primary attributes:          – Are “Ope...
OO Design Principles   •    Single Responsibility Principle (SRP)   •    Open Closed Principle (OCP)   •    Liskov Substit...
Liskov Substitution Principle           What is wanted here is something like the following       substitution property: I...
Liskov Substitution Principle         FUNCTIONS THAT USE POINTERS OR REFERENCES TO BASE           CLASSES MUST BE ABLE TO ...
Liskov Substitution Principle   • Design rule to create hierarchies of classes     that conform to OCP   • Client code:   ...
Liskov Substitution Principle   • Subtypes must be substitutable for their base     types.                                ...
Liskov Substitution Principle    Consider this class:    public class Board {      public Piece getPieceFromPosition(int x...
Liskov Substitution Principle     Running….     public void main(String args[]) {       Board board = new Board();       G...
Liskov Substitution Principle   • Read the article:          – http://www.objectmentor.com/resources/articles            /...
OO Design Principles   •    Single Responsibility Principle (SRP)   •    Open Closed Principle (OCP)   •    Liskov Substit...
Interface Segregation Principle         Clients should not be forced to depend            on methods that they do not use....
Interface Segregation Principle   • ‘Fat’ and polluted interfaces should be splitted     into smaller and specific interfa...
Interface Segregation Principle                                                                         Phone interface is...
Interface Segregation Principle                                                            Phone interface is start to bei...
Interface Segregation Principle   • SimplePhone does not need     takePhoto, zoomIn or     connectToInternet methods, but ...
Interface Segregation Principle   • Every time you change     Phone     interface, you’ll, general     ly, have to change ...
Interface Segregation Principlepublic      interface Phone {  void      ring();  void      answerCall();  void      reject...
Interface Segregation Principle  Possible solution applying the ISPOO Design Principles is licensed under a Creative Commo...
OO Design Principles   •    Single Responsibility Principle (SRP)   •    Open Closed Principle (OCP)   •    Liskov Substit...
Dependency Inversion Principle       High Level Modules should not depend upon        low level modules. Both should depen...
Dependency Inversion Principle   • Higher level modules are best candidates for reuse,     so they should not be coupled w...
Dependency Inversion Principle   • Transitive dependencies should be avoided   • If GSMModem changes, most likely Vehicle ...
Dependency Inversion Principle   • Applying DIP                                                                           ...
Dependency Inversion Principle                              “All well structured object-oriented                          ...
Dependency Inversion Principle                                     “Each layer builds upon equally                        ...
Dependency Inversion Principle   • Going deeper with DIP                                                                  ...
OO Design Principles   •    Single Responsibility Principle (SRP)   •    Open Closed Principle (OCP)   •    Liskov Substit...
Don’t Repeat Yourself principle   • Avoid duplicate code by abstracting out things     that are common and placing those t...
Don’t Repeat Yourself principle                         public void businessMethod(String buinsesCommand) {               ...
Don’t Repeat Yourself principle   • Have each piece of information and behavior     in a single small place (SRP)   • Chan...
Upcoming SlideShare
Loading in …5
×

OO design principles

1,982 views

Published on

Introduction to the SOLID OO Design Principles.
Single Responsability Principle
Open Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle

Visit http://jpereira.eu/2012/04/16/oo-design-principles-part-2/
If you want to download the presentation, please contact me (find my contact in my blog)

Published in: Education, Technology, Business

OO design principles

  1. 1. OO Design Principles João Miguel Pereira http://jpereira.eu March 2012OO Design Principles by João Miguel Pereira is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  2. 2. OO Design Principles • Single Responsibility Principle (SRP) • Open Closed Principle (OCP) • Liskov Substitution Principle (LSP) • Interface Segregation Principle (ISP) • Dependency Inversion Principle (DIP) • Don’t Repeat Yourself (DRY)OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 2ShareAlike 3.0 Unported License.
  3. 3. OO Design Principles • Single Responsibility Principle (SRP) • Open Closed Principle (OCP) • Liskov Substitution Principle (LSP) • Interface Segregation Principle (ISP) • Dependency Inversion Principle (DIP) • Don’t Repeat Yourself (DRY)OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 3ShareAlike 3.0 Unported License.
  4. 4. Single Responsibility Principle • Tom DeMarco and Meilir Page-Jones call it Cohesion – The Practical Guide to Structured Systems Design, 2d. ed., Meilir PageJones, Yourdon Press Computing Series, 1988 http://amzn.to/book_TPGSS – Structured Analysis and System Specification, Tom DeMarco, Yourdon Press Computing Series, 1979  http://amzn.to/book_SASSOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 4ShareAlike 3.0 Unported License.
  5. 5. Single Responsibility Principle There shouldn’t never be more than one reason for a class to change! Robert C. Martin http://www.objectmentor.com/resources/articles/srp.pdfOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 5ShareAlike 3.0 Unported License.
  6. 6. Single Responsibility Principle Responsability Class implementing Move ball Ball Kick Ball Player Score Ball,ScoreBoardOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 6ShareAlike 3.0 Unported License.
  7. 7. Single Responsibility Principle Change in responsability Responsability Class implementing Move Ball Move ball Ball Kick Ball Player Score Ball,ScoreBoardOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 7ShareAlike 3.0 Unported License.
  8. 8. Single Responsibility Principle Ball has one reason to change! Responsability Class implementing Move Ball Move ball Ball Ball Kick Ball Player Score Ball,ScoreBoardOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 8ShareAlike 3.0 Unported License.
  9. 9. Single Responsibility Principle Change in responsability Responsability Class implementing Move ball Ball Kick Ball Kick Ball Player Score Ball,ScoreBoardOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 9ShareAlike 3.0 Unported License.
  10. 10. Single Responsibility Principle Player has one reason to change! Responsability Class implementing Move ball Ball Kick Ball Kick Ball Player Player Score Ball,ScoreBoardOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 10ShareAlike 3.0 Unported License.
  11. 11. Single Responsibility Principle Change in responsability Responsability Class implementing Move ball Ball Kick Ball Player Score Score Ball,ScoreBoardOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 11ShareAlike 3.0 Unported License.
  12. 12. Single Responsibility Principle ScoreBoard has one reason to change! Ball has TWO reasons to change! Responsability Class implementing Move Ball Move ball Ball Ball Kick Ball Player Score Score Ball,ScoreBoard Ball, ScoreboardOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 12ShareAlike 3.0 Unported License.
  13. 13. Single Responsibility Principle • Avoid responsibility coupling by giving a class/module a single reason to change • Every line of code should only focus on carrying out that single responsibilityOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 13ShareAlike 3.0 Unported License.
  14. 14. Single Responsibility Principle public class EmployeeBoard { public void showListOfEmployeeNames(List<Employee> employees) { for (Employee employee : employees) { if (employee.getGender.equals("M")) { printMessage("Mr. "+employee.getName()); } else { printMessage("Miss. "+employee.getName()); } } } } } This class shows the names of employees in a screen… but also format each employees name!!OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 14ShareAlike 3.0 Unported License.
  15. 15. Single Responsibility Principle public class EmployeeBoard { public void showListOfEmployeeNames(List<Employee> employees) { for (Employee employee : employees) { printMessage(employee.getFormattedName()); } } } public class Employee { public String getFormattedName() { String formattedName; if (this.getGender.equals("M")) { formattedName = "Mr. " + this.getName(); } else { formattedName = "Miss. " + this.getName(); } return formattedName; } }OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 15ShareAlike 3.0 Unported License.
  16. 16. Single Responsibility Principle • Read the article: – http://www.objectmentor.com/resources/articles/ srp.pdfOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 16ShareAlike 3.0 Unported License.
  17. 17. OO Design Principles • Single Responsibility Principle (SRP) • Open Closed Principle (OCP) • Liskov Substitution Principle (LSP) • Interface Segregation Principle (ISP) • Dependency Inversion Principle (DIP) • Don’t Repeat Yourself (DRY)OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 17ShareAlike 3.0 Unported License.
  18. 18. Open Close Principle • Classes should be closed for modification but open for extension public class Calculator { Well tested, robust public int sum(int a, int b) { and correctly return a + b; implemented class. } Why modify it? public int multiply(int a, int b) { return a * b; } } How to extend Calculator class to add the square root function ?OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 18ShareAlike 3.0 Unported License.
  19. 19. Open Close Principle • Classes should be closed for modification but open for extension public class Calculator { public int sum(int a, int b) { return a + b; } public int multiply(int a, int b) { return a * b; } } //AdvancedCalulator extends a well tested class public class AdvancedCalculator extends Calculator { public double sqrt(int a) { return Math.sqrt(Float.valueOf(a)); } }OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 19ShareAlike 3.0 Unported License.
  20. 20. Open Close Principle Extreme public class SpaceShip { private String crew1Name= "Matt"; example! private String crew2Name= "Daemon"; private String crew3Name= "Willis"; public String getCrew1Name() { return this.crew1Name; } public String getCrew2Name() { return crew2Name; } public void setCrew2Name(String crew2Name) { this.crew2Name = crew2Name; } public String getCrew3Name() { return crew3Name; } public void setCrew3Name(String crew3Name) { this.crew3Name = crew3Name; } }OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 20ShareAlike 3.0 Unported License.
  21. 21. Open Close Principle Hey! I want to replace Daemon for Bob. Can you public class SpaceShip { private String crew1Name= "Matt"; Extreme do it? private String crew2Name= "DaemonBob"; example! private String crew3Name= "Willis"; public String getCrew1Name() { return this.crew1Name; } public String getCrew2Name() { Sure! I just return crew2Name; } replace the string ;) public void setCrew2Name(String crew2Name) { this.crew2Name = crew2Name; } public String getCrew3Name() { return crew3Name; } public void setCrew3Name(String crew3Name) { this.crew3Name = crew3Name; } }OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 21ShareAlike 3.0 Unported License.
  22. 22. Hey! We increased the Open Close Principle capacity of the spaceship and public class SpaceShip { now we want to private String crew1Name= "Matt"; Extreme add 2 new guys private String crew2Name= "Bob"; example! to the crew. private String crew3Name= "Willis"; public String getCrew1Name() { return this.crew1Name; } public String getCrew2Name() { return crew2Name; Well… that } will cost you public void setCrew2Name(String crew2Name) { a lot! this.crew2Name = crew2Name; } public String getCrew3Name() { return crew3Name; } public void setCrew3Name(String crew3Name) { this.crew3Name = crew3Name; } }OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 22ShareAlike 3.0 Unported License.
  23. 23. Open Close Principle “How can we create designs that are stable in the face of change and that will last longer than the first version?”Robert C. Martin SOFTWARE ENTITIES (CLASSES, MODULES, FUNCTIO NS, ETC.) SHOULD BE OPEN FOR EXTENSION, BUT CLOSED FOR MODIFICATION.OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- Bertrand Meyer 23ShareAlike 3.0 Unported License.
  24. 24. Open Close Principle • An object model that follows this principle, show two primary attributes: – Are “Open For Extension” An object can be extended and behave in new and different ways. – Are “Closed for Modification”  The source code of such an object is inviolate. No one is allowed to make source code changes to it. Robert C. Martin - http://objectmentor.com/resources/articles/ocp.pdfOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 24ShareAlike 3.0 Unported License.
  25. 25. OO Design Principles • Single Responsibility Principle (SRP) • Open Closed Principle (OCP) • Liskov Substitution Principle (LSP) • Interface Segregation Principle (ISP) • Dependency Inversion Principle (DIP) • Don’t Repeat Yourself (DRY)OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 25ShareAlike 3.0 Unported License.
  26. 26. Liskov Substitution Principle What is wanted here is something like the following substitution property: 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." - BarbaraLiskov, Data Abstraction and Hierarchy, SIGPLAN Notices, 23,5 (May, 1988)OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 26ShareAlike 3.0 Unported License.
  27. 27. Liskov Substitution Principle FUNCTIONS THAT USE POINTERS OR REFERENCES TO BASE CLASSES MUST BE ABLE TO USE OBJECTS OF DERIVED CLASSES WITHOUT KNOWING IT. – Robert C. Martin http://www.objectmentor.com/resources/articles/lsp.pdfOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 27ShareAlike 3.0 Unported License.
  28. 28. Liskov Substitution Principle • Design rule to create hierarchies of classes that conform to OCP • Client code: Piece piece = null; if (this.board instanceof Board ) { piece = board.getPieceFromPosition(x, y); } else if (this.board instanceof ThreeDimensionalBoard) { piece = ((ThreeDimensionalBoard)board).getPieceFromPosition(x, y, z); } Violation of LSPOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 28ShareAlike 3.0 Unported License.
  29. 29. Liskov Substitution Principle • Subtypes must be substitutable for their base types. OK OKOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 29ShareAlike 3.0 Unported License.
  30. 30. Liskov Substitution Principle Consider this class: public class Board { public Piece getPieceFromPosition(int x, int y) {} public void addPiecetoPosition(int x, int y, Piece piece) {} } And its use: public class Game { public void setBoard(Board board) { this.board = board; } public void play(int x, int y, int newX, int newY) { Piece piece = board.getPieceFromPosition(x, y); board.addPieceToPosition(newX, newY, piece); } } Now, extend Board to have a 3D board: public class ThreeDimensionBoard extends Board{ public Piece getPieceFromPosition(int x, int y, int z) {} public void addPiecetoPosition(int x, int y, int z, Piece piece) {} }OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 30ShareAlike 3.0 Unported License.
  31. 31. Liskov Substitution Principle Running…. public void main(String args[]) { Board board = new Board(); Game game = new Game(); game.setBoard(board); game.play(0, 0, 0, 2); //Ok Board threeDimensionalBoard = new ThreeDimensionBoard(); game.setBoard(threeDimensionalBoard); game.play(0, 0, 0, 2); ////And the z and newZ } Compiling, running but not working! Discussions http://c2.com/cgi/wiki?LiskovSubstitutionPrincipleOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 31ShareAlike 3.0 Unported License.
  32. 32. Liskov Substitution Principle • Read the article: – http://www.objectmentor.com/resources/articles /lsp.pdfOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 32ShareAlike 3.0 Unported License.
  33. 33. OO Design Principles • Single Responsibility Principle (SRP) • Open Closed Principle (OCP) • Liskov Substitution Principle (LSP) • Interface Segregation Principle (ISP) • Dependency Inversion Principle (DIP) • Don’t Repeat Yourself (DRY)OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 33ShareAlike 3.0 Unported License.
  34. 34. Interface Segregation Principle Clients should not be forced to depend on methods that they do not use. Robert. C. Martin - http://www.objectmentor.com/resources/articles/isp.pdf Interface does not necessarily means Java InterfaceOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 34ShareAlike 3.0 Unported License.
  35. 35. Interface Segregation Principle • ‘Fat’ and polluted interfaces should be splitted into smaller and specific interfaces, achieving high cohesive interfaces • Clients should only depend on methods they use, thus reducing couplingOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 35ShareAlike 3.0 Unported License.
  36. 36. Interface Segregation Principle Phone interface is High Cohesive OK SimplePhone needs all methods from Phone InterfaceOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 36ShareAlike 3.0 Unported License.
  37. 37. Interface Segregation Principle Phone interface is start to being polluted with takePhoto method PhoneWithCamera is Ok with the Phone interface. SimplePhone does not cares about taking photo, but the interface is polluted and have to implement it!OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 37ShareAlike 3.0 Unported License.
  38. 38. Interface Segregation Principle • SimplePhone does not need takePhoto, zoomIn or connectToInternet methods, but has to implement them. • PhoneWithCamera does not know how to connect to internet, but has to implement the method connectToInternet • SmartPhone is ok with the interfaceOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 38ShareAlike 3.0 Unported License.
  39. 39. Interface Segregation Principle • Every time you change Phone interface, you’ll, general ly, have to change the clients, even when they don’t care about the reason of the change. • Clients are forced to implement methods they don’t needOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 39ShareAlike 3.0 Unported License.
  40. 40. Interface Segregation Principlepublic interface Phone { void ring(); void answerCall(); void rejectCall(); void terminateCall(); void takePhoto();}class SimplePhone implements Phone { @Override public void takePhoto() { throw new UnsupportedOperationException("Sorry. I dont know how to do that"); }} Sign that you should rethink about the design! OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 40 ShareAlike 3.0 Unported License.
  41. 41. Interface Segregation Principle Possible solution applying the ISPOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 41ShareAlike 3.0 Unported License.
  42. 42. OO Design Principles • Single Responsibility Principle (SRP) • Open Closed Principle (OCP) • Liskov Substitution Principle (LSP) • Interface Segregation Principle (ISP) • Dependency Inversion Principle (DIP) • Don’t Repeat Yourself (DRY)OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 42ShareAlike 3.0 Unported License.
  43. 43. Dependency Inversion Principle High Level Modules should not depend upon low level modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions. Robert C. Martin - http://www.objectmentor.com/resources/articles/dip.pdfOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 43ShareAlike 3.0 Unported License.
  44. 44. Dependency Inversion Principle • Higher level modules are best candidates for reuse, so they should not be coupled with details. Higher Level Class Lower Level Class providing details for communication • How to reuse the class vehicle in a new application that uses WIFI for communication?OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 44ShareAlike 3.0 Unported License.
  45. 45. Dependency Inversion Principle • Transitive dependencies should be avoided • If GSMModem changes, most likely Vehicle should be recompiled also, because there is a transitive dependency between Vehicle and GSMModemOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 45ShareAlike 3.0 Unported License.
  46. 46. Dependency Inversion Principle • Applying DIP Higher Level Class does not depends on details, but rather on an abstractionOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 46ShareAlike 3.0 Unported License.
  47. 47. Dependency Inversion Principle “All well structured object-oriented architectures have clearly-defined layers, with each layer providing some coherent set of services through a well defined and controlled interface” Layer A Layer B Layer C Grady BoochOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 47ShareAlike 3.0 Unported License.
  48. 48. Dependency Inversion Principle “Each layer builds upon equally well-defined and controlled facilities at lower levels of abstraction” AbstractLayer A AbstractLayer B AbstrctLayer C Grady BoochOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 48ShareAlike 3.0 Unported License.
  49. 49. Dependency Inversion Principle • Going deeper with DIP Abstract Classes only depend on abstract classes, i.e. higher layers are build upon abstractions of lower layersOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 49ShareAlike 3.0 Unported License.
  50. 50. OO Design Principles • Single Responsibility Principle (SRP) • Open Closed Principle (OCP) • Liskov Substitution Principle (LSP) • Interface Segregation Principle (ISP) • Dependency Inversion Principle (DIP) • Don’t Repeat Yourself (DRY)OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 50ShareAlike 3.0 Unported License.
  51. 51. Don’t Repeat Yourself principle • Avoid duplicate code by abstracting out things that are common and placing those things in a single location public void businessMethod(String buinsesCommand) { if (Logger.isEnabled() && Configuration.getLevel() >= LogLevel.DEBUG.value()) { Logger.info("Entering business method"); } //Business code } public void otherBusinessMethod(String buinsesCommand) { if (Logger.isEnabled() && Configuration.getLevel() >= LogLevel.DEBUG.value()) { Logger.info("Entering business method"); } //Business Code }OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 51ShareAlike 3.0 Unported License.
  52. 52. Don’t Repeat Yourself principle public void businessMethod(String buinsesCommand) { Logger.attemptDebug("Entering business method"); Abstracting out //Business code } public void otherBusinessMethod(String buinsesCommand) { Logger.attemptDebug("Entering business method"); //Business Code } public class Logger { public static void attemptDebug(String message) { if (Logger.isEnabled() && Configuration.getLevel() >= LogLevel.DEBUG.value()) { Logger.info(message); } } }OO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 52ShareAlike 3.0 Unported License.
  53. 53. Don’t Repeat Yourself principle • Have each piece of information and behavior in a single small place (SRP) • Changes to your system only affect small portions of your code • Potential bugs arising from changes are kept to a minimum, in isolated places • Cost and risk of changes are reducedOO Design Principles is licensed under a Creative Commons Attribution-NonCommercial- 53ShareAlike 3.0 Unported License.

×