国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOS設計模式之中介者模式

2019-11-14 18:34:39
字體:
來源:轉載
供稿:網友

中介者模式

基本理解

  • 中介者模式又叫做調停者模式,其實就是中間人或者調停者的意思。
  • 盡管將一個系統分割成許多對象通常可以增加可復用性,但是對象之間的連接又降低了可復用性。
  • 如果兩個類不必彼此直接通信,那么著兩個類就不應當發生直接的相互作用。如果其中一個類需要調用另一個類的方法的話,可以用過第三者轉發這個調用。而這個第三者就是中介者。
  • 概念:中介者模式(Mediator),用一個中介者對象來封裝一系列的對象交互。中介者使各個對象不需要顯式地相互引用,從而使其耦合松散,而且可 以獨立地改變他們之間的交互。
  • UINavigationViewController就是屬于一個中介者。

中介者模式的優缺點

中介者模式很容易在系統中應用,也很容易在系統中誤用。當系統出現了多對多交互復雜的對象群時,不要急于使用中介者模式,而要先反思你在系統上設計是否合理。
優點就是集中控制,減少了對象之間的耦合度。缺點就是太過于集中。

應用場景

  • 對象間的交互雖定義明確然而非常復雜,導致一組對象彼此相互依賴而且難以理解。
  • 因為對象引用了許多其他對象并與其通信,導致對象難以復用。
  • 想要定制一個分布在多個類中的邏輯或者行為,又不想生成太多子類。

例子

CoordinatingViewController.h

////  CoordinatingViewController.h//  CoordinateDemo////  Created by zhanggui on 15/8/6.//  Copyright (c) 2015年 zhanggui. All rights reserved.//#import <UIKit/UIKit.h>typedef NS_ENUM(NSInteger,ButtonTag) {    kButtontagOpenThree,    kButtontagOpenThreeNext,    kButtontagBackThree};@interface CoordinatingViewController : UIViewController@PRoperty(nonatomic,strong)NSMutableArray *controllersArray;@property(nonatomic,strong)UIViewController *activeController;@property(nonatomic,strong)UIViewController *mainViewController;+(instancetype)shareInstance;- (void)requestViewChangeByObject:(id)sender;@end

CoordinationgViewController.m

////  CoordinatingViewController.m//  CoordinateDemo////  Created by zhanggui on 15/8/6.//  Copyright (c) 2015年 zhanggui. All rights reserved.//#import "CoordinatingViewController.h"#import "ThirdViewController.h"#import "Third2ViewController.h"@interface CoordinatingViewController (){//    UIStoryboard *storyboard;}@end@implementation CoordinatingViewController+(instancetype)shareInstance{    static CoordinatingViewController *coorVC;    if (coorVC==nil) {        coorVC = [[self alloc] init];    }    return coorVC;}- (void)viewDidLoad {    [super viewDidLoad];//    storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];    // Do any additional setup after loading the view.}- (void)requestViewChangeByObject:(id)sender {    UIStoryboard *storyboard =[UIStoryboard storyboardWithName:@"Main" bundle:nil];    if ([sender isKindOfClass:[UIButton class]]) {        switch ([sender tag]) {            case kButtontagOpenThree:            {                ThirdViewController *thirdVC = [storyboard instantiateViewControllerWithIdentifier:@"ThirdViewController"];                self.activeController = thirdVC;                [self.controllersArray addObject:thirdVC];//                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:thirdVC];                [self.mainViewController presentViewController:thirdVC animated:YES completion:nil];            }                                break;            case kButtontagOpenThreeNext:            {                Third2ViewController *third2VC = [storyboard instantiateViewControllerWithIdentifier:@"Third2ViewController"];                [self.controllersArray addObject:third2VC];                UIViewController *cvc = [self.controllersArray objectAtIndex:1];                [cvc presentViewController:third2VC animated:YES completion:nil];            }                break;            case kButtontagBackThree:            {                UIViewController *cvc = [self.controllersArray objectAtIndex:2];                [cvc dismissViewControllerAnimated:YES completion:nil];                [self.controllersArray removeObjectAtIndex:2];            }                break;            default:            {                UIViewController *cvc = [self.controllersArray objectAtIndex:1];                [cvc dismissViewControllerAnimated:YES completion:nil];                [self.controllersArray removeObjectAtIndex:1];            }                break;        }    }}@end

上面這個就是中介類。
在ViewController.m

////  ViewController.m//  CoordinateDemo////  Created by zhanggui on 15/8/6.//  Copyright (c) 2015年 zhanggui. All rights reserved.//#import "ViewController.h"#import "CoordinatingViewController.h"@interface ViewController (){    CoordinatingViewController *coorController;}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    coorController = [CoordinatingViewController shareInstance];    coorController.controllersArray = [[NSMutableArray alloc] initWithObjects:self, nil];    _firstButton.tag = kButtontagOpenThree;    coorController.activeController = self;    coorController.mainViewController = self;    // Do any additional setup after loading the view, typically from a nib.}- (IBAction)showAction:(id)sender {    [coorController requestViewChangeByObject:_firstButton];}@end

ThirdViewController.m

////  ThirdViewController.m//  CoordinateDemo////  Created by zhanggui on 15/8/6.//  Copyright (c) 2015年 zhanggui. All rights reserved.//#import "ThirdViewController.h"#import "CoordinatingViewController.h"@interface ThirdViewController ()@end@implementation ThirdViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (IBAction)forwardAction:(id)sender {    if ([sender isKindOfClass:[UIButton class]]) {        UIButton *btn = (UIButton *)sender;        btn.tag = kButtontagOpenThreeNext;        CoordinatingViewController *coor = [CoordinatingViewController shareInstance];        [coor requestViewChangeByObject:btn];    }}@end

Third2ViewController.m

////  Third2ViewController.m//  CoordinateDemo////  Created by zhanggui on 15/8/6.//  Copyright (c) 2015年 zhanggui. All rights reserved.//#import "Third2ViewController.h"#import "CoordinatingViewController.h"@interface Third2ViewController ()@end@implementation Third2ViewController- (void)viewDidLoad {    [super viewDidLoad];        // Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/- (IBAction)backAction:(id)sender {    if ([sender isKindOfClass:[UIButton class]]) {        UIButton *btn = (UIButton *)sender;        btn.tag = kButtontagBackThree;        CoordinatingViewController *coor = [CoordinatingViewController shareInstance];        [coor requestViewChangeByObject:btn];    }}- (IBAction)forwardAction:(id)sender {}@end

上面的這兩個就是視圖遷移的中介處理ThirdViewController和Third2ViewController。

附:


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岱山县| 水富县| 和顺县| 拜泉县| 正阳县| 凌云县| 阆中市| 浪卡子县| 青阳县| 新邵县| 扶沟县| 西吉县| 峨眉山市| 安泽县| 张家口市| 兴城市| 通山县| 安徽省| 萨嘎县| 蕉岭县| 高阳县| 红安县| 新宾| 合肥市| 莱阳市| 阿克苏市| 宜州市| 错那县| 庄浪县| 富宁县| 遂宁市| 拉孜县| 峨眉山市| 新源县| 武邑县| 平泉县| 平遥县| 奈曼旗| 双鸭山市| 新竹县| 滁州市|