Single Responsibility Principle (SRP)
Open-Closed Principle (OCP)
Liskov Substitution Principle (LSP) (*)
Interface Segregation Principle (ISP)
Dependency Inversion Principle (DIP)
Separation of Concern (SoC)
High Cohesion, Low Coupling
2. Bad use: Class 1 och class 2 have objects of each other, and methods a in class 1, and method b in class 2 refer to each other. Lays the groundwork for code reuse (flexible and modular code).
Prefer Composition over Inheritance
1. Composition: A class that references one or more objects of other classes in instance variables. This allows you to model a has-a association between objects. Inheritance: The mechanism of basing an object or class upon its super-class with a set of attributes and methods (i.e polymorphism - establishing is-a association).
Composition should be preferred since: It creates fewer dependencies, more robust code (since subclasses can stop functioning as intended if the superclass is modified), allows for more flexible code (Java doesn’t support multiple inheritance implementations), and allows for code reuse where inheritance isn’t suitable.
Law of Demeter
1. Just like your mother taught you - don’t talk to strangers! LoD requires that a method m, of an object O, may only invoke (call) the methods of: O itself, m’s parameters, any objects created/instantiated within m, O’s component objects, and global variables accessible by O, in the scope of m. That is, don’t call methods outside of the class you’re currently in!
Command-Query Separation Principle