Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the dependency inversion principle of java design principles?

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what is the principle of dependency inversion of java design principles". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Brief introduction

The principle of dependency inversion is an important principle of system decoupling, and following it can make our system more robust.

Define

The principle of dependency inversion (Dependency Inversion Principle) was proposed by Robert C. Martin, which declares two aspects:

The upper module should not rely on the lower module, and both sides should rely on abstraction.

Abstraction should not depend on implementation, implementation should rely on abstraction.

There are several concepts in a declaration that relies on the principle of inversion: upper, lower, abstract, and implementation.

The upper layer and the lower layer are a kind of concepts. In computer design, layering is a common method of task decomposition. Each layer uses the functions provided by the lower layer and provides its own functions for the upper layer. The principle of dependency inversion requires that when designing inter-layer communication and interaction standards, we should not rely on a lower layer, but on abstraction, so that there is no strong coupling between the upper and lower layers. If both implementations follow the same abstraction, the lower layer implementation can be replaced without awareness in the upper layer.

Abstraction and implementation are a kind of concepts, abstraction is the expression of the essential attributes of the same kind of task, and implementation is the expression of the details of each kind of task. The principle of dependency inversion states that the implementation should depend on the abstraction because the implementation is the filling of the abstract skeleton, while the abstraction should not depend on the implementation, because the abstraction is the induction of the essence and the interference of details should be removed.

After looking at the above rules, you also need to ask what is the inversion of the principle of dependency inversion?

Literally, the inversion of dependence is of course the inversion of dependence, but the core is the inversion of control. Let's explain it from the following example.

Practice

The demand requires the implementation of a sorting system, the system needs to implement a variety of sorting algorithms, users can call different sorting algorithms to sort their own data.

The designed interfaces are as follows:

Public interface Sort {public void sort (int [] nums);}

There are different sorting algorithms implemented:

Public class QuickSort {public void sort (int [] nums) {/ / quick sort implementation...}} public class MergeSort {public void sort (int [] nums) {/ / merge sort implementation...}} public class BubbleSort {public void sort (int [] nums) {/ / bubble sort implementation...}}.

The user uses:

Public class Client {public static void main (String [] args) {Sort sort = new QuickSort (); int [] nums = new int [10]; / / initial nums sort.sort (nums);}}

You can see that the user relies on the abstract Sort interface when using it, but the interface cannot be instantiated, so the first sentence Sort sort = new QuickSort (); assigns the implementation to the variable after instantiation. Here, the user as the upper-level module relies on the lower-level implementation, which violates the dependency inversion principle.

To solve this problem, the instantiation process needs to be migrated to the sorting system, and the user selects the algorithm he wants to use through configuration, parameters and so on, so that the user does not rely on the specific implementation of the sorting system, but only depends on the interface abstraction of Sort.

We implement a simple factory in which the sorting algorithm is returned by the intelligent judgment of the sorting system.

Public final class SortFactory {private static final int SIZE_THRESHOLD = 300; public static Sort choose (int sortSize) {if (sortSize < SIZE_THRESHOLD) {return new BubbleSort ();} return new QuickSort ();}}

When using it, the user can directly use the form of Sort sort = SortFactory.choose (size) to obtain the sort instance.

From this example, we can see that the instantiation control is originally in the hands of the user, but this couples the abstraction with the implementation, and later we use the simple factory pattern to hand over the control back to the sorting system. The user only needs to call the factory method to get the instance without paying attention to the specific implementation.

This is the end of the content of "what is the principle of dependency inversion of java design principles". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report