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 principle of dependency reversal in Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you what the principle of dependency reversal in Java is, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. What is the principle of dependency reversal?

High-level modules should not rely on lower-level modules, both should rely on their abstraction.

Abstraction should not rely on details, details should rely on abstractions.

The central idea of dependency inversion (inversion) is interface-oriented programming.

The principle of dependency reversal is based on the design idea that abstract things are much more stable than the variability of details. An abstraction-based architecture is much more stable than a detail-based architecture. In Java, abstraction refers to an interface or abstract class, and the details are concrete implementation classes.

The purpose of using interfaces or abstract classes is to develop specifications without involving any specific operations, leaving the task of presenting details to their implementation classes.

two。 Code case package com.szh.principle.inversion; / * * * / class Email {public String getInfo () {return "email message: hello,world";}} / / complete the function of Person to receive messages / / Mode 1 Analysis / / 1. Simple, easy to think of / / 2. If the object we get is Wechat, SMS, etc., we will add a new class and a corresponding receiving method / / 3 for Person. Solution: introduce an abstract interface IReceiver to represent the receiver, so that the Person class depends on the interface IReceiver / / because Email, WeiXin and so on belong to the receiving scope, and their respective implementation of the IReceiver interface is ok, so we rely on the inversion principle class Person {public void receive (Email email) {System.out.println (email.getInfo ()). }} public class DependencyInversion {public static void main (String [] args) {Person person = new Person (); person.receive (new Email ());}}

We can improve the above code according to the principle of dependency inversion, as follows:?

Package com.szh.principle.inversion.improve; / * * * / / define the interface interface IReceiver {public String getInfo ();} class Email implements IReceiver {public String getInfo () {return "email message: hello,world";}} / / add Wechat class WeiXin implements IReceiver {public String getInfo () {return "Wechat Information: hello,ok" }} / way 2class Person {/ / here we are dependent on the interface public void receive (IReceiver receiver) {System.out.println (receiver.getInfo ());}} public class DependencyInversion {public static void main (String [] args) {/ / client does not need to change Person person = new Person (); person.receive (new Email ()); person.receive (new WeiXin ()) }}

3. Three ways of dependency transfer and case examples 3.1 API transfer package com.szh.principle.inversion.improve; / * * mode 1: implement dependency * / interface IOpenAndClose1 {public void open (ITV1 tv) through interface delivery; / / abstract method, receive interface} interface ITV1 {/ / ITV interface public void play () } class OpenAndClose1 implements IOpenAndClose1 {@ Override public void open (ITV1 tv) {tv.play ();}} class ChangHong1 implements ITV1 {@ Override public void play () {System.out.println ("Changhong TV, turn on");}} public class DependencyPass1 {public static void main (String [] args) {ChangHong1 changHong = new ChangHong1 () OpenAndClose1 openAndClose = new OpenAndClose1 (); openAndClose.open (changHong);}}

3.2.Constructor pass package com.szh.principle.inversion.improve; / * method 2: pass * / interface IOpenAndClose2 {public void open () through constructor dependency; / / abstract method} interface ITV2 {/ / ITV interface public void play ();} class OpenAndClose2 implements IOpenAndClose2 {public ITV2 tv; / / member public OpenAndClose2 (ITV2 tv) {/ / constructor this.tv = tv } public void open () {this.tv.play ();}} class ChangHong2 implements ITV2 {public void play () {System.out.println ("Changhong TV, on");}} public class DependencyPass2 {public static void main (String [] args) {ChangHong2 changHong = new ChangHong2 (); / / dependency passing through the constructor OpenAndClose2 openAndClose = new OpenAndClose2 (changHong) OpenAndClose.open ();}}

3.3.3 setter method pass package com.szh.principle.inversion.improve; / * Mode 3: pass * / interface IOpenAndClose3 {public void open (); / / Abstract method public void setTv (ITV3 tv) through setter method;} interface ITV3 {/ / ITV interface public void play ();} class OpenAndClose3 implements IOpenAndClose3 {private ITV3 tv; public void setTv (ITV3 tv) {this.tv = tv } public void open () {this.tv.play ();}} class ChangHong3 implements ITV3 {public void play () {System.out.println ("Changhong TV, on");}} public class DependencyPass3 {public static void main (String [] args) {ChangHong3 changHong = new ChangHong3 (); / / dependency passing OpenAndClose3 openAndClose = new OpenAndClose3 () through the setter method OpenAndClose.setTv (changHong); openAndClose.open ();}}

4. Summary of the principle of dependency inversion

As far as possible, low-level modules should have abstract classes or interfaces, or both, so that the program is more stable.

The declaration type of variables is abstract classes or interfaces as far as possible, so that there is a buffer layer between our variable references and actual objects, which is conducive to program expansion and optimization.

Follow the principle of Richter scale substitution when inheriting. (we'll talk about it later)

The above is all the content of the article "what is the principle of dependency reversal in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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