IOS基礎之 (設計模式)

來源:互聯網
上載者:User

標籤:

一 Factory 方法

 Factory 方法方便我們快速建立類的執行個體的方法。通過Factory 方法,可以讓調用過程更加清晰。

 Person.h

 1 #import <Foundation/Foundation.h> 2  3 @interface Person : NSObject 4 { 5     int _age; 6     NSString * _name; 7 } 8  9 - (int)age;10 - (NSString *) name;11 12 + (NSString *) personWithAge:(int)age andName:(NSString *) name;13 - (id) initWithAge:(int)age andName:(NSString*)name;14 15 @end

Person.m

 1 #import "Person.h" 2  3 @implementation Person 4  5 - (int)age{ 6     return _age; 7 } 8  9 - (NSString *) name{10     return _name;11 }12 13 14 + (NSString *)personWithAge:(int)age andName:(NSString *)name{15         return [[Person  alloc ]initWithAge:age andName:name ];16 };17 18 - (id)initWithAge:(int)age andName:(NSString *)name{19     self = [super init];20     if(self != nil){21         _age = age;22         _name = name;23     }24     return self;25 }26 27 - (NSString *)description{28     return [NSString stringWithFormat: @"人的年齡是%d,名字是%@", self.age, self.name ];29 }

main.h

 1 #import <Foundation/Foundation.h> 2 #import "Person.h" 3  4  5 int main(int argc, const char * argv[]) { 6      7     Person *p1 = [Person personWithAge:20 andName: @"lisi"]; 8     NSLog(@"%@"  , p1); 9     10     return 0;11 }

 

二 代理設計模式

設計原理

    有些麻煩的事情不想自己親自做,就可以找個人幫忙,即交給代理對象去做。

 

設計原則

    首先的擁有某個代理對象屬性。其次要很清楚代理有哪些方法。最後要保證能解耦。

 

實現方案

   定義一個protocol,在其中聲明一些和代理溝通的方法。

   擁有一個代理屬性 id<protocol> delegate

   讓代理遵守protocol.

 

執行個體: 保姆與小孩

1.當A對象想監聽B對象的一些變化時, 可以使用代理設計模式 保姆想監聽嬰兒的變化, 那麼保姆就可以成為嬰兒的代理, 當嬰兒發生變化之後保姆就可以監聽到.
2.當B對象發生一些事情, 想通知A對象的時候, 可以使用代理設計模式 嬰兒想通知保姆, 那麼就可以 讓保姆成為嬰兒的代理, 只要保姆成為嬰兒的代理, 以後嬰兒發生變化就可以通知保姆.
3.當對象A無法處理某些行為的時候,想讓對象B幫忙處理(讓對象B成為對象A的代理對象) 嬰兒無法自己吃東西, 也無法自己入睡, 所以可以讓保姆幫忙處理. 只要讓保姆成為嬰兒的代理就可以幫嬰兒喂它吃東西和哄他睡覺.

 

三 delegate模式

 IPeople.h

 1 #import <Foundation/Foundation.h> 2  3 @protocol IPeople <NSObject> 4  5 - (int) age; 6  7 - (void)setAge:(int)age; 8  9 - (NSString*)name;10 11 @end

 

ManDelegate.h

1 #import <Foundation/Foundation.h>2 3 @protocol ManDelegate <NSObject>4 5 - (void) onAgeChanged:(int) age;6 7 @end

 

Man.h

 1 #import <Foundation/Foundation.h> 2 #import "IPeople.h" 3 #import "ManDelegate.h" 4  5 @interface Man : NSObject<IPeople> 6 { 7     int _age; 8 } 9 10 - (id) init;11 12 - (int) age;13 14 - (void) setAge:(int)age;15 16 - (NSString *)name;17 18 @property id<ManDelegate> delegate;19 20 @end

 

Man.m

 1 #import "Man.h" 2  3 @implementation Man 4  5 - (id)init{ 6     self = [super init]; 7     if( self ){ 8         self.delegate=nil; 9         _age = 20;10     }11     12     return self;13 }14 15 - (int) age{16     return _age ;17 }18 19 - (void)setAge:(int)age{20     if (age != _age) {21         if(self.delegate){22             [self.delegate onAgeChanged:age];23         }24     }25     26     _age = age;27 }28 29 - (NSString *)name{30       return @"lisi";31 }32 33 @end

 

Main.m

 1 #import <Foundation/Foundation.h> 2 #import "Man.h" 3 #import "ManDelegate.h" 4  5 @interface MainListener : NSObject<ManDelegate> 6    - (void) onAgeChanged:(int) age; 7 @end 8  9 @implementation MainListener10 11 - (void)onAgeChanged:(int)age{12     NSLog(@"Age changed, Current age is %d" , age);13 }14 15 @end16 17 int main(int argc, const char * argv[]) {18     Man *man = [[Man alloc]init];19    // man.delegate = [[MainListener alloc]init];20     [man setDelegate: [[MainListener alloc]init]  ];21     22     //[man setAge:20];23     [man setAge:21];24     25     return 0;26 }

 

 

參考資料:

http://www.cnblogs.com/king129/p/4943875.html

 

IOS基礎之 (設計模式)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.