- Encapsulate what varies
- Favor composition over inheritance
- Program to interface not implementations
- Strive for loosely coupled design between objects that interact
- Classes should be open for extension but closed for modification
- Strategy Pattern (simuDuck)
- Observer Pattern (weatherStation)
- Decorator Pattern (starbuzz)
- Classes should be open for extension but closed for modification
- This means we can extend classes and add new behaviours but we are not allowed to touch pre-written code, it is closed for modification.
- Using inheritance determines behaviours statically at compile time. Using composition allows dynamic behaviour switch like we see in FlyingBehaviour of ducks. Using compositions we can perform decorator pattern-esque implementations and add new responsibilities to objects which were not present in original superclass. This also avoids altering existing code to add new functionalities.