DEV Community

noah.k
noah.k

Posted on • Updated on

Design Principles for OOP Brief Summary

If you are developing software using object oriented programming, design principles help you a lot, by increasing flexibility and reusability.
All design patterns follow design principles below.

Liskov Substitution Principle

  • Description
    • An instance of superclass must be replaceable by an instance of subclass
    • Therefore, subclass's functionality should be general enough
  • Benefit
    • It keeps client code away from being impacted
    • Open-Closed Principle can also be satisfied

Open-Closed Principle

  • Description
    • Class should be open to extension, and closed to change
    • Class is recognized to be closed when design and implementation is completed in the following cases
      • It is tested to function properly
      • All the attributes and behaviors are encapsulated
      • Class and instance do not block the system, and they are confirmed to be stable
  • Benefit
    • It keeps stability by preventing unexpected change
    • It allows adding functionality (e.g. by inheritance)

Dependency Inversion Principle

  • Description
    • Access high level generalization
    • Do not directly access low level concrete class
  • Example
    • Create an interface which generalizes the concrete class
    • Client accesses only the interface
  • Benefit
    • It looses coupling, and realizes flexibility and reusability

Composing Object Principle (Composition over inheritance)

  • Description
    • Extend functionality by composition(aggregation) rather than by inheritance
  • Example
    • When you want to use some class's functionality, add a property of its instance, rather than inheriting it
  • Benefit
    • It looses coupling, by avoiding inheriting all the properties and methods of the superclass

Interface Segregation Principle

  • Description
    • Separate interface, and avoid enforcing to implement unwanted methods
  • Benefit
    • It avoids unnecessary dependency, and looses coupling

Principle of Least Knowledge (Law of Demeter)

  • Description
    • Method can only call other methods of the following
      • In the same object
      • Method parameter
      • Object instantiated within the method
      • Instance variable in the same object
  • Benefit
    • It looses coupling, by avoiding unnecessary interaction over classes

Reference

Coursera Design Patterns

Top comments (0)