iOS design mode-commands
Schematic diagram
Description
The Command object encapsulates information about how the instruction is executed against the target, so the client or caller does not have to know any details of the target, but can still perform any existing operations on him. By encapsulating the request into an object, the client can parameterize it and place it in a queue or log, and can support a revocable operation. The command object binds one or more actions to a specific sink. The command pattern eliminates the binding between the action as an object and the receiver that executes it.
invoker.h// commandpattern//// Created by youxianming on 15/10/17.// Copyright? 2015 Youxianming. All rights reserved.//#import <Foundation/Foundation.h> #import "CommandProtocol.h" @interface invoker:nsobject /** * Single Example * * @return Singleton */+ (instancetype) sharedinstance;/** * Add and Execute * * @param command command */-(void) Addan Dexecute: (id <CommandProtocol>) command; @end
invoker.m// commandpattern//// Created by youxianming on 15/10/17.// Copyright? 2015 Youxianming. All rights reserved.//#import "Invoker.h" @interface Invoker () @property (nonatomic, strong) Nsmutablearray * Commandqueue, @end @implementation invoker+ (instancetype) sharedinstance { static Invoker * Sharedinstancevalue = nil; Static dispatch_once_t oncepredicate; Dispatch_once (&oncepredicate, ^{ sharedinstancevalue = [[Invoker alloc] init]; Sharedinstancevalue.commandqueue = [Nsmutablearray array]; }); return sharedinstancevalue;} -(void) Addandexecute: (id <CommandProtocol>) command { //Add and Execute [Self.commandqueue addobject:command ]; [Command execute];} @end
commandprotocol.h// commandpattern//// Created by youxianming on 15/10/17.// Copyright? 2015 Youxianming. All rights reserved.//#import <Foundation/Foundation.h> @protocol commandprotocol <NSObject> @required/** * execute command */-(void) execute; @end
Details
iOS design mode-commands