※J.Y Chen 的個人部落格 ※

Just Follow Our Heart And We will shine!

34 瀏覽人次

Six Programming Principles You need to follow. (In the Company)

Published: in Knowledge by .

SOLID Principles

  • Single Responsibility
  • Principle Open / Closed Principle
  • Liskov Substitution Principle
  • Interface Segregation Principle
  • Dependency Inversion Principle

SRP

  • Cohesion

    • Relation of responsibilities
    • Focused on single task
  • Coupling

    • Dependency on other modules
    • Relationship between modules
  • Ideal: low coupling + strong cohesion

OCP

  • Open to extension
    • New behavior can be added later
  • Closed to modification
    • Changes to source or binary code are not required
  • Change behavior without changing the code?!
    • Yes,this is possible, e.g. by inheritance or by parameterization
    • Rely on abstractions, not on implementations
    • Do not limit the variety of implementations

LSP

  • Comes from MIT professor
  • Substitutability – child classes must not
    • Remove parent class behavior
    • Violate parent class intent
  • Instead of dad fight

ISP

  • Segregate interfaces
    • Prefer small, cohesive interfaces
    • Divide "fat" interfaces into smaller ones
  • Having "fat" interfaces leads to:
    • Classes having methods they do not need
    • Increased coupling
    • Reduced flexibility / maintainability

DIP

©2019 - 2024 Henry Chen