iOS 桥接模式

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

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

游戏机系统抽象类(控制类/协议)

//
//  AbstractSystem.h
//  LearnBridge
//
//  Created by 印林泉 on 2017/3/7.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AbstractImplementor.h"

/**
 控制类
 */
@interface AbstractSystem : NSObject

@property (strong, nonatomic)AbstractImplementor *implementor;

/**
 加载系统
 */
- (void)loadSystem;

- (void)commandUp;
- (void)commandDown;
- (void)commandLeft;
- (void)commandRight;
- (void)commandA;
- (void)commandB;

@end
//
//  AbstractSystem.m
//  LearnBridge
//
//  Created by 印林泉 on 2017/3/7.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "AbstractSystem.h"

@implementation AbstractSystem

- (void)loadSystem {
    
}

- (void)commandUp {
    [self.implementor loadCommand:kUp];
}

- (void)commandDown {
    [self.implementor loadCommand:kDown];
}

- (void)commandLeft {
    [self.implementor loadCommand:kLeft];
}

- (void)commandRight {
    [self.implementor loadCommand:kRight];
}

- (void)commandA {
    [self.implementor loadCommand:kA];
}

- (void)commandB {
    [self.implementor loadCommand:kB];
}

@end

游戏机执行抽象类(执行类/协议)

//
//  AbstractImplementor.h
//  LearnBridge
//
//  Created by 印林泉 on 2017/3/7.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef enum : NSInteger {
    kUp,
    kDown,
    kLeft,
    kRight,
    kA,
    kB,
    kO,
    kX,
} ECommandType;
/**
 执行类
 */
@interface AbstractImplementor : NSObject

- (void)loadCommand:(ECommandType)command;

@end
//
//  AbstractImplementor.m
//  LearnBridge
//
//  Created by 印林泉 on 2017/3/7.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "AbstractImplementor.h"

@implementation AbstractImplementor

- (void)loadCommand:(ECommandType)command {
    
}

@end

GBA游戏机系统类

//
//  GBASystem.h
//  LearnBridge
//
//  Created by 印林泉 on 2017/3/7.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "AbstractSystem.h"

@interface GBASystem : AbstractSystem

@end
//
//  GBASystem.m
//  LearnBridge
//
//  Created by 印林泉 on 2017/3/7.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "GBASystem.h"

@implementation GBASystem

- (void)loadSystem {
    NSLog(@"GBASystem");
}

@end

GBA执行类

//
//  GBAImplementor.h
//  LearnBridge
//
//  Created by 印林泉 on 2017/3/7.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "AbstractImplementor.h"

@interface GBAImplementor : AbstractImplementor

@end
//
//  GBAImplementor.m
//  LearnBridge
//
//  Created by 印林泉 on 2017/3/7.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "GBAImplementor.h"

@implementation GBAImplementor

- (void)loadCommand:(ECommandType)command {
    switch (command) {
        case kUp:
            NSLog(@"GBA up");
            break;
        case kDown:
            NSLog(@"GBA down");
            break;
        case kLeft:
            NSLog(@"GBA left");
            break;
        case kRight:
            NSLog(@"GBA right");
            break;
        case kA:
            NSLog(@"GBA A");
            break;
        case kB:
            NSLog(@"GBA B");
            break;
        default:
            NSLog(@"GBA none");
            break;
    }
}

@end

PSP游戏机系统类(扩展X、O)

//
//  PSPSystem.h
//  LearnBridge
//
//  Created by 印林泉 on 2017/3/7.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "AbstractSystem.h"

@interface PSPSystem : AbstractSystem

- (void)commandX;
- (void)commandO;

@end
//
//  PSPSystem.m
//  LearnBridge
//
//  Created by 印林泉 on 2017/3/7.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "PSPSystem.h"

@implementation PSPSystem

- (void)loadSystem {
    NSLog(@"PSPSystem");
}

- (void)commandX {
    [self.implementor loadCommand:kX];
}

- (void)commandO {
    [self.implementor loadCommand:kO];
}

@end

PSP执行类

//
//  PSPImplementor.h
//  LearnBridge
//
//  Created by 印林泉 on 2017/3/7.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "AbstractImplementor.h"

@interface PSPImplementor : AbstractImplementor

@end
//
//  PSPImplementor.m
//  LearnBridge
//
//  Created by 印林泉 on 2017/3/7.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "PSPImplementor.h"

@implementation PSPImplementor

- (void)loadCommand:(ECommandType)command {
    switch (command) {
        case kUp:
            NSLog(@"PSP up");
            break;
        case kDown:
            NSLog(@"PSP down");
            break;
        case kLeft:
            NSLog(@"PSP left");
            break;
        case kRight:
            NSLog(@"PSP right");
            break;
        case kA:
            NSLog(@"PSP A");
            break;
        case kB:
            NSLog(@"PSP B");
            break;
        case kX:
            NSLog(@"PSP X");
            break;
        case kO:
            NSLog(@"PSP O");
            break;
        default:
            NSLog(@"PSP none");
            break;
    }
}

@end

应用

//
//  ViewController.m
//  LearnBridge
//
//  Created by 印林泉 on 2017/3/7.
//  Copyright © 2017年 ylq. All rights reserved.
//

#import "ViewController.h"
#import "GBASystem.h"
#import "GBAImplementor.h"
#import "PSPSystem.h"
#import "PSPImplementor.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    AbstractSystem *gbaSystem = [[GBASystem alloc] init];
    gbaSystem.implementor = [[GBAImplementor alloc] init];
    [gbaSystem loadSystem];
    [gbaSystem commandUp];
    
    PSPSystem *pspSystem = [[PSPSystem alloc] init];
    pspSystem.implementor = [[PSPImplementor alloc] init];
    [pspSystem loadSystem];
    [pspSystem commandX];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 生活中的场景: 就拿汽车在路上行驶的来说。即有小汽车又有公共汽车,它们都不但能在市区中的公路上行驶,也能在高速公路...
    LikeSomeBody阅读 2,677评论 0 6
  • 1 场景问题# 1.1 发送提示消息## 考虑这样一个实际的业务功能:发送提示消息。基本上所有带业务流程处理的系统...
    七寸知架构阅读 5,102评论 5 63
  • 生活中总是遇到难以排遣,难以解决的困难,但是与我而言,最大的事情是缺钱,可以说我拜金,可以说我太烂,但是,金钱,时...
    梦花生阅读 525评论 1 5
  • Richard是我儿子,我自认不是事业心很强的人,但却因为工作在他最需要陪伴的时候离开。Richard刚出生后不久...
    PM胡爷阅读 260评论 0 0
  • 二十几岁的年龄,正是一个男人拼搏的黄金时段,如果在这个时间你为了你认为的所谓的爱情而浪费时间,真是太不值得了。二十...
    稲薄堡阅读 181评论 0 1