2. Outline
Overview
Bad Design
Class Design Principles
Single Responsibility Principle
Open-Closed Principle
Liskov’s Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 2/33
3. Overview
Design Principles
“Software design principles represent a set of guidelines that
helps us to avoid having a bad design.”
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 3/33
4. Overview
Design Principles
“Software design principles represent a set of guidelines that
helps us to avoid having a bad design.”
Identified by Robert C. Martin (Uncle Bob) in the early 2000s.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 3/33
5. Overview
Design Principles
“Software design principles represent a set of guidelines that
helps us to avoid having a bad design.”
Identified by Robert C. Martin (Uncle Bob) in the early 2000s.
Gathered in ”Agile Software Development: Principles,
Patterns, and Practices” (2003).
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 3/33
6. Overview
Design Principles
“Software design principles represent a set of guidelines that
helps us to avoid having a bad design.”
Identified by Robert C. Martin (Uncle Bob) in the early 2000s.
Gathered in ”Agile Software Development: Principles,
Patterns, and Practices” (2003).
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 3/33
7. Overview
Design Principles
“Software design principles represent a set of guidelines that
helps us to avoid having a bad design.”
Identified by Robert C. Martin (Uncle Bob) in the early 2000s.
Gathered in ”Agile Software Development: Principles,
Patterns, and Practices” (2003).
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 3/33
12. Bad Code
Rigidity - It is hard to change because every change affects
too many other parts of the system.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 8/33
13. Bad Code
Rigidity - It is hard to change because every change affects
too many other parts of the system.
Fragility - When you make a change, unexpected parts of the
system break.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 8/33
14. Bad Code
Rigidity - It is hard to change because every change affects
too many other parts of the system.
Fragility - When you make a change, unexpected parts of the
system break.
Immobility - It is hard to reuse in another application because
it cannot be disentangled from the current application.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 8/33
15. Class Design Principles
Single Responsibility Principle
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 9/33
16. Class Design Principles
Single Responsibility Principle
Open-Closed Principle
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 9/33
17. Class Design Principles
Single Responsibility Principle
Open-Closed Principle
Liskov’s Substitution Principle
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 9/33
18. Class Design Principles
Single Responsibility Principle
Open-Closed Principle
Liskov’s Substitution Principle
Interface Segregation Principle
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 9/33
22. Single Responsibility Principle
Figure 1: Real World
Intent:
“A class should have only one reason to change.”
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 11/33
23. Single Responsibility Principle
Another Definition:
“Every software module should have only one reason to change”
Software Module - Class, Function,...
Reason to change - Responsibility
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 12/33
25. Single Responsibility Principle
Question
A class will have single responsibility.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 13/33
26. Single Responsibility Principle
Question
A class will have single responsibility.
A function will have single responsibility.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 13/33
27. Single Responsibility Principle
Question
A class will have single responsibility.
A function will have single responsibility.
A class will have more than one function.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 13/33
28. Single Responsibility Principle
Question
A class will have single responsibility.
A function will have single responsibility.
A class will have more than one function.
→ A class will have more than one responsibility?
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 13/33
30. Single Responsibility Principle
Drawbacks?
The so called ’Class Explosion’. Our application may end
up with too many classes to manage.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 15/33
31. Single Responsibility Principle
Drawbacks?
The so called ’Class Explosion’. Our application may end
up with too many classes to manage.
There are a lot of class, so when I want to change
something, I can’t find the code I need to change.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 15/33
32. Single Responsibility Principle
The Single-Responsibility Principle is one of the simplest
principles but one of the most difficult to get right.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 16/33
33. Single Responsibility Principle
The Single-Responsibility Principle is one of the simplest
principles but one of the most difficult to get right.
Recommendation
“The first rule of functions is that they should be small. The
second rule of functions is that they should be smaller than
that.” (Clean Code, Chapter 3: Functions)
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 16/33
34. Single Responsibility Principle
The Single-Responsibility Principle is one of the simplest
principles but one of the most difficult to get right.
Recommendation
“The first rule of functions is that they should be small. The
second rule of functions is that they should be smaller than
that.” (Clean Code, Chapter 3: Functions)
“The first rule of classes is that they should be small. The
second rule of classes is that they should be smaller than
that.” (Clean Code, Chapter 10: Classes)
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 16/33
35. Single Responsibility Principle
The Single-Responsibility Principle is one of the simplest
principles but one of the most difficult to get right.
Recommendation
“The first rule of functions is that they should be small. The
second rule of functions is that they should be smaller than
that.” (Clean Code, Chapter 3: Functions)
“The first rule of classes is that they should be small. The
second rule of classes is that they should be smaller than
that.” (Clean Code, Chapter 10: Classes)
With functions we measured size by counting physical lines.
With classes we count responsibilities.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 16/33
36. Open-Closed Principle
Intent:
Software entities (classes, modules, functions, etc.) should be
open for extension but closed for modification.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 17/33
37. Open-Closed Principle
Intent:
Software entities (classes, modules, functions, etc.) should be
open for extension but closed for modification.
“Open For Extension”
The behavior of the module can be extended.
We can make the module behave in new and different ways as the
requirements of the application change, or to meet the needs of new
applications.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 17/33
38. Open-Closed Principle
Intent:
Software entities (classes, modules, functions, etc.) should be
open for extension but closed for modification.
“Open For Extension”
The behavior of the module can be extended.
We can make the module behave in new and different ways as the
requirements of the application change, or to meet the needs of new
applications.
“Closed For Modification”
The source code of such a module is inviolate. No one is allowed to
make source code changes to it.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 17/33
43. Open-Closed Principle
Abstraction is the Key
Well designed code can be extended without modification
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 20/33
44. Open-Closed Principle
Abstraction is the Key
Well designed code can be extended without modification
In well designed program, new features are added by adding
new code, rather than by change old, already working code
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 20/33
46. Open-Closed Principle
Abstraction is the Key
What are the characteristics of the best inheritance
hierarchies?
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 21/33
47. Open-Closed Principle
Abstraction is the Key
What are the characteristics of the best inheritance
hierarchies?
Liskov’s Substitution Principle
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 21/33
48. Liskov’s Substitution Principle
Barbara Liskov, “Data Abstraction and Hierarchy” (1988)
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 22/33
49. Liskov’s Substitution Principle
Barbara Liskov, “Data Abstraction and Hierarchy” (1988)
What is wanted here is something like the following substitution property:
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 22/33
50. Liskov’s Substitution Principle
Barbara Liskov, “Data Abstraction and Hierarchy” (1988)
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
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 22/33
51. Liskov’s Substitution Principle
Barbara Liskov, “Data Abstraction and Hierarchy” (1988)
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
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 22/33
52. Liskov’s Substitution Principle
Barbara Liskov, “Data Abstraction and Hierarchy” (1988)
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,
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 22/33
53. Liskov’s Substitution Principle
Barbara Liskov, “Data Abstraction and Hierarchy” (1988)
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
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 22/33
54. Liskov’s Substitution Principle
Barbara Liskov, “Data Abstraction and Hierarchy” (1988)
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.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 22/33
55. Liskov’s Substitution Principle
Robert Martin
Function that use pointers or references to base classes must be
able to use object of derived classes without knowing it.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 23/33
56. Liskov’s Substitution Principle
Robert Martin
Function that use pointers or references to base classes must be
able to use object of derived classes without knowing it.
Intent: Derived classes should be substitutable for base classes.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 23/33
57. Liskov’s Substitution Principle
Derived classes should be substitutable for base classes.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 24/33
58. Liskov’s Substitution Principle
Derived classes should be substitutable for base classes.
We can always write
BaseClass b = new DerivedClass();
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 24/33
59. Liskov’s Substitution Principle
Derived classes should be substitutable for base classes.
We can always write
BaseClass b = new DerivedClass();
Why would such a principle be made?
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 24/33
60. Liskov’s Substitution Principle
class Rectangle{
private int width;
private int height;
public setWidth(int width_in);
public setHeight(int height_in);
public int getWidth ();
public int getHeight ();
public int getArea( return width * height );
}
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 25/33
61. Liskov’s Substitution Principle
class Rectangle{
private int width;
private int height;
public setWidth(int width_in);
public setHeight(int height_in);
public int getWidth ();
public int getHeight ();
public int getArea( return width * height );
}
Clearly, a square is a rectangle for all normal intents and purposes.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 25/33
62. Liskov’s Substitution Principle
class Rectangle{
private int width;
private int height;
public setWidth(int width_in);
public setHeight(int height_in);
public int getWidth ();
public int getHeight ();
public int getArea( return width * height );
}
Clearly, a square is a rectangle for all normal intents and purposes.
class Square extends Rectangle{
// Code specific to square
}
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 25/33
63. Liskov’s Substitution Principle
class Rectangle{
private int width;
private int height;
public setWidth(int width_in);
public setHeight(int height_in);
public int getWidth ();
public int getHeight ();
public int getArea( return width * height );
}
Clearly, a square is a rectangle for all normal intents and purposes.
class Square extends Rectangle{
// Code specific to square
}
class RectangleFactory (){
public static Rectangle createRectangle (int type){
switch(type){
case 1: return new Rectangle ();
case 2: return new Square ();
}
}
}
Rectangle a = RectangelFactory :: createRectangle (2);
a.setWidth (10);
a.setHeight (5);
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 25/33
64. Liskov’s Substitution Principle
class Rectangle{
private int width;
private int height;
public setWidth(int width_in);
public setHeight(int height_in);
public int getWidth ();
public int getHeight ();
public int getArea( return width * height );
}
class Square extends Rectangle{
public setWidth(int width_in){
Rectangle :: setWidth(width_in);
Rectangle :: setHeight(width_in);
}
public setHeight(int height_in){
Rectangle :: setWidth(height_in);
Rectangle :: setHeight(height_in);
}
}
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 26/33
65. Liskov’s Substitution Principle
class Rectangle{
private int width;
private int height;
public setWidth(int width_in);
public setHeight(int height_in);
public int getWidth ();
public int getHeight ();
public int getArea( return width * height );
}
class Square extends Rectangle{
public setWidth(int width_in){
Rectangle :: setWidth(width_in);
Rectangle :: setHeight(width_in);
}
public setHeight(int height_in){
Rectangle :: setWidth(height_in);
Rectangle :: setHeight(height_in);
}
}
Rectangle a = RectangleFactory : createRectangle (2);
a.setWidth (10); // both width and height are set to 10
a.setHeight (5); //now , both width and height are 5
println(a.getArea ());
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 26/33
66. Liskov’s Substitution Principle
Violating LSP!
Changing the behavior of Width and Height properties in derived
class.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 27/33
67. Liskov’s Substitution Principle
Violating LSP!
Changing the behavior of Width and Height properties in derived
class.
Consider postcondition of Rectangle::setWidth(int width in):
assert (( width == width_in) && (height == old.height))
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 27/33
68. Liskov’s Substitution Principle
Design by Contract: Introduced by Bertrand Meyer, in Object
Oriented Software Construction, 1988.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 28/33
69. Liskov’s Substitution Principle
Design by Contract: Introduced by Bertrand Meyer, in Object
Oriented Software Construction, 1988.
Preconditions: The preconditions must be true in order for the
method to execute.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 28/33
70. Liskov’s Substitution Principle
Design by Contract: Introduced by Bertrand Meyer, in Object
Oriented Software Construction, 1988.
Preconditions: The preconditions must be true in order for the
method to execute.
Postconditions: Upon completion, the method guarantees
that the postconditions will be true.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 28/33
71. Liskov’s Substitution Principle
Design by Contract: Introduced by Bertrand Meyer, in Object
Oriented Software Construction, 1988.
Preconditions: The preconditions must be true in order for the
method to execute.
Postconditions: Upon completion, the method guarantees
that the postconditions will be true.
...when redefining a routine [in a derivative], you may only
replace its precondition by a weaker one, and its postcondition
by a stronger one.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 28/33
72. Dependency Inversion Principle
Intent:
High Level Modules should not depend upon low level
modules. Both should be depend upon abstractions.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 29/33
73. Dependency Inversion Principle
Intent:
High Level Modules should not depend upon low level
modules. Both should be depend upon abstractions.
Abstractions should not depend upon details. Details should
depend upon abstractions.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 29/33
75. Dependency Inversion Principle
Figure 5: The Copy Program
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 30/33
76. Interface Segregation Principle
Intent:
The clients should not be forced to depend upon interfaces that
they do not use.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 31/33
77. Interface Segregation Principle
Intent:
The clients should not be forced to depend upon interfaces that
they do not use.
Another definition:
Many client specific interfaces are better than one general
purpose interface.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 31/33
83. Summary
Single Responsibility Principle
Open-Closed Principle
Liskov’s Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle
A principle is only a principle. In order to avoid bad design and
make a flexible design, we should spend a lot of time and effort.
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 32/33
84. Thank you for watching!
Dinh Hoang Long, Mental Log Object Oriented Design Principles, Class Design Principles 33/33