橋接模式
抽象出層次結構。
上層抽象接口的職能,實現上層抽象接口的職能,層級間的通信協議(可以抽象為接口)。
橋接模式的目的,就是把抽象層次結構從具體的實現中分離出來,使其能夠獨立變更。抽象層次定義了供客戶端使用的上層抽象接口。實現結構定義了供抽象層使用的底層接口。實現類的引用被封裝到控制類抽象層的實例中,橋接就形成了。應用,使用場景
游戲機模擬器、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