How do I write a message processing center that is closed for modification in extended development?
The push message processing that was performed some time ago always enters different message processing processes based on different message types. The overall framework of the message processing process is similar, but a very small piece of code is different. Therefore, it is easy to think of using the template method mode to write the Framework process for the base class. The derived class is responsible for the specific implementation.
Three classes are required:
Lppushdispatchcenter: Message Processing Center class
Lppushdispatch: Message Processing Base Class
Lpdetailpushdispatch (many of these derived classes): derived class of Message Processing
All messages first enter the message processing center, which is implemented as a singleton class for external convenience. It stores the ing between the message type and the corresponding derived class object.
When handlemessage is required, it finds a specific derived class object for Message Processing Based on the message type, and then creates the corresponding object for specific processing. How to save the ing between message types and processing objects? At the beginning, the implementation was relatively simple. Directly switch case and return different class objects based on type. Now I have carefully read the loading process of the Class Object and found that the load method will be called when the class object is loaded, so I think the load method in the derived class can tell myself the type, and bind its type and class to the message center.
Message Center methods:
# Pragma mark Singleton method // Singleton + (lppushdispatchcenter *) specify instance {static dispatch_once_t oncetoken; static lppushdispatchcenter * pushcenter = nil; dispatch_once, ^ {pushcenter = [[self alloc] init] ;}); return pushcenter ;}- (ID) Init {If (Self = [Super init]) {_ typeandclassdic = [[nsmutabledictionary alloc] init];} return self;}-(void) registerpushtype :( nsinteger) type andclass :( class) typeclass {If (_ typeandclassdic [@ (type)]) {nslog (@ "error: type (% LD) has same class", type); return ;} _ typeandclassdic [@ (type)] = typeclass;}-(void) handlemessage :( lppushmessage *) MSG {nsinteger type = MSG. type; Class typeclass = _ typeandclassdic [@ (type)]; If (typeclass) {lppushdispatch * dispatch = [[typeclass alloc] init]; dispatch. pushmessage = MSG; [dispatch handlepushmessage];} else {nslog (@ "handlemessagewithtype: unknown type (% LD)", type );}}
The message center has only one external method, handlemessage, which is passed in MSG. Based on the Type type of MSG, find the corresponding class object in _ typeandclassdic, create the corresponding object, and assign a value to the pushmessage parameter, call the handlepushmessage method of lppushdispatch.
Lppushdispatch base framework:
+ (Void) settype :( nsinteger) type {[[lppushdispatchcenter reply instance] registerpushtype: Type andclass: [self class];}/** process push messages */-(void) handlepushmessage {bool isneedhandle = [self beforehandlemessage]; If (isneedhandle) {[self handle]; [self afterhandlemessage] ;}// preprocessing-(bool) beforehandlemessage {return yes ;} // processing-(void) handlingpushmessage {[self openmessage];} // post-processing-(void) afterhandlemessage {}
It provides a settype method for the derived class to call. The derived class calls settype to register its own type and class to the lppushdispatchcenter. The real solution is handlingpushmessage, which calls the openmessage method of the derived class.
Let's look at the derived class lpdetailpushdispatch:
+(void)load{ DLog(@""); self.type = 0;}- (void)openMessage { DLog(@"%@", self.pushMessage);}It uses self. type to call the settype method of the base class and registers itself to the dictionary of the message center class. The openmessage method also needs to be implemented, which performs different processing based on different self. pushmessage.
Summary:
Open to extensions. You can create a new derived class. Disable modification and do not need to change the base class or message center.
Better code packaging. I think it is a good idea. You are welcome to exchange ideas.
Now let's write the message center here. There is a small record for your own small thoughts.