A factory approach
The factory method allows us to quickly create a method of an instance of a class. With the factory method, the invocation process can be made clearer.
Person.h
1#import <Foundation/Foundation.h>2 3 @interface Person:nsobject4 {5 int_age;6NSString *_name;7 }8 9- (int) age;Ten-(NSString *) name; One A+ (NSString *) Personwithage: (int) Age Andname: (NSString *) name; --(ID) Initwithage: (int) Age Andname: (nsstring*) name; - the@end
Person.m
1#import"Person.h"2 3 @implementation Person4 5- (int) age{6 return_age;7 }8 9-(NSString *) name{Ten return_name; One } A - -+ (NSString *) Personwithage: (int) Age Andname: (NSString *) name{ the return[Person Alloc]initwithage:age andname:name]; - }; - --(ID) Initwithage: (int) Age Andname: (NSString *) name{ +Self =[Super init]; - if(Self! =Nil) { +_age =Age ; A_name =name; at } - returnSelf ; - } - --(NSString *) description{ - return[NSString stringWithFormat:@"The age of the person is%d, the name is%@", Self.age, Self.name]; in}
Main.h
1#import <Foundation/Foundation.h>2#import"Person.h"3 4 5 intMainintargcConst Char*argv[]) {6 7Person *P1 = [Person personwithage: -Andname:@"Lisi"];8NSLog (@"%@", p1);9 Ten return 0; One}
Two-agent design mode
Design principle
Some troublesome things do not want to do their own, you can find a person to help, that is, to the agent to do the object.
Design principles
First, you have a proxy object property. Second, it is very clear what agents have. Finally, be sure to decouple.
Implementation scenarios
Define a protocol, in which you declare some methods of communicating with the agent.
has a proxy attribute id<protocol> delegate
Let the agent comply with protocol.
Example: Nanny and child
1. When a object wants to listen to some changes in the B object, it can use the proxy design mode The nanny wants to monitor the baby's change, then the nanny can become the baby's agent, when the baby changes after the nanny can be heard.
2. When a B object occurs, you want to notify a object, you can use the proxy design mode baby to inform the nanny, then you can let the nanny become an agent of the baby, as long as the nanny become an agent of the baby, after the change in the baby can inform the nanny.
3. When object A cannot handle certain behaviors, it wants object B to handle it (making object B a proxy object for object a) the baby can't eat herself or fall asleep, so the babysitter can help. So long as the babysitter becomes an agent for the baby, it can feed the baby and coax him to sleep.
Tri-Delegate Mode
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; Ten @end
ManDelegate.h
1 #import <Foundation/Foundation.h>23 @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 Ten-(ID) init; One A- (int) age; - -- (void) Setage: (int) age; the --(NSString *) name; - -@property id<mandelegate>Delegate; + -@end
Man.m
1#import"Man.h"2 3 @implementation Mans4 5-(ID) init{6Self =[Super init];7 if(self) {8Self.Delegate=Nil;9_age = -;Ten } One A returnSelf ; - } - the- (int) age{ - return_age; - } - +- (void) Setage: (int) age{ - if(Age! =_age) { + if(self.)Delegate){ A[Self.DelegateOnagechanged:age]; at } - } - -_age =Age ; - } - in-(NSString *) name{ - return @"Lisi"; to } + -@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 @end8 9 @implementation MainlistenerTen One- (void) Onagechanged: (int) age{ ANSLog (@"Age changed, current age is%d", age); - } - the @end - - intMainintargcConst Char*argv[]) { -Man *man =[[Man alloc]init]; + //man.delegate = [[Mainlistener alloc]init]; - [Man setdelegate: [[Mainlistener Alloc]init]]; + A //[man setage:20]; at[Man Setage: +]; - - return 0; -}
Resources:
Http://www.cnblogs.com/king129/p/4943875.html
iOS Basics (design mode)