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

Sample Code Analysis of Java bridging Mode

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

Share

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

This article mainly introduces "Java bridging mode sample code analysis". In daily operation, I believe many people have doubts about Java bridging mode sample code analysis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Java bridging mode sample code analysis". Next, please follow the editor to study!

Define

The bridge mode is the structural mode of the object. Also known as Handle and Body mode or Interface mode. The intention of the bridge model is to "decouple Abstraction from Implementation so that the two can change independently".

Case requirement

Send messages to employees through WeCom and qq

Option one

Define the interface over which messages are sent

/ * * Interface to send messages * @ author:liyajie * @ createTime:2022/2/21 10:33 * @ version:1.0 * / public interface AbstractSendMsgService {/ / send messages public void sendMsg ();}

Define a normal message interface

/ * * ordinary messaging interface * @ author:liyajie * @ createTime:2022/2/21 11:32 * @ version:1.0 * / public interface CommonMsg extends AbstractSendMsgService {}

Define Wechat message implementation class

/ * author:liyajie * @ createTime:2022/2/21 10:35 * @ version:1.0 * / public class WxMsgServiceImpl implements CommonMsg {@ Override public void sendMsg () {System.out.println ("Boss sent you a message using Wechat, go and receive it");}}

Define the QQ message implementation class

/ * author:liyajie * @ createTime:2022/2/21 10:35 * @ version:1.0 * / public class QqMsgServiceImpl implements CommonMsg {@ Override public void sendMsg () {System.out.println ("Boss sent you a message using QQ, go and receive it");}}

Define test classes

/ * Test class * @ author:liyajie * @ createTime:2022/2/21 10:37 * @ version:1.0 * / public class Test {public static void main (String [] args) {new WxMsgServiceImpl () .sendMsg (); new QqMsgServiceImpl () .sendMsg ();}}

Test result

Option 2

Define an abstract interface for sending messages

/ * * the abstract interface for sending messages * @ author:liyajie * @ createTime:2022/2/21 10:33 * @ version:1.0 * / public abstract class NewAbstractSendMsgService {protected SendMsgImplementor sendMsgImplementor; public NewAbstractSendMsgService (SendMsgImplementor sendMsgImplementor) {this.sendMsgImplementor = sendMsgImplementor;} / / Wechat public void sendMsg () {this.sendMsgImplementor.sendMsg ();}}

Define the implementation class of sending messages

/ * author:liyajie * @ createTime:2022/2/21 10:35 * @ version:1.0 * / public class NewSendMsgServiceImpl extends NewAbstractSendMsgService {public NewSendMsgServiceImpl (SendMsgImplementor sendMsgImplementor) {super (sendMsgImplementor);} @ Override public void sendMsg () {super.sendMsg ();}}

Define a unified interface for sending messages

/ * * Unified messaging API * @ author:liyajie * @ createTime:2022/2/21 10:41 * @ version:1.0 * / public interface SendMsgImplementor {void sendMsg ();}

Define a Wechat messaging implementation class

/ * * Wechat send message * @ author:liyajie * @ createTime:2022/2/21 10:45 * @ version:1.0 * / public class WxImplementor implements SendMsgImplementor {@ Override public void sendMsg () {System.out.println ("the boss sent you a message using Wechat, go and receive it");}}

Define the QQ send message implementation class

/ * * QQ send message * @ author:liyajie * @ createTime:2022/2/21 10:45 * @ version:1.0 * / public class QqImplementor implements SendMsgImplementor {@ Override public void sendMsg () {System.out.println ("the boss sent you a message using QQ, go and receive it");}}

Define test classes

/ * author:liyajie * @ createTime:2022/2/21 10:37 * @ version:1.0 * / public class TestNew {public static void main (String [] args) {NewSendMsgServiceImpl newSendMsgService = new NewSendMsgServiceImpl (new QqImplementor ()); newSendMsgService.sendMsg (); NewSendMsgServiceImpl newSendMsgService2 = new NewSendMsgServiceImpl (new WxImplementor ()); newSendMsgService2.sendMsg ();}}

Test result

Comparison and analysis

Scheme 1, that is, the general implementation method, Wechat and QQ need to implement the methods in CommonMsg and AbstractSendMsgService interfaces at the same time, with high coupling; in addition, for example, the requirement of sending emergency messages or SMS messages has many changes and poor expansibility.

In the second scheme, for the implementation of the bridge pattern, the interface SendMsgImplementor is passed into the construction method of the message class, so that the abstract and concrete implementation of sending messages are separated and decoupled. For the need to send emergency messages or send messages by adding SMS messages in the later stage, you only need to add emergency message classes and SMS sending classes, which do not affect each other with QQ, Wechat and other sending messages, and are easy to expand, in line with the principle of opening and closing and the principle of reuse.

At this point, the study of "sample code analysis of Java bridging mode" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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