“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Author: Tangyuan

Personal blog: Javalover.cc

preface

This article introduces seven principles of design patterns and explains them with UML class diagrams.

directory

  • Single responsibility principle
  • Interface Isolation Principle
  • Dependency inversion principle
  • Richter’s substitution principle
  • The open closed principle
  • Demeter’s rule
  • Principle of composite reuse

The body of the

1. Principle of single responsibility

Definition: A class does only one thing or a class of things

For example, a variety of vehicles, if all in one class, will lead to class bloat

  • Counterexample: There is a transportation class, which contains the movement of planes, trains and buses, resulting in confusion of concept

  • Example: Upgrade the transportation class to an interface, and then airplane, train, and bus classes to implement the transportation class, to achieve decoupling

2. Interface isolation principle

Definition: The dependency of a class on another class (interface), based on the minimum interface

For example, if class A uses only the individual methods of class B, class B can be split into multiple classes.

Break up a large interface into explicitly independent smaller interfaces by using multiple specialized interfaces.

For example, there is an animal interface, including methods: fly, swim; At this point, if the subclass is implemented directly, it will lead to interface bloat;

A better way would be to split the animal interface into two smaller interfaces, one for flying and one for swimming.

If the two small interfaces have something in common, abstract a parent interface (for example, animals eat).

  • Counterexample: The animal interface includes fly and swim, but subclasses use only one method, so the other method is redundant.

  • Example: Split the animal interface into two small interfaces, including fly and swim respectively

3. Dependency inversion principle

Definition: Make detail dependent on abstraction, not abstraction dependent on detail

In other words, program toward the interface, not toward the implementation, which is more flexible

  • Counterexample: The Cat object and Dog object are defined respectively. The Demo class depends on Cat and Dog

  • Example: Define only one Animal object. The Demo class only depends on Animal

4. Richter’s substitution principle

Definition: To be able to transparently use subclass objects where base classes are used (i.e. subclasses do not modify existing methods of the parent class)

When subclasses inherit from their parents, try not to change existing methods because that increases coupling

If you really need to change it, you can reduce the coupling between the parent class and the child class by inheriting the same class

  • Example: The Demo class here has a buy method that takes Animal as its parent, and then we can call Cat or Dog

5. Open and close principle

Definition: An entity should be open for extension (provider) and closed for modification (consumer)

  • Simple understanding, is the application layer, to increase the function, do not need to change too much; But for the implementation layer, it needs to be modified frequently (extension)
  • When the software needs to change, try to extend the class to implement, rather than modify the existing class; Because modifying an existing class affects where that class depends, extending a new class does not
  • Example: If you want to add an Animal class, you just need to create a new class and implement Animal

Demeter’s Rule

Definition: also known as the least know principle, the less you know about the classes you depend on, the better

  • Communicate only with direct friends, not with unfamiliar classes
  • Immediate friends: If a class exists in another species in one of the following three ways, the two classes are direct friends
    • Member variables
    • Method parameters
    • Method return value
  • Indirect friends: that is,New class
    • A local variable in a method of a class belongs to an unfamiliar class
  • The core is only intended to reduce coupling between classes, not eliminate coupling altogether
  • Counterexample: We usually buy fruit, if we go to the orchard to buy, it will be more trouble; For example, to buy apples to the apple orchard, to buy bananas to the banana garden;

  • Example: Then there needs to be a store, integrating the fruit; Then we can just go to the store and buy it. This way, we only need to rely on the store, not on individual fruit orchards

7. Principle of composite reuse

Definition: Use composition/aggregation rather than inheritance whenever possible

  1. Dependency: Communicating with a direct friend means that they have a Dependency and an unknown friend is also a Dependency. But inheritance that doesn’t satisfy Demeter’s rule is also dependency
  2. Aggregation: The whole and the parts can be separated, such as computer classes, aggregated mouse and keyboard, which are introduced via member variables and assigned via setters
  3. Composite: the relationship between the whole and the parts: inseparable, such as the human class, which combines the header member variables and assigns values when defined
  • Positive example, I will not list here, the above six principles are basically beneficial to use this principle. It’s more reuse, less inheritance