The Observer pattern is also known as the Publish/subscribe pattern, and 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. in the Observer pattern, a target object manages all the observer objects that are dependent on it, and proactively notifies when its own state changes. The most obvious observer pattern in iOS development is the KVO key value pair observation, if for KVC and KVO is not very familiar with, you can refer to my previous blog iOS development-KVC and KVO understanding.
About the design patterns on the Internet, reference is readily available, simple use of OC to implement the observer pattern, we give a practical example of work, headhunting and Programmer's story, Simple is a headhunter has a number of candidates in the information, once the company has a new job opportunities, will inform all candidates, Based on the above information, we can easily implement the observer pattern.
Subject Object base class:
@interface subject:nsobject-(void) Registerobserver: (ID) observer;-(void) Deleteobserver: (ID) observer;-(void) Notifyobservers; @end
@interface Subject () @property (strong,nonatomic) nsmutablearray *observers; @end @implementation subject-(void) Registerobserver: (ID) observer{ [Self.observers addobject:observer];} -(void) Deleteobserver: (ID) observer{ [Self.observers removeobject:observer];} -(void) notifyobservers{for (Nsinteger i=0; i<[self.observers count]; i++) { Observer *observer= ( Observer *) [self.observers objectatindex:i]; [Observer Updatestate:self Observer:observer]; } Getter and setter-(Nsmutablearray *) observers{ if (!_observers) { _observers=[[nsmutablearray Alloc]init]; } return _observers;} @end
Headhunting (Hunter) implements the base class:
@implementation hunter-(void) UpdateInfo: (NSString *) personname{ self.personname=personname; [Self notifyobservers];} @end
Observer (Observer) base class:
@interface observer:nsobject-(void) Updatestate: (ID) Subject Observer: (ID) Observer; @end
Programmer (Programmer) Definition:
@interface programmer:observer@property (strong,nonatomic) nsstring *programmername;-(instancetype) Initwithname: (NSString *) name; @end
@implementation programmer-(Instancetype) Initwithname: (NSString *) name{ self=[super init]; if (self) { self.programmername=name; } return self;} Original address: http://www.cnblogs.com/xiaofeixiang/-(void) Updatestate: (ID) Subject Observer: (ID) observer{ NSLog (@ " Guys, XX to recruit, interested hurriedly contact me "); Hunter *hunter= (Hunter *) subject; Programmer *programmer= (Programmer *) observer; NSLog (@ "%@---%@", hunter,programmer);} @end
To test the Observer effect:
Hunter *hunter=[[hunter Alloc]init]; Programmer *programmer=[[programmer alloc]initwithname:@ "iOS development Engineer"]; NSLog (@ "blog Park-flyelephant"); [Hunter Registerobserver:programmer]; [Hunter updateinfo:@ "original address: http://www.cnblogs.com/xiaofeixiang/"]; [Hunter Deleteobserver:programmer]; [Hunter updateinfo:@ "iOS technology: 228407086"];
Test everything OK, if there is inappropriate, welcome to discuss ~
iOS Development-Viewer mode