Design Patterns Overview

Main Purpose

Separate out the things that change from those that stay the same.

  1. Program to an Interface, not Implementation
  2. Prefer composition & delegation over Inheritance
    • Delegation is about interface sharing
    • Inheritance is about implementation sharing

But in practice, design patterns will usually not used until your code become ugly enough.

SOLID Principle

  1. Single Responsibility principle
  2. Open/Closed principle
  3. Liskov substitution principle
  4. Injection of Dependencies
    • Traditionally, Interface Segregation principle
  5. Demeter principle

For for informations and examples about Design Patterns, go to this website.

Single Responsibility Principle

A class should have one and only one reason to change.

Symptoms:

Patterns/Refactorings:

Open/Closed Principle

Classes should be open for extension, but closed for source modification.

Symptoms:

Patterns/Refactorings:

In Practice:

Can’t close against all types of changes, we have to choose.

Liskov Substitution Principle

Subtypes can substitute for base types.
A method that works on an instance of type T, should also work on any subtype of T.

Symptoms:

Patterns/Refactorings:

Injection of Dependencies (DIP)

“Inject” an abstract interface that a & b depend on.

Symptoms:

A depends on B, but B interface & implementation can change, even if functionality stable.

Patterns/Refactorings:

Demeter Principle

Only talk to your friends, not strangers.

Symptoms:

Patterns/Refactorings:

· 软件工程, 设计模式