🌱 SOLID principals
code that is hard to change is bad design
1. Single responsibility
2. Open-closed
software is open for extension; original source closed for modification
Separate extensible behavior behind an interface and flip the dependencies
Square, Circle, Triangle -> Shapes AreaCalculator class
polymorphic : different behavior behind a common interface
3. Liskov Substitution
B
should be able to be substituted anywhere A
is used in the codebase.
class A {
public function fire() {}
}
class B extends A {
public function fire() {}
}
return values should be of the same type for methods in an interface
if you have to do type checking you are probably breaking a SOLID principal
- signature must match
- preconditions can’t be greater
- post conditions at least equal to
- exception types must match
4. Interface segregation
Clients should never be forced to implement interfaces they don’t use.
Prevent ripple effects.
5. Dependency Inversion
High-level modules should depend on abstractions not lower level modules. Both should depend on abstraction(contract or interface).
Decouple code.
High level code : not concerned with details
Low level code : concerned with details
Class should depend on an interface not an implementation