18 SOLID Principles
18 SOLID Principles
Ref: https://stackify.com/solid-design-principles/
What is SOLID
S • Single-responsibility Principle
O • Open-closed Principle
// Profile management
void updateStudentInfo sinfo;
// Course management
void manageCourse( String operation, Course c ); operation is ADD, DROP
};
Indications of violations of “S”
class CourseGrade{
Course course;
float grade;
public:
CourseGrade( Course c );
updateGrade(float grade);
};
Real-world Examples
Using Inheritance
• Define concrete methods to “close”
• Define polymorphic methods to “open”
• Increases coupling
Using Interfaces
• Provide “open” specifications
• No straight forward to give “closed” methods
Real-World Examples
• Custom UI themes in Linux Distributions
• JDBC implementations
• Design Patterns?
L – LISKOV SUBSTITUION
PRINCIPLE
The “LSP”
The principle
Let Φ(x) be a defines
propertythat objects
provable of a superclass
about objects x ofshall
typebeT.
replaceable with objects
Then Φ(y) should be trueof
foritsobjects
subclasses without
y of type breaking
S where S is
the application.
a subtype of T..
class Rectangle{
int width, height; What are the violations
public: of the “LSP” here?
void setWidth(w){width=w};
void setHeight(h){height=h};
int area(){return width*height;}
};