SOLID Principles Design Patterns and Principles Part 3 Flashcards
is a mnemonic for five design principles intended to make
software designs more understandable, flexible and maintainable.
SOLID
introduced them in the book Agile Software Development, Principles, Patterns, and Practices
Robert Martin
● A class should have just one reason to change.
● The main goal of this principle is reducing complexity
Single Responsibility Principle
● Classes should be open for extension but closed for
modification.
● The main idea of this principle is to keep existing code
from breaking when implementing new features.
Open/Closed Principle
A class is __ if you can extend it, produce a subclass
and do whatever you want with it add new methods
or fields, override base behavior, etc.
open
A class is __ (or complete ) if it’s 100% ready to be
used by other classes its interface is clearly defined
and won’t be changed in the future.
closed
● 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.
● This means that the subclass should remain compatible with the
behavior of the superclass.
Liskov Substitution Principle
Liskov
Substitution Principle is named by ___ , who defined it in 1987 in
her work Data abstraction and hierarchy
Barbara Liskov
The substitution principle has a set of formal
requirements for subclasses , and specifically for
their methods, which are as follows:
- Parameter types in a method of a
subclass should match or be more abstract
than parameter types in the method of the
superclass. - 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. - A method in a subclass
shouldn’t throw types of
exceptions which the base method isn’t expected to
throw. - A subclass shouldn’t strengthen pre
conditions. - A subclass shouldn’t weaken post
conditions. - Invariants of a superclass must be preserved.
- A subclass shouldn’t change values of private fields of
the superclass
● Clients shouldn’t be forced to depend on
methods they do not use.
● Try to make your interfaces narrow enough
that client classes don’t have to implement
behaviors they don’t need.
● Clients should implement only those methods
that they really need. Otherwise, a change to
a “fat” interface would break even clients
that don’t use the changed methods.
Interface Segregation Principle
●
High level classes shouldn’t depend on low level classes. Both
should depend on abstractions.
●
Abstractions shouldn’t depend on details. Details should
depend on abstractions.
Dependency Inversion Principle
Usually when designing software, you can make a distinction
between two levels of classes.
Low level classes
High level classes
implement basic operations
such as working with a disk, transferring data over
a network, connecting to a database, etc.
Low level classes
contain complex business logic
that directs low level classes to do something.
High level classes
The dependency inversion principle suggests changing the direction of this
dependency by:
■ Describe interfaces for low level operations that high level classes rely on,
preferably in business terms.
■ Make high level classes dependent on those interfaces, instead of on
concrete low level classes.
■ Once low level classes implement these interfaces, they become
dependent on the business logic level