Design Principle - MacKittipat/note-design-pattern GitHub Wiki

Design Principle

1. Identify the aspects of your application that vary and separate them from what stays the same.

Move the code that will be changed often out and encapsulate it so it will not affect the rest of the code

2. Program to an interface, not an implementation.

Program to super type, abstract class or interface.

Program to an implementation

Duck d = new Duck();
d.makeSound();

Program to an interface

Animal a = new Duck();
a.makeSound();

a = new Dog();
a.makeSound();

3. Favor composition over inheritance

A class contain instances of other classes that implement the desired functionality.

  • Inheritance = IS-A
  • Composition = HAS-A

4. Strive for loosely coupled designs between objects and interact.

5. Classes should be open for extension but closed for modification.

6. Depend upon abstractions. Do not depend upon concrete classes.

High level component should not depend on low level component, rather, they should both depend on abstractions.

7. Principle of Least Knowledge : Talk only to your immediate friends.

8. The Hollywood Principle : Don't call us, we will call you. Prevent dependency rot