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

How to realize the dependency inversion principle of java Design pattern

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to realize the dependency inversion principle of java design pattern". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the dependency inversion principle of java design pattern".

Dependency inversion principle (Dependence Inversion Principle), referred to as DIP

Define

High level modules should depend upon low level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.

That is,

1. High-level modules should not rely on lower-level modules, both should rely on abstractions (abstract classes or interfaces)

2. Abstractions (abstract classes or interfaces) should not depend on details (concrete implementation classes)

3. Details (concrete implementation classes) should rely on abstraction

Abstract: that is, abstract classes or interfaces, both of which cannot be instantiated.

Details: that is, concrete implementation classes, implementation interfaces, or classes generated by inheriting abstract classes, both of which can be instantiated directly through the keyword new.

The essence of relying on the inversion principle is to make the implementation of each class or module independent of each other and not influence each other through abstraction (abstract class or interface) to realize the loose coupling between modules. But this principle is also the most difficult to implement of the six design principles, and if it is not implemented, it means that the open-close principle (for extended development and off for modification) cannot be implemented.

Dependency inversion can be achieved in three ways

1. Pass dependent objects through constructors

For example, the parameters that need to be passed in the constructor are implemented in an abstract class or interface.

2. Passing dependent objects through setter method

That is, the parameters in the setXXX method we set are abstract classes or interfaces to pass dependent objects.

3. Interface declaration implements dependent objects

For example, the following example

Tu Tu is a female monk.

Public class Tutu {/ / Tu is a girl who can cook public void cook (Noodles noodles) {noodles.eat ();}}

Noodles (currently only know how to cook noodles)

Public class Noodles {/ / eat noodles public void eat () {System.out.println ("smear noodles...");}}

Tu Tu sits at home and eats noodles (scene class)

Public class Home {public static void main (String args []) {Tutu tutu = new Tutu (); Noodles food = new Noodles (); tutu.cook (food);}}

Running result: smear and eat noodles.

But there is a problem, painting can only make noodles, it is impossible to eat noodles every time, eat noodles every day to kill you, so in the above Tutu class in the cook method, if the coating will do other food, that would be better. So she took a step towards a housewife, using the principle of dependency inversion.

That is to say, Tu Tu can also braise rice, fire squid (although it sounds uncomfortable, but delicious), Sautéed Shredded Pork in Sweet Bean Sauce and so on. To implement it in your code, you need to implement two interfaces: ITutu and IFood

Public interface ITutu {/ / so you can cook a lot of food public void cook (IFood food);}

Implementation class

Public class Tutu implements ITutu {@ Override public void cook (IFood food) {food.eat ();}}

Food interface

Public interface IFood {public void eat ();}

This leaves a lot of room for extension, and aspects extend other classes. There will be no change in the details. In the future, you can make it yourself by learning what you want to eat.

Realize noodles

Public class Noodles implements IFood {@ Override public void eat () {System.out.println ("smear noodles...");}}

Realize rice

Public class Rice implements IFood {@ Override public void eat () {System.out.println ("smear and eat rice (finally)");}}

Scene category: smear starts to eat at home, just cook what you want.

Public class Home {public static void main (String args []) {/ / API makes it impossible to instantiate ITutu tutu = new Tutu (); / / instantiate rice, IFood rice = new Rice () when coated; / / eat noodles / / IFood noodles = new Noodles (); tutu.cook (rice);}}

In this way, the implementation of each class or module is independent of each other and does not affect each other.

Thank you for your reading, the above is the content of "how to realize the principle of dependency inversion of java design pattern". After the study of this article, I believe you have a deeper understanding of how to realize the principle of dependency inversion of java design pattern. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Development

Wechat

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

12
Report