为子系统中的一组接口提供了一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用
这要分三个阶段来说,首先,在设计初期阶段,应该要有意识的将不同的两个层分离,比如经典的三层架构,就需要考虑在数据访问层和业务逻辑层,业务逻辑层和表示层的层与层之间建立外观Facada,这样可以为复杂的子系统提供一个简单的jiek,使得耦合大大降低,其次,在开发阶段,子系统往往因为不断的重构演化而变得越来越复杂,大多数的模式使用时也都会产生很多很小的类,这本就是好事,但是也给外部调用他们的用户程序带来了使用上的困难,增加外观Facade 可以提供一个简单的接口,减少他们之间的依赖,第三,在维护一个一流的大型系统时,可能这个系统已经非常难以维护和扩展了,但是因为它包含非常重要的功能,新的需求开发必须要依赖于它,此时用外观模式Facade也是非常合适的,你可以为新系统开发一个外观Facade对象交互,Facade与一流代码交互所有的复杂的工作
#import "SubsystemOne.h" @implementation SubsystemOne -(void)MethodOne{ NSLog(@"方法一"); } @end
其他的三个子类类似
#import <Foundation/Foundation.h> #import "SubsystemOne.h" #import "SubSystemTwo.h" #import "SubSystemThree.h" @interface Facade : NSObject @property(nonatomic,strong)SubsystemOne *one; @property(nonatomic,strong)SubSystemTwo *two; @property(nonatomic,strong)SubSystemThree *three; -(void)MethodA; -(void)MethodB; @end
外观类Facade.m
#import "Facade.h" @implementation Facade - (instancetype)init { self = [super init]; if (self) { _one=[[SubsystemOne alloc]init]; _two=[[SubSystemTwo alloc]init]; _three=[[SubSystemThree alloc]init]; } return self; } -(void)MethodA{ NSLog(@"方法A"); [_one MethodOne]; [_two MethodTwo]; [_three MethodThree]; } -(void)MethodB{ NSLog(@"方法B"); [_one MethodOne]; [_three MethodThree]; } @end
#import <Foundation/Foundation.h> #import "Facade.h" int main(int argc, const char * argv[]) { @autoreleasepool { Facade *facade=[[Facade alloc]init]; [facade MethodA]; [facade MethodB]; } return 0; }