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

A case study of the principle of Java dependency inversion

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I would like to share with you the relevant knowledge points of the case study of Java dependence inversion principle. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Define

Dependency inversion principle, also known as dependency inversion principle (Dependence Inversion Principle), also known as DIP principle, that is, upper modules should not rely on underlying modules, they should all rely on abstraction, abstraction should not depend on details, details should depend on abstraction. Abstraction for code is the interface or abstract class details for the code is the implementation class. In other words, the idea that relies on the core of the inversion principle is much more stable than the details. It requires us to program and design for the interface.

Case requirement

The staff received the overtime message from the boss of Wechat.

Option one

Define staff Worker.java

/ * * staff * @ author:liyajie * @ createTime:2022/1/30 20:08 * @ version:1.0 * / public class Worker {/ * staff receives messages * @ author:liyajie * @ date: 20:10 on 2022-1-30 * @ param weChat * @ return void * @ exception: * @ update: * @ updatePerson: * * / public void getMessage (WeChat weChat) {weChat.sendMessage () }}

Define Wechat message class WeChat.java

/ * * Wechat message class * @ author:liyajie * @ createTime:2022/1/30 20:07 * @ version:1.0 * / public class WeChat {/ * message sent by Wechat * @ author:liyajie * @ date: 20:10 on 2022-1-30 * @ param * @ return void * @ exception: * @ update: * @ UpdatePerson: * * / public void sendMessage () {System.out.println (on Wechat The boss asked you to work overtime. }}

Define the test class Test1.java

Public class Test1 {public static void main (String [] args) {new Worker () .getMessage (new WeChat ());}} scenario II

Define message interface IMessage.java

/ * message interface * @ author:liyajie * @ createTime:2022/1/30 20:15 * @ version:1.0 * / public interface IMessage {void sendMessage ();}

Define Wechat message class WeChatNew.java

/ * * Wechat message class * @ author:liyajie * @ createTime:2022/1/30 20:07 * @ version:1.0 * / public class WeChatNew implements IMessage {/ * message sent by Wechat * @ author:liyajie * @ date: 20:10 on 2022-1-30 * @ param * @ return void * @ exception: * @ update: * UpdatePerson: * * / @ Override public void sendMessage () {System.out.println ("on Wechat The boss asked you to work overtime. }}

Define the flying book class FeiShu.java

/ * @ author:liyajie * @ createTime:2022/1/30 20:16 * @ version:1.0 * / public class FeiShu implements IMessage {@ Override public void sendMessage () {System.out.println ("the boss called you to work overtime");}}

Define the staff class WorkerNew.java

/ * author:liyajie * @ createTime:2022/1/30 20:18 * @ version:1.0 * / public class WorkerNew {/ * staff receives messages * @ author:liyajie * @ date: 2022-1-30 20:10 * @ param iMessage * @ return void * @ exception: * @ update: * @ updatePerson: * * / public void getMessage (IMessage iMessage) {iMessage.sendMessage () }}

Define the test class Test2.java

Public class Test2 {public static void main (String [] args) {/ / Wechat new WorkerNew () .getMessage (new WeChatNew ()); / / Feishu new WorkerNew () .getMessage (new FeiShu ());}} Comparative Analysis

Solution 1 violates the principle of dependency inversion. If the functional requirements are expanded, for example, we need to expand a flying book to send messages, we need to add a flying book class and implement the function of sending messages. The staff class also needs to modify the method of receiving messages, and the client also needs to make corresponding changes, which is risky.

The second scheme abides by the principle of dependency inversion, the same requirement expansion, abstracting a common message interface, all Wechat, Feishu and other classes that send messages as long as they implement this interface and rewrite the method of sending messages. the staff's message receiving method takes the message interface as the input parameter, and the client only needs to pass in the corresponding message entity class, which is convenient for expansion and low coupling.

These are all the contents of the article "case study of Java dependency inversion principle". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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