Lecture 6 - SOLID
Lecture 6 - SOLID
Senior-lecturer
[email protected]
Astana IT University
▪ Introduction to design principles
▪ Single Responsibility Principle
▪ Open Closed Principle
▪ Liskov’s Substitution Principle
▪ Interface Segregation Principle
▪ Dependency Inversion Principle
Robert Martin (Uncle Bob)
▪ Classes are blocks of your application
▪ Badly designed block can cause a problem (headache)
▪ Implementing unneeded methods, re-reading code to understand what
method does
▪ Adding new features : No time to worry about project structure
▪ The term "SOLID" is an acronym for a set of practices for designing
software code and building a flexible and adaptive program
▪ This term was introduced about 20 years ago by the famous American
programming specialist Robert Martin, better known as "Uncle Bob"
▪ Rigidity – every change affects many other parts
▪ Fragility – things break in unrelated places
▪ Immobility – cannot reuse code outside of its original context
▪ Low cohesion and tight coupling
▪ Cohesion describes how much a function has a relation within a
single module
▪ Coupling refers to the interdependencies between modules
S
▪ In the first example a bird (Ostrich) which cannot fly will have some
problems. Since it is a bird, it would inherit fly method as well. But a
programmer did not consider birds which cannot fly
▪ The same inconvenience we confronted with was in previous task
(Shape, Rectangle, Square). On that example we had Square class
which had setLength() and setWidth() methods which were
unneeded for class Square
▪ A Square is NOT a Rectangle in terms of substitution rule
I
▪ Make fine-grained interfaces with specific methods *
▪ A client should never be forced to implement an interface that
it doesn't use, or clients shouldn't be forced to depend on
methods they do not use.
▪ A client should never depend on anything more that the
method which is used
▪ Changing a method should not affect on unrelated class
▪ Fat interface should be changed to several small and specific
ones
I
D
▪ Depend on abstractions, not on concretions *
▪ It states that the high-level module must not depend on the low-
level module, but they should depend on abstractions
▪ It should be possible to change an implementation easily
without altering high-level code
▪ The code that implements high-level policy should not depend
on the code that implements low-level details. Rather, details
should depend on policies
D