Professional Documents
Culture Documents
- OO DESIGN PRINCIPLES
SOLID
- Single Responsibility Principle
- Open Closed Principle
- Liskov Substitution Principle
- Interface Segregation Principle
- Dependency Inverison Principle
Apply ’smart’
- don’t do stuff ’just because of’
- very importad to see the context of the program/code when applying SOLID
- Joel On Software advise – use with common sense!
Sida 2 2022-05-13
© 2009 Capgemini - All rights reserved
Single Responsibility Principle
Sida 3 2022-05-13
© 2009 Capgemini - All rights reserved
Single Responsibility Principle
interface Modem {
Two resposibilities public void dial(String pno);
public void hangup();
Sida 4 2022-05-13
© 2009 Capgemini - All rights reserved
Single Responsibility Principle
interface DataChannel {
public void send(char c);
public char recv();
}
interface Connection {
public void dial(String phn);
public char hangup();
}
Sida 5 2022-05-13
© 2009 Capgemini - All rights reserved
Single Responsibility Principle
public class Book {
public class Book { private String name;
private String name; private String author;
private String text;
private String author; //constructor, getters and setters
private String text; // methods that directly relate to the book properties
public String replaceWordInText(String word){
return text.replaceAll(word, text);
//constructor, getters and }
setters public boolean isWordInText(String word){
return text.contains(word);
}
}
} void printTextToConsole(){
// our code for formatting and printing the text
}
Sida 6 2022-05-13
© 2009 Capgemini - All rights reserved
Single Responsibility Principle
public class Book { public class BookPrinter {
private String name;
private String author; // methods for outputting text
private String text; void printTextToConsole(String text){
//constructor, getters and setters //our code for formatting and printing the text
// methods that directly relate to the book properties }
Sida 7 2022-05-13
© 2009 Capgemini - All rights reserved
Open Closed Principle
Sida 8 2022-05-13
© 2009 Capgemini - All rights reserved
Open/Closed Principle (OCP)
class Shape {
int m_type;
}
Sida 10 2022-05-13
© 2009 Capgemini - All rights reserved
Open Closed Principle – a Few Problems….
Sida 11 2022-05-13
© 2009 Capgemini - All rights reserved
Open Closed Principle - Improved
class Shape {
abstract void draw();
}
Sida 12 2022-05-13
© 2009 Capgemini - All rights reserved
Liskov Substitution Principle
Sida 13 2022-05-13
© 2009 Capgemini - All rights reserved
Liskov Substitution Principle (LSP)
“If you have class B inherits from class A then class A should be replaceable by class
B without any changes ”
Sida 15 2022-05-13
© 2009 Capgemini - All rights reserved
Liskov Substitution Principle
class LspTest
{
private static Rectangle getNewRectangle()
{
// it can be an object returned by some factory ...
return new Square();
}
public static void main (String args[])
{
Rectangle r = LspTest.getNewRectangle();
r.setWidth(5);
r.setHeight(10);
// user knows that r it's a rectangle. It assumes that he's able to set the width and
height as for the base class
System.out.println(r.getArea());
// now he's surprised to see that the area is 100 instead of 50.
}
}
Sida 16 2022-05-13
© 2009 Capgemini - All rights reserved
Liskov Substitution Principle
The Rectangle/Square hierarchy violates the Liskov Substitution
Principle (LSP)! Square is behaviorally not a correct substitution for
Rectangle.
Sida 17 2022-05-13
© 2009 Capgemini - All rights reserved
Liskov Substitution Principle
To get a LSP compliant solution, we make Rectangle and Square
siblings. We introduce the interface Shape to bundle common
methods.
Sida 18 2022-05-13
© 2009 Capgemini - All rights reserved
Interface Segregation Principle
Sida 19 2022-05-13
© 2009 Capgemini - All rights reserved
Interface Segregation Principle
Sida 20 2022-05-13
© 2009 Capgemini - All rights reserved
Interface Segregation Principle
Sida 21 2022-05-13
© 2009 Capgemini - All rights reserved
Interface Segregation Principle
Solution
- split into two interfaces
interface Workable {
public void work();
}
interface Feedable{
public void eat();
}
Sida 22 2022-05-13
© 2009 Capgemini - All rights reserved
Dependency Inversion Principle
Sida 23 2022-05-13
© 2009 Capgemini - All rights reserved
Dependency Inversion Principle (DIP)
+GmailObj: Gmail
+HotmailObj: Hotmail
+SendByGmail(): void
+SendByHotmail(): void
Gmail Hotmail
+Send(): void Low Level +Send(): void
Gmail Hotmail
+Send(): void +Send(): void