iOS设计模式--桥接
来源:互联网 发布:什么叫绿鞋机制知乎 编辑:程序博客网 时间:2024/06/03 16:53
学习设计模式推荐看OBJECTIVE - C设计模式。
何时使用桥接模式
1、不想在抽象与其实现之间形成固定的绑定关系(这样就在运行时切换实现)
2、抽象及其实现都应可以通过子类化独立进行扩展;
3、对抽象的实现进行修改不应影响客户端代码
4、如果每个实现需要额外的子类以细化抽象,则说明有必要把它们分成两个部分
5、想在带有不同抽象接口的多个对象之间共享一个实现
下面用代码说说这个设计模式
先展示下调用的代码
//// ViewController.m// BridgeTest//// Created by qitmac000246 on 15/7/13.// Copyright (c) 2015年 杜甲. All rights reserved.//#import "ViewController.h"#import "TouchConsoleController.h"#import "GameBoyEmulator.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 子功能实现 GameBoyEmulator *gbe = [[GameBoyEmulator alloc] init]; // 功能模块调用 TouchConsoleController *tcc = [[TouchConsoleController alloc] init]; [tcc setEmulator:gbe];// 指定功能对象 [tcc setCommand:kConsoleUp];// 实现 }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
ConsoleEmulator.h
//// ConsoleEmulator.h// BridgeTest//// Created by qitmac000246 on 15/7/14.// Copyright (c) 2015年 杜甲. All rights reserved.//#import <Foundation/Foundation.h>typedef enum{ kConsoleUp,}ConsoleType;// 执行类的抽象父类@interface ConsoleEmulator : NSObject// 抽象的公用方法- (void) loadInstructionsForCommand;@end
ConsoleEmulator.m
#import "ConsoleEmulator.h"@implementation ConsoleEmulator- (void) loadInstructionsForCommand{ }@endGameBoyEmulator.h
#import "ConsoleEmulator.h"@interface GameBoyEmulator : ConsoleEmulator@end
GameBoyEmulator.m
#import "GameBoyEmulator.h"@implementation GameBoyEmulator// 子执行类- (void) loadInstructionsForCommand{ NSLog(@"%s",__func__);}@end
ConsoleController.h
#import <Foundation/Foundation.h>#import "ConsoleEmulator.h"@interface ConsoleController : NSObject// 控制器的父类中定义 执行类的抽象父类@property (nonatomic , strong) ConsoleEmulator *emulator;// 抽象的公用调用方法- (void) setCommand:(ConsoleType)command;@end
ConsoleController.m
#import "ConsoleController.h"@implementation ConsoleController- (void) setCommand:(ConsoleType)command{ [_emulator loadInstructionsForCommand];}@end
TouchConsoleController.h
#import "ConsoleController.h"// 控制器子类@interface TouchConsoleController : ConsoleController- (void) up;@end
TouchConsoleController.m
#import "TouchConsoleController.h"@implementation TouchConsoleController- (void) up{ [super setCommand:kConsoleUp];}@end
代码下载:http://pan.baidu.com/s/1hqo2ka4
0 0
- iOS设计模式--桥接
- iOS设计模式-桥接
- IOS设计模式学习(9)桥接
- ios中的设计模式
- IOS 设计模式
- iOS设计模式:观察者
- iOS设计模式:观察者
- IOS 设计模式
- IOS设计模式总结
- iOS设计模式
- ios设计模式 Category
- ios设计模式 delegate
- ios设计模式 Notification
- iOS设计模式
- iOS 中设计模式
- IOS 设计模式
- 设计模式与iOS
- iOS 设计模式
- oracle卸载 oracle11g安装细节+ x5的配置
- 快速排序(Quicksort)
- Chapter 3. The LWP Class Model
- 链表面试题小结
- Mongodb与Redis应用指标对比
- iOS设计模式--桥接
- xml
- openwrt下VLAN的配置
- 关于ImageLoadingProgressListener不执行的囧事儿。
- iOS xxx.pch
- java4大基本算法加密解析
- Linux 网桥配置命令:brctl
- C++的运算符重载
- GIT使用总结