Detailed explanation of single responsibility and dependence inversion principle of java object oriented design principle

  • 2021-11-29 07:07:52
  • OfStack

List 1 Duty Concept Realization
Expand
Dependence on the concept of inversion principle Example
Expand

Single 1 concept of responsibility

Don't have more than one cause of class change, which means that each class should fulfill the responsibility of single 1, otherwise the class should be split. The mixed responsibilities will make the code move all over the body, which will lead to the code being mixed, difficult to understand, difficult to modify, difficult to expand and reuse. For example, the rich client program in C/S program was developed before, which mixed human-computer interaction logic, business processing logic and database operation logic into one.

Realization

The principle of single 1 responsibility is the basic principle of dividing and encapsulating classes, and abstracting classes concretely. Try to make the function of the class single 1 and clear.

1. According to functional partition, encapsulation is used to create boundary layer between objects, so that designers can modify one side of boundary layer without bad influence on the other side, thus realizing loose coupling between layers.
2. The current development framework 1 is generally a hierarchical framework. In the hierarchical framework, the responsibilities of each layer have been clearly given. For small systems with simple business, I think the classification of classes can be done according to the following steps.

Front-end page: Responsibilities mainly deal with user operation logic (including validity verification and style), and encapsulate common controls and components independently, and encapsulate components without accessing background data. Functional modules (independent pages) complete the assembly of various component controls and the extraction of data.
Back-end controller layer: Responsibility for the completion of front-end display data format conversion and submission of operational information received; Small information system 1 generally encapsulates controller in front-end independent pages (functional modules), and one controller completes all information functions of corresponding functional modules.
Back-end services layer: responsibility to complete business logic and data processing; Corresponding to the encapsulation class of controller layer, the external interface meets the requirements of controller, and it is necessary to abstract data processing and business rules, and divide related business classes in one step. The back-end services is similar to the facade mode in the design pattern, which isolates controller from various business logic and database logic, and only provides interaction with controller. A small and simple system like services can be completed only by using the back-end services class.
dao Layer: Responsibility to complete data persistence issues; There are two types of dao layer classes, one is to create an dao layer class (interface) for tables that need to be added, deleted, modified and looked up; The other one is the independent class (interface) that completes the associated query and stored procedure according to the function division
Entity layer: responsibility is the carrier function of data; Need to complete adding, deleting, changing and looking up tables to create independent entities; Create independent data entities for associated queries.

Expand

Single 1 duty principle is one of the most easily violated principles in writing code, and codes that violate this principle are immediately converted into all in one;;
Although many frameworks clearly define the responsibilities of each layer, programmers often write code to one place;
Although using object-oriented language, it is still oriented to procedure calls

Dependence on the concept of inversion principle

The principle of dependence inversion requires relying on abstraction instead of concreteness; The stability of abstraction determines the stability of the system, because abstraction is invariant, and relying on abstraction is the essence of object-oriented design.

Example

Reference: Rely on abstraction, not concreteness

Expand

Control flipping (IOC): The Richter substitution principle solves the problem that subclasses can replace parent classes, but when and by whom subclasses are created is the problem solved by relying on the inversion principle. Object, you must create a specific object, such as B class called in the A class, in the A class need to create an instance of B, then A depends on B. Now let A depend on the parent class (interface) of B, A does not create B instance, but gives the permission to create instance to container, which creates instance to A, container to that instance, A uses that instance, and A control is lost. The so-called container is a piece of code, such as factory class, Spring container and so on.

The above is the java object-oriented design principles of the single 1 responsibility and dependency inversion principle details, more information about java object-oriented design principles please pay attention to other related articles on this site!


Related articles: