SOLID Principles Flashcards
SOLID - S
Single Responsibility Principle
Main idea of Single Responsibility Principle
A class should have just one reason to change.
SOLID - O
Open / Closed Principle
Main idea of Open / Closed Principle
Classes should be open for extension but closed for modification.
SOLID - L
Liskov Substitution Principle
Main idea of Liskov Substitution Principle
When extending a class, remember that you should be
able to pass objects of the subclass in place of objects of
the parent class without breaking the client code.
What means Liskov Substistution Principle
This means that the subclass should remain compatible with
the behavior of the superclass. When overriding a method,
extend the base behavior rather than replacing it with some-
thing else entirely.
What Liskov Substitution Principle states about method parameters
Parameter types in a method of a subclass should match or be
more abstract than parameter types in the method of the super-
class
What Liskov Substitution Princliple states about method return types
The return type in a method of a subclass should match or be
a subtype of the return type in the method of the superclass
What Liskov Substitution Principle states about throwning exceptions
A method in a subclass shouldn’t throw types of exceptions
which the base method isn’t expected to throw
What Liskov Substitution Principle states about pre-conditions
A subclass shouldn’t strengthen pre-conditions
What Liskov Substitution Principle states about post-conditions
A subclass shouldn’t weaken post-conditions
What Liskov Substitution Principle states about invariants
Invariants of a superclass must be preserved
What Liskov Substitution Principle states about private fields
A subclass shouldn’t change values of private fields of the
superclass
SOLID - I
Interface Segregation Principle
Main idea of Inteface Segregation Principle
Clients shouldn’t be forced to depend on methods they do not use (interface should be as narrow as possible)
SOLID - D
Dependency Inversion Principle
Main idea of Dependency Inversion Principle
High-level classes shouldn’t depend on low-level class-es. Both should depend on abstractions. Abstractions
shouldn’t depend on details. Details should depend on abstractions.
What is low-level class?
Implement basic operations such as working with a disk, transferring data over a network, connecting to a
database
What is high-level class?
Contain complex business logic that directs low-level classes to do something
What is an object invariants
Invariants are conditions in which an object makes sense. They can be defined explicitly in the form of interface contracts or a set of assertions within methods, they could also be implied by certain unit tests and expectations of the client code.
For example, invariants of a cat are having four legs, a tail, ability to meow.