赞
踩
桥接模式(Bridge Pattern))也称为桥梁模式、接口(Interfce)模式或柄体(Handle and Body)模式,是将抽象部分与它的具体实现部分分离,使它们都可以独立地变化
通过组合的方式建立两个类之间的联系,而不是继承
属于结构型模式。
1、在抽象和具体实现之间需要增加更多的灵活性的场景
2、一个类存在两个(或多个)独立变化的维度,而这两个(或多个)维度都需要独立进行扩展
3、不希望使用继承,或因为多层继承导致系统类的个数剧增
连接起两个空间维度的桥
发达:就是抽象维度;
虚拟网络与真实网络的桥接
1、分离抽象部分及其具体实现部分
2、提高了系统的扩展性
3、符合开闭原则
4、符合合成复用原则
1、增加了系统的理解与设计难度
2、需要正确地识别系统中两个独立变化的维度
- public interface ICourse {
- }
-
- public interface INote {
- void edit();
- }
- public interface IVideo {
- }
-
- public class JavaNote implements INote {
- @Override
- public void edit() {
-
- }
- }
-
- public class JavaVideo implements IVideo {
- }
-
- public class PythonNote implements INote {
- @Override
- public void edit() {
-
- }
- }
-
- public class PythonVideo implements IVideo {
- }
-
-
- // 定义一个桥接
- public class AbstractCourse implements ICourse {
- private INote note;
- private IVideo video;
-
- public void setNote(INote note) {
- this.note = note;
- }
-
- public void setVideo(IVideo video) {
- this.video = video;
- }
-
- @Override
- public String toString() {
- return "AbstractCourse{" +
- "note=" + note +
- ", video=" + video +
- '}';
- }
- }
-
- public class JavaCourse extends AbstractCourse {
- }
-
- public class PythonCourse extends AbstractCourse {
- }
t图:
- public interface IMessage {
- //发送消息的内容和接收人
- void send(String message,String toUser);
- }
- public class EmailMessage implements IMessage {
- @Override
- public void send(String message, String toUser) {
- System.out.println("使用邮件消息发送" + message + "给" + toUser);
- }
- }
- public class SmsMessage implements IMessage {
- @Override
- public void send(String message, String toUser) {
- System.out.println("使用短信消息发送" + message + "给" + toUser);
- }
- }
- //俺是桥接,一个是抽象,一个是具体实现,但是我拥有的必须是行为,是抽象,
- public abstract class AbastractMessage {
- private IMessage message;
- public AbastractMessage(IMessage message) {
- this.message = message;
- }
-
- void sendMessage(String message,String toUser){
- this.message.send(message,toUser);
- }
- }
-
- public class UrgencyMessage extends AbastractMessage {
- public UrgencyMessage(IMessage message) {
- super(message);
- }
-
- @Override
- void sendMessage(String message, String toUser){
- message = "【加急】" + message;
- super.sendMessage(message,toUser);
- }
-
- public Object watch(String messageId){
- return null;
- }
- }
-
- public class NomalMessage extends AbastractMessage {
- public NomalMessage(IMessage message) {
- super(message);
- }
- }
-
-
- public class Test {
- public static void main(String[] args) {
- IMessage message = new SmsMessage();
- AbastractMessage abastractMessage = new NomalMessage(message);
- abastractMessage.sendMessage("加班申请","王总");
-
- message = new EmailMessage();
- abastractMessage = new UrgencyMessage(message);
- abastractMessage.sendMessage("加班申请","王总");
- }
-
- }
图2:
图2-1:
- // jdbc是抽象模块
- public interface Driver {
-
- Connection connect(String url, java.util.Properties info)
- throws SQLException;
- }
-
- // mysql是具体实现Driver
- public class Driver extends NonRegisteringDriver implements java.sql.Driver {
- public Driver() throws SQLException {
- }
-
- static {
- try {
- // msyql实现给new进行,添加到接口
- DriverManager.registerDriver(new Driver());
- } catch (SQLException var1) {
- throw new RuntimeException("Can't register driver!");
- }
- }
- }
-
- //
- public static synchronized void registerDriver(java.sql.Driver driver,
- DriverAction da)
- throws SQLException {
-
- /* Register the driver if it has not already been added to our list */
- if(driver != null) {
- registeredDrivers.addIfAbsent(new DriverInfo(driver, da));
- } else {
- // This is for compatibility with the original DriverManager
- throw new NullPointerException();
- }
-
- println("registerDriver: " + driver);
-
- }
适配器强调的是兼容
桥接强调是连接
适配器必须要实现一个功能
桥接是指定一个规则
1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。