In the last article we talked about the principles of the “design approach”. This article talks about the principles of the “design goal” : the open-close principle, the Richter substitution Principle, and The Demeter principle.

Open-closed Principle (OCP)

The open closed principle simply means open for extension, closed for modification. It is formally defined as follows:

Software entities like classes,modules and functions should be open for extension but closed for modifications.

Software entities such as classes, modules, and functions should be open for extension, but closed for modification

Following the open closed principle, we designed our classes, modules, and functions (methods) to extend new functionality without changing the original code.

Benefits of following the open closed principle:
  • Improve system stability, extend new functions, do not modify the original code, will not affect the system’s existing functions.
  • Improve system scalability, because the purpose of the open closed principle is to expand development
How to implement the open closed principle
  1. Programming toward abstractions (interfaces) can be done by implementing new interfaces, even if the functionality changes. This allows you to extend new features without changing the code.

  2. Encapsulate the same changes into one interface or abstract class, encapsulate different changes into different interfaces or abstract classes, and there should be no two different changes in the same interface or abstract class.

Liskov Substitution Principle (LSP)

If for every object O1 of type S, there is an object O2 of type T, such that the behavior of all programs P defined in terms of T does not change when all objects O1 is substituted for O2, then S is a subtype of T

Put another way: all references to the base class (parent) must transparently use the objects of its subclass. More directly, a subclass cannot override a nonabstract method of its parent class. (If a subclass overrides a superclass method, the program’s behavior will definitely change if the subclass replaces the superclass method.)

The meaning of following the Richter substitution principle

As we repeatedly mentioned earlier in abstract oriented programming, in order to satisfy the open closed principle, when coding, use the parent (base) class as much as possible. When the behavior changes, you simply implement different subclasses, which are determined at run time. If the parent class overrides the methods of the child class, then the methods of the base class will be called with incorrect results. Therefore, The Richter substitution principle is one of the important ways to realize the open-closed principle

Law Of Demeter (LoD)

Demeter’s law is also known as the Least Knowledge Principle (LKP).

Talk only to your immediate friends and not to strangers.

The purpose of the Demeter principle is to reduce coupling between classes, reduce dependencies between classes, and provide module independence (high aggregation, low coupling).

Demeter’s law requires interaction with a direct friend, and the direct friend condition is:

  1. Current object itself (this)
  2. An object passed in as a parameter to the current object method
  3. An object directly referenced by the instance variable of the current object
  4. If the instance variable of the current object is an aggregate, then the elements in the aggregate are also friends
  5. The object created by the current object

summary

This article mainly introduces the principles related to design goals: open closed principle, Richter substitution principle, Demeter principle. It’s more about the concept. The design patterns learned later can fully embody these principles.