Login.h
@interface Login : NSObject -(void)isLogin; @end
Login.m
#import "Login.h" @implementation Login -(void)isLogin { NSLog(@"it is login"); } @end
Browse.h
@interface Browse : NSObject -(void)isBrowse; @end
Browse.m
#import "Browse.h" @implementation Browse -(void)isBrowse { NSLog(@"is browse"); } @end
Download.h
@interface Download : NSObject -(void)isDownload; @end
DownLoad.m
#import "Download.h" @implementation Download -(void)isDownload { NSLog(@"is doownload"); } @end
Logout.h
@interface Logout : NSObject -(void)isLogout; @end
Logout.m
#import "Logout.h" @implementation Logout -(void)isLogout { NSLog(@"is logout"); } @end
Facade.h
#import "Login.h" #import "Browse.h" #import "Download.h" #import "Logout.h" @interface Facade : NSObject { Login *_login; Browse *_browse; Download *_download; Logout *_logout; } @property(nonatomic,retain)Login *login; @property(nonatomic,retain)Browse *browse; @property(nonatomic,retain)Download *download; @property(nonatomic,retain)Logout *logout; -(void)control; @end
Facade.m
#import "Facade.h" @implementation Facade -(void)setLogin:(Login *)aLogin { if (_login != aLogin) { [_login release]; _login = [aLogin retain]; } } -(Login *)login { return _login; } -(void)setBrowse:(Browse *)aBrowse { if (_browse != aBrowse) { [_browse release]; _browse = [aBrowse retain]; } } -(Browse *)browse { return _browse; } -(void)setDownload:(Download *)aDownload { if (_download != aDownload) { [_download release]; _download = [aDownload retain]; } } -(Download *)download { return _download; } -(void)setLogout:(Logout *)aLogout { if (_logout != aLogout) { [_logout release]; _logout = [aLogout retain]; } } -(Logout *)logout { return _logout; } -(id)init { self = [super init]; if (self) { Login *l = [[Login alloc] init]; self.login = l; [l release]; Browse *b = [[Browse alloc]init]; self.browse = b; [b release]; Download *d = [[Download alloc]init]; self.download = d; [d release]; Logout *lo = [[Logout alloc]init]; self.logout = lo; [lo release]; } return self; } -(void)control { [self.login isLogin]; [self.browse isBrowse]; [self.download isDownload]; [self.logout isLogout]; } -(void)dealloc { [_login release]; [_browse release]; [_download release]; [_logout release]; [super dealloc];//这里需要写到最后,如果写在release前会先释放父类的,易出问题 } @end
AppDelegate.m
Facade *facade = [[Facade alloc]init];
[facade control];
[facade release];
使用类似场景:买基金,找个专业人员帮忙管理。旅游,找个旅行团一条路游玩。
不足:违背了“开闭原则”(通过多态继承达到扩展)。