一,什么是桥接模式

  • 定义

    抽象出层次结构。
    上层抽象接口的职能,实现上层抽象接口的职能,层级间的通信协议(可以抽象为接口)。
    桥接模式的目的,就是把抽象层次结构从具体的实现中分离出来,使其能够独立变更。抽象层次定义了供客户端使用的上层抽象接口。实现结构定义了供抽象层使用的底层接口。实现类的引用被封装到控制类抽象层的实例中,桥接就形成了。

  • 使用场景
    游戏机模拟器、H5混编解决方案

二,桥接模式结构图

  实现步骤

  1. 创建控制类基类,并定义变量持有具体实现类基类对象关系。
  2. 创建控制类基类子类,和实现类基类,并在控制器类接口方法中使用基类实现类对象实现功能(基类已经让控制器建立了持有实现类对象的关系)。
  3. 在控制器内调用控制器子类,建立控制器持有实现类对象。

  结构图
  

三,代码实现

  • 基类

    • 控制器基类

      • ConsoleController

        • ConsoleController.h

          #import <Foundation/Foundation.h>
          #import "ConsoleEmulator.h" @interface ConsoleController : NSObject /**
          * 抽象模拟器
          */
          @property (nonatomic, strong) ConsoleEmulator *emulator; /**
          * 执行指令
          *
          * @param command 指令
          */
          - (void)excuteCommand:(ConsoleCommand)command; @end
        • ConsoleController.m 
          #import "ConsoleController.h"
          
          @implementation ConsoleController
          
          - (void)excuteCommand:(ConsoleCommand)command {
          [_emulator loadInstructionsForCommand:command];
          [_emulator excuteInstructions];
          } @end
    • 实现基类
      • ConsoleEmulator

        • ConsoleEmulator.h

          #import <Foundation/Foundation.h>
          
          typedef enum : NSUInteger {
          
              kConsoleCommandUp,
          kConsoleCommandDown,
          kConsoleCommandLeft,
          kConsoleCommandRight, kConsoleCommandSelect,
          kConsoleCommandStart, kConsoleCommandAction1,
          kConsoleCommandAction2, } ConsoleCommand; @interface ConsoleEmulator : NSObject /**
          * 加载指令
          *
          * @param command 指令
          */
          - (void)loadInstructionsForCommand:(ConsoleCommand)command; /**
          * 执行指令
          */
          - (void)excuteInstructions; @end
        • ConsoleEmulator.m 
          #import "ConsoleEmulator.h"
          
          @implementation ConsoleEmulator
          
          - (void)loadInstructionsForCommand:(ConsoleCommand)command {
          // 由子类重载实现
          }
          - (void)excuteInstructions {
          // 由子类重载实现
          }
          @end

               

  • 子类
    • 控制器基类

      • GameBoyConsoleController

        • GameBoyConsoleController.h

          #import "ConsoleController.h"
          
          @interface GameBoyConsoleController : ConsoleController
          
          - (void)up;
          - (void)down;
          - (void)left;
          - (void)right; - (void)select;
          - (void)start; - (void)action1;
          - (void)action2; @end
        • GameBoyConsoleController.m
          #import "GameBoyConsoleController.h"
          @implementation GameBoyConsoleController
          - (void)up {
          [super excuteCommand:kConsoleCommandUp];
          } - (void)down {
          [super excuteCommand:kConsoleCommandDown];
          }
          - (void)left {
          [super excuteCommand:kConsoleCommandLeft];
          }
          - (void)right {
          [super excuteCommand:kConsoleCommandRight];
          }
          - (void)select {
          [super excuteCommand:kConsoleCommandSelect];
          }
          - (void)start {
          [super excuteCommand:kConsoleCommandStart];
          }
          - (void)action1 {
          [super excuteCommand:kConsoleCommandAction1];
          }
          - (void)action2 {
          [super excuteCommand:kConsoleCommandAction2];
          }
          @end
    • 实现子类
      • GameBoyEmulator

        • GameBoyEmulator.h

          #import "ConsoleEmulator.h"
          @interface GameBoyEmulator : ConsoleEmulator
          - (void)loadInstructionsForCommand:(ConsoleCommand)command;
          - (void)excuteInstructions;
          @end
        • GameBoyEmulator.m
          #import "GameBoyEmulator.h"
          @implementation GameBoyEmulator
          - (void)loadInstructionsForCommand:(ConsoleCommand)command {
          NSLog(@"GameBoyEmulator loadInstructionsForCommand");
          }
          - (void)excuteInstructions {
          NSLog(@"GameBoyEmulator excute");
          }
          @end
  • 打印结果:
    -- ::13.226801+ BridgePattern[:] GameBoyEmulator loadInstructionsForCommand
    -- ::13.226970+ BridgePattern[:] GameBoyEmulator excute
    -- ::13.227075+ BridgePattern[:] GameGearEmulator loadInstructionsForCommand
    -- ::13.227162+ BridgePattern[:] GameGearEmulator excute

