An explanation of the viewer design pattern
- An explanation of the viewer design pattern
- Basic concepts
- Use of Nsnotificationcenter
- Add Listener
- Receiving messages
- Delete Monitoring
- Use of KVO
- Basic concepts
- Registered Observer
- callback method when the Observer object has changed
- Remove Observer identity
- Code implementation
Basic concepts
The Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time. This subject object notifies all observer objects when the state changes, enabling them to automatically update themselves. And in iOS development we may be exposed to the classic observer pattern implementation, there are several: Nsnotificationcenter, KVO, delegate, etc.
Nsnotificationcenter Use to add monitoring
- (void) Addobserver: (ID) Observer selector: (SEL) aselector name: (NSString *) AName object: (ID) anobject;
Sample
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (dosomething:) name:get_up Object:nil];
Description
The "Nsnotificationcenter defaultcenter" Message Center has only one, and this method obtains a singleton of the class
Addobserver: Adding a listener
Selector: A handler function that indicates that the object was heard by the supervisor
Name: Indicates the names of the messages that the registration cares about
Receiving messages
- (void) Postnotification: (nsnotification *) notification;
- (void) Postnotificationname: (NSString *) AName object: (ID) anobject;
- (void) Postnotificationname: (NSString ) AName object: (ID) anobject userInfo: (nsdictionary ) auserinfo;
Sample
[[Nsnotificationcenter Defaultcenter] postnotificationname:get_up object:[nsnumber NumberWithInteger:self.time]];
Analysis:
GET_UP: Name of the push message
Object: objects that send messages
UserInfo: Send message to carry information
Delete Monitoring
- (void) Removeobserver: (ID) observer;
- (void) Removeobserver: (ID) Observer name: (NSString *) AName object: (ID) anobject;
Basic concepts of the use of KVO
KVO, Key-value Observing, provides a mechanism for the object to receive notification when the properties of the specified object have been modified. This method differs greatly from Nsnotification, which does not require a notificationcenter to provide notification of changes for all observers, but instead, when a change occurs, the notification is sent directly to the observer, and NSObject implements this method for us.
With this method we can observe the object, which is a one-to-many relationship.
Registered Observer
You must first send a
addobserver:selfforkeypath:@ "Happynum" options:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold
CONTEXT:NIL] Message to the Observer, which transmits the critical path of the observed object and the property to be observed, parameter analysis:
-(void) Addobserver: (NSObject ) Observer Forkeypath: (NSString ) keypath options: (nskeyvalueobservingoptions) Options context: (void *) context;
Observer: Indicates who the observer is, usually self
KeyPath: Indicates which object to observe
Options:nskeyvalueobservingoptionnew means observing the new value, nskeyvalueobservingoptionold means observing the old value
Context: Represents a contextual environment
callback method when the Observer object has changed
(void) Observevalueforkeypath: (NSString *) keypath
Ofobject: (ID) object
Change: (nsdictionary *) change
Context: (void *) Context {
NSLog (@ "%@", change);
}
Parametric analysis:
Change: Indicates the value returned when the changes occurred
KeyPath: Represents the Observed object
Contenst: Indicates context
Remove Observer identity
Send an observer object and path to remove the observer, which is typically placed in the Dealloc:
[self.childremoveobserver:selfforkeypath:@ "Happynum" context:nil];
Code implementation
Child. h#import <Foundation/Foundation.h> @interface Child: nsobject @property(nonatomic,Assign)NsintegerHappynum;@endChild. M#import "Child.h" @implementation Child-(ID) init{ Self=[SuperINIT];if( Self) { Self. Happynum= -; [Nstimer Scheduledtimerwithtimeinterval:1Target SelfSelector@selector(Decrease:) UserInfo:NilRepeatsYES]; }return Self;} -(void) Decrease: (nstimer*) timer{//NSLog (@ "The Happynum is%ld", self.happynum);_happynum--;//[self setvalue:[nsnumber numberwithinteger:_happynum] forkey:@ "Happynum"];}@endNurse. h#import <Foundation/Foundation.h> #import "Child.h" @interface nurse : nsobject @property(nonatomic, retain) Child *child;-(ID) Initwithchild: (child*) child;@endNurse. M:#import "Nurse.h" @implementation nurse -(ID) Initwithchild: (child*) child{ Self=[SuperINIT];if( Self) { Self. Child= child; [ Self. ChildAddobserver: Selfforkeypath:@"Happynum"Options:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold Context:Nil]; }return Self;} - (void) Observevalueforkeypath: (NSString*) KeyPath Ofobject: (ID) Object change: (nsdictionary*) Change Context: (void*) Context {NSLog(@"%@", change);} -(void) dealloc{[ Self. ChildRemoveobserver: Selfforkeypath:@"Happynum"ContextNil]; [_child release]; [SuperDealloc];}@endMain. M:#import <Foundation/Foundation.h> #import "Child.h" #import "Nurse.h" intMainintargcConst Char* argv[]) {@autoreleasepool {child *childobj=[[[child alloc] init] autorelease]; Nurse *nurseobj=[[[nurse Alloc] initwithchild:childobj] autorelease]; [[Nsrunloop Currentrunloop] run];//[nurseobj release];}return 0;}
iOS common design Patterns-observer design Patterns