SOLID Flashcards
What are the solid principles?
In software engineering, SOLID is a mnemonic acronym for five design principles intended to make object-oriented designs more understandable, flexible, and maintainable.
Describe the Single Responsibility Principle (SRP)
A class should have only one reason to change.
Responsibilities should be segregated to reduce coupling.
Describe the Open/Closed Principle (OCP)
Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification.
Concrete classes violate OCP. Instead use interfaces or abstract base classes. Consider the Strategy or Template design pattern. New derivatives/sub-types can be added without breaking OCP.
Describe the Liskov Substitution Principle (LSP)
An object of a superclass should be replaceable by objects of its subclasses
Defined by Barbara Liskov in 1988.
Simply put, the Liskov Substitution Principle (LSP) states that objects of a superclass should be replaceable with objects of its subclasses without breaking the application. In other words, what we want is to have the objects of our subclasses behaving the same way as the objects of our superclass.
Example violation would be referencing derived types in a base class.
Describe the Dependency-Inversion Principle (DIP)
A. High-level modules should not depend on low-level modules. Both should depend on abstractions.
B. Abstractions should not depend upon details. Details should depend upon abstractions.
Inversion of control (IOC) is a technique you can use to comply with DIP.
Describe the Interface Segregation Principle (ISP)
The interface segregation principle states that no code should be forced to depend on methods it does not use. ISP splits interfaces that are very large into smaller and more specific ones so that clients will only have to know about the methods that are of interest to them.