iOS 橋接模式

  • 橋接模式
    抽象出層次結(jié)構(gòu)。
    上層抽象接口的職能,實現(xiàn)上層抽象接口的職能,層級間的通信協(xié)議(可以抽象為接口)。
    橋接模式的目的,就是把抽象層次結(jié)構(gòu)從具體的實現(xiàn)中分離出來,使其能夠獨立變更。抽象層次定義了供客戶端使用的上層抽象接口。實現(xiàn)結(jié)構(gòu)定義了供抽象層使用的底層接口。實現(xiàn)類的引用被封裝到控制類抽象層的實例中,橋接就形成了。

  • 應(yīng)用,使用場景
    游戲機(jī)模擬器、H5混編解決方案

游戲機(jī)系統(tǒng)抽象類(控制類/協(xié)議)

//
//  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;

/**
 加載系統(tǒng)
 */
- (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

游戲機(jī)執(zhí)行抽象類(執(zhí)行類/協(xié)議)

//
//  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;
/**
 執(zhí)行類
 */
@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游戲機(jī)系統(tǒng)類

//
//  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執(zhí)行類

//
//  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游戲機(jī)系統(tǒng)類(擴(kuò)展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執(zhí)行類

//
//  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

應(yīng)用

//
//  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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 生活中的場景: 就拿汽車在路上行駛的來說。即有小汽車又有公共汽車,它們都不但能在市區(qū)中的公路上行駛,也能在高速公路...
    LikeSomeBody閱讀 2,671評論 0 6
  • 1 場景問題# 1.1 發(fā)送提示消息## 考慮這樣一個實際的業(yè)務(wù)功能:發(fā)送提示消息。基本上所有帶業(yè)務(wù)流程處理的系統(tǒng)...
    七寸知架構(gòu)閱讀 5,101評論 5 63
  • 生活中總是遇到難以排遣,難以解決的困難,但是與我而言,最大的事情是缺錢,可以說我拜金,可以說我太爛,但是,金錢,時...
    夢花生閱讀 525評論 1 5
  • Richard是我兒子,我自認(rèn)不是事業(yè)心很強(qiáng)的人,但卻因為工作在他最需要陪伴的時候離開。Richard剛出生后不久...
    PM胡爺閱讀 260評論 0 0
  • 二十幾歲的年齡,正是一個男人拼搏的黃金時段,如果在這個時間你為了你認(rèn)為的所謂的愛情而浪費時間,真是太不值得了。二十...
    稲薄堡閱讀 180評論 0 1