Java桥接模式示例代码分析
导读:本文共2571.5字符,通常情况下阅读需要9分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 定义桥梁模式是对象的结构模式。又称为柄体(Handle and Body)模式或接口(Interface)模式。桥梁模式的用意是“将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化”。案例需求通过企业微信和qq的方式给员工发送消息方案一定义发送消息的接口/***发送消息的接口*@author:liyajie... ...
目录
(为您整理了一些要点),点击可以直达。定义
桥梁模式是对象的结构模式。又称为柄体(Handle and Body)模式或接口(Interface)模式。桥梁模式的用意是“将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化”。
案例
需求
通过企业微信和qq的方式给员工发送消息
方案一
定义发送消息的接口
/***发送消息的接口*@author:liyajie*@createTime:2022/2/2110:33*@version:1.0*/publicinterfaceAbstractSendMsgService{//发送消息publicvoidsendMsg();}
定义普通消息接口
/***普通消息接口*@author:liyajie*@createTime:2022/2/2111:32*@version:1.0*/publicinterfaceCommonMsgextendsAbstractSendMsgService{}
定义微信消息实现类
/***发送消息的实现接口*@author:liyajie*@createTime:2022/2/2110:35*@version:1.0*/publicclassWxMsgServiceImplimplementsCommonMsg{@OverridepublicvoidsendMsg(){System.out.println("老板使用微信给你发了消息,快去接收吧");}}
定义QQ消息实现类
/***发送消息的实现接口*@author:liyajie*@createTime:2022/2/2110:35*@version:1.0*/publicclassQqMsgServiceImplimplementsCommonMsg{@OverridepublicvoidsendMsg(){System.out.println("老板使用QQ给你发了消息,快去接收吧");}}
定义测试类
/***测试类*@author:liyajie*@createTime:2022/2/2110:37*@version:1.0*/publicclassTest{publicstaticvoidmain(String[]args){newWxMsgServiceImpl().sendMsg();newQqMsgServiceImpl().sendMsg();}}
测试结果
方案二
定义发送消息抽象接口
/***发送消息的抽象接口*@author:liyajie*@createTime:2022/2/2110:33*@version:1.0*/publicabstractclassNewAbstractSendMsgService{protectedSendMsgImplementorsendMsgImplementor;publicNewAbstractSendMsgService(SendMsgImplementorsendMsgImplementor){this.sendMsgImplementor=sendMsgImplementor;}//微信publicvoidsendMsg(){this.sendMsgImplementor.sendMsg();}}
定义发送消息实现类
/***发送消息的实现接口*@author:liyajie*@createTime:2022/2/2110:35*@version:1.0*/publicclassNewSendMsgServiceImplextendsNewAbstractSendMsgService{publicNewSendMsgServiceImpl(SendMsgImplementorsendMsgImplementor){super(sendMsgImplementor);}@OverridepublicvoidsendMsg(){super.sendMsg();}}
定义发送消息统一接口
/***发送消息统一接口*@author:liyajie*@createTime:2022/2/2110:41*@version:1.0*/publicinterfaceSendMsgImplementor{voidsendMsg();}
定义微信发送消息实现类
/***微信发送消息*@author:liyajie*@createTime:2022/2/2110:45*@version:1.0*/publicclassWxImplementorimplementsSendMsgImplementor{@OverridepublicvoidsendMsg(){System.out.println("老板使用微信给你发了消息,快去接收吧");}}
定义QQ发送消息实现类
/***QQ发送消息*@author:liyajie*@createTime:2022/2/2110:45*@version:1.0*/publicclassQqImplementorimplementsSendMsgImplementor{@OverridepublicvoidsendMsg(){System.out.println("老板使用QQ给你发了消息,快去接收吧");}}
定义测试类
/***测试类*@author:liyajie*@createTime:2022/2/2110:37*@version:1.0*/publicclassTestNew{publicstaticvoidmain(String[]args){NewSendMsgServiceImplnewSendMsgService=newNewSendMsgServiceImpl(newQqImplementor());newSendMsgService.sendMsg();NewSendMsgServiceImplnewSendMsgService2=newNewSendMsgServiceImpl(newWxImplementor());newSendMsgService2.sendMsg();}}
测试结果
对比分析
方案一,即一般实现方式,微信和QQ两种发送消息的方式需要同时去实现CommonMsg和AbstractSendMsgService两个接口中的方法,耦合性较高;另外比如新增紧急消息的发送或者新增短信的方式发送消息的需求,改动较多,扩展性较差。
方案二,为桥接模式实现方式,在消息类的构造方法中传入接口SendMsgImplementor,从而使发送消息的抽象和具体实现进行了分离,实现了解耦;后期新增紧急消息的发送或者新增短信的方式发送消息的需求,也只需要新增紧急消息类和短信发送类即可,和QQ,微信等发送消息互不影响,扩展方便,符合开闭原则,复用原则。
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
Java桥接模式示例代码分析的详细内容,希望对您有所帮助,信息来源于网络。