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