四,优缺点

  • 优点: 1、抽象和实现的分离。 2、优秀的扩展能力。 3、实现细节对客户透明。
  • 缺点:桥接模式的引入会增加系统的理解与设计难度,由于聚合关联关系建立在抽象层,要求开发者针对抽象进行设计与编程。

五,demo

  桥接模式

iOS设计模式之桥接模式的更多相关文章

  1. iOS 设计模式之工厂模式

    iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...

  2. 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern)

    原文:乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) 作者:webabcd 介绍 ...

  3. java面试题之----jdbc中使用的设计模式(桥接模式)

    1.JDBC(JavaDatabase Connectivity) JDBC是以统一方式访问数据库的API. 它提供了独立于平台的数据库访问,也就是说,有了JDBC API,我们就不必为访问Oracl ...

  4. php设计模式之桥接模式

    php设计模式之桥接模式 一.概述 桥接模式:将两个原本不相关的类结合在一起,然后利用两个类中的方法和属性,输出一份新的结果. 其实就是讲不相关的东西通过类(本例中是SendInfo)结合在一起,从而 ...

  5. java设计模式7——桥接模式

    java设计模式7--桥接模式 1.桥接模式介绍 桥接模式是将抽象部分与它的实现部分分离,使他们都可以独立的变化.它是一种对象结构型模式,又称为柄体模式或接口模式. 2.解决问题 2.1.将复杂的组合 ...

  6. C#设计模式之八桥接模式(Bridge)【结构型】

    一.引言 今天我们要讲[结构型]设计模式的第二个模式,该模式是[桥接模式],也有叫[桥模式]的.大家第一次看到这个名称会想到什么呢?我第一次看到这个模式根据名称猜肯定是连接什么东西的.因为桥在我们现实 ...

  7. C#设计模式之七桥接模式(Bridge Pattern)【结构型】

    一.引言 今天我们要讲[结构型]设计模式的第二个模式,该模式是[桥接模式],也有叫[桥模式]的,英文名称:Bridge Pattern.大家第一次看到这个名称会想到什么呢?我第一次看到这个模式根据名称 ...

  8. C++设计模式-Bridge桥接模式

    作用:将抽象部份与它的实现部份分离,使它们都可以独立地变化. 将抽象(Abstraction)与实现(Implementation)分离,使得二者可以独立地变化. 桥接模式号称设计模式中最难理解的模式 ...

  9. 设计模式之桥接模式(Bridge)

    注:本文不属于原创,而是根据原文重新整理,原文是:我给媳妇解释设计模式:第一部分 设计模式不是基于理论发明的.相反,总是先有问题场景,再基于需求和情景不断演化设计方案,最后把一些方案标准化成“模式”. ...

随机推荐

  1. nginx下使用asan和valgrind两个静态检查工具

    1.valgrind valgrind安装:参考:https://blog.csdn.net/justheretobe/article/details/52986461 wegit:http://va ...

  2. cookie/http/https

    今天再学习顺便外加复习下http的相关知识,顺便试试在笔记中导出一个长篇的图片回事怎么样的效果. HTTP相关知识,不是很全仅供参考

  3. charles_01_打断点修改接口请求&返回数据

    前言 测试过程中,为了模拟某场景测试,经常需要修改接口请求或者返回数据.可通过抓包工具打断点,实现模拟测试场景.常用的抓包工具charles和fiddler均可打断点mock数据.由于小编安装了cha ...

  4. mooc-IDEA postfix--007

    十三.IntelliJ IDEA -postfix 代码中输入: 总结常用的postfix: 1.for  (<=>100.fori) 2.sout (<=>System.ou ...

  5. [Web 前端] 007 css 常见的七种选择器

    1. 标签选择器 影响范围大 建议尽量应用在层级选择器中 举例 <!-- body 体中的 div --> <div>box...</div> /* style 中 ...

  6. PHP 登陆失效之后,重新登陆,跳转到失效前界面

    登陆失效之后,需要重新进行登陆,登陆之后,进入到默认首页,如果需要继续之前的进行操作,必须重新点击菜单进行跳转,体验不太好 登陆的时候,将之前的url,拼接到登陆界面的url上作为一个redirect ...

  7. Leading and Trailing LightOJ - 1282 题解

    LightOJ - 1282 Leading and Trailing 题解 纵有疾风起 题目大意 题意:给你一个数n,让你求这个数的k次方的前三位和最后三位. \(2<=n<2^{31} ...

  8. arcgis server地图服务切片(10.4.1)

    首先要发布地图服务,过程略 首先,熟悉arcgis server的人应该知道,最直接的切片方式操作方法是在“服务属性”中设置切片,但这种方式可操作性太差,很多设置无法实现,因此不推荐 下面正式开始,打 ...

  9. 2018-8-10-C#-TimeSpan-时间计算

    title author date CreateTime categories C# TimeSpan 时间计算 lindexi 2018-08-10 19:16:51 +0800 2018-06-1 ...

  10. Sql 使用游标

    DECLARE data_cursor CURSOR FOR WITH T0 AS ( SELECT COUNT(f.DeptID) SubmitCount , f.DeptID FROM biz.F ...