一 工厂方法
工厂方法方便我们快速创建类的实例的方法。通过工厂方法,可以让调用过程更加清晰。
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 MainListener
10
11 - (void)onAgeChanged:(int)age{
12 NSLog(@"Age changed, Current age is %d" , age);
13 }
14
15 @end
16
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 }
一个知识点,你自己看懂了,那是一个层次;你会用,是另外一个层次;你写出来,你写出来让别人懂,那又是更高的一个层次;你用最通俗的言语,把知识点讲出来,让别人一下子明白,又是更高的一个层次。