Design pattern is a kind of solution that software developers come up with by summing up their experience and lessons in long-term practice. It represents the best practice in some scenarios.

Understanding and mastering design patterns is a must for every good Android developer and is a regular part of the interview process.

This article introduces you to the six principles of software design patterns:

  • Single responsibility principle
  • Open and closed principle
  • Omega substitution principle
  • Dependency inversion principle
  • Demeter principle
  • Interface Isolation Principle

Single responsibility principle

Definition: There should be only one reason for a class to change.

Simply put, a class should not take on too many responsibilities. If there are too many responsibilities, it is easy to create responsibility coupling. For example, in daily development, does an Activity have a lot of logic? Network request, RecyclerView adapter logic, will cause Activity more bloated.

Open and closed principle

Definition: Classes, modules, functions, etc. should be extensible, but not modifiable.

In daily development work, considering the constant change of requirements, we can refer to this principle when designing functions, adding abstract function classes, and making specific operations as subclasses of function classes.

Omega substitution principle

Definition: all references to a base class (parent class) must transparently use objects of its subclasses.

Reese substitution principle is one of the important ways to realize the open and closed principle. In the application of the substitution principle design, try to design the parent class as an abstract class or interface, let the subclass inherit the parent class or implement the interface, and implement the method declared in the parent class.

Dependency inversion principle

Definition: high-level modules should not depend on low-level modules; both should depend on abstractions. Abstractions should not depend on details, details should depend on abstractions.

In Java, abstraction refers to an abstract class or interface, neither of which can be instantiated directly; Details are the implementation classes, and details are generated by implementing interfaces or inheriting abstract classes, which are objects generated by the keyword new. The high-level module is the calling side, and the low-level module is the concrete implementation class. In Java, dependencies between modules occur through abstraction, and there are no direct dependencies between implementation classes.

Demeter principle

Definition: a software entity should have as few interactions with other entities as possible.

Therefore, the development design should minimize the interaction between objects, by introducing a reasonable third party role to reduce the degree of coupling between existing objects. There are three points worth noting:

  • Try to create loosely coupled classes
  • In the structure design of the class, the access permission of its member variables and member functions should be reduced as much as possible
  • When referring to other objects, one object’s references to other objects should be minimized.

Interface Isolation Principle

Definition: A class’s dependency on another class should be based on the smallest interface.

Simply put, build a single interface, not a huge, bloated interface; Try to refine the interface and have as few methods in the interface as possible. Design interfaces that are specific to each class. Don’t try to create one big interface that all the classes that depend on it can call, but only within limits.

Resources: Android Advanced Light