- Command pattern is a behavior design pattern that transforms a request into a single object that contains all the information associated with the request.
- This transformation allows you to parameterize methods based on different requests, defer or queue requests, and implement undoable operations.
- For example, restaurant cooks, waiters and customers place orders
- User 1 orders
- 1. Order lamb kebabs
- 2. Order duck skewers
- User 2 orders
- 1. Order chicken skewers
- 2. Order duck skewers
- 3. Cancel the duck skewers
- The waiter began to add orders, and finally deleted one order and added it to his queue and gave it to the chef
- The chef receives the order list and executes the order
- User 1 orders
#import <Foundation/Foundation.h> #import "HCDOrder.h" #import "HCDWorker.h" @interface HCDOrder : NSObject @property(nonatomic,copy)NSString *orderString; -(instancetype)initWithOrderString:(NSString *)orderString; // Execute the command -(void)executeOrder; @end #import "HCDOrder.h" @implementation HCDOrder -(instancetype)initWithOrderString:(NSString *)orderString{ self = [super init]; if (self) { _orderString = orderString; } return self; } @endCopy the code
#import "HCDOrder.h" @interface HCDMuttonOrder : HCDOrder @end #import "hcdmuttonorder. h" @implementation HCDMuttonOrder -(void)executeOrder{NSLog(@" "); [[HCDWorker sharedWorker] doMuttonWork:self.orderString]; } @end #import "HCDOrder.h" @interface HCDChickenOrder : HCDChickenOrder @end #import "hcdchickenorder. h" @implementation HCDChickenOrder -(void)executeOrder{NSLog(@" chicken "); [[HCDWorker sharedWorker] doChickenWork:self.orderString]; } @end #import "HCDOrder.h" @interface HCDDuckOrder : HCDDuckOrder @end #import "hcdDuckorder. h" @implementation HCDDuckOrder -(void)executeOrder{NSLog(@" duck "); [[HCDWorker sharedWorker] doChickenWork:self.orderString]; } @endCopy the code
#import <Foundation/Foundation.h> @interface HCDWorker : NSObject +(instancetype)sharedWorker; -(void)doMuttonWork:(NSString *)work; -(void)doChickenWork:(NSString *)work; -(void)doDuckWork:(NSString *)work; @end #import "HCDWorker.h" @implementation HCDWorker +(instancetype)sharedWorker{ static dispatch_once_t once; static HCDWorker *sharedWorker = nil; dispatch_once(&once, ^{ sharedWorker = [[HCDWorker alloc]init]; }); return sharedWorker; } -(void)doMuttonWork:(NSString *)work{NSLog(@" cook :%@",work); } -(void)doChickenWork:(NSString *)work{NSLog(@" cook :%@",work); } - (void) doDuckWork: (nsstrings *) work {NSLog (@ "cook duck meat: % @", the work); } @endCopy the code
#import <Foundation/Foundation.h> #import "HCDOrder.h" #import "HCDMuttonOrder.h" #import "HCDChickenOrder.h" @interface HCDWaiter : NSObject -(void)addOrder:(HCDOrder *)order; -(void)deleteOrder:(HCDOrder *)order; -(void)notifyOrder; @property(nonatomic,strong)NSMutableArray *orderList; @end #import "HCDWaiter. @end @implementation HCDWaiter -(instancetype)init{ self = [super init]; if (self) { _orderList = [NSMutableArray array]; } return self; } -(void)addOrder:(HCDOrder *)order{NSLog(@" addOrder "); [self.orderList addObject:order]; } -(void)deleteOrder (HCDOrder *)order{NSLog(@" cancel order "); [self.orderList removeObject:order]; } / * command receiving end, began to execute the command * / - (void) notifyOrder {NSLog (@ "= = = = begin to execute the Order = = ="); for (HCDOrder *order in self.orderList) { [order executeOrder]; } } @endCopy the code
#import <Foundation/Foundation.h>
@class HCDOrder,HCDMuttonOrder,HCDChickenOrder;
typedef NS_ENUM(NSUInteger,orderType){
orderTypeMutton,
orderTypeChicken,
orderTypeDuck
};
@interface HCDCustomr : NSObject
-(HCDOrder *)pushOrderWithString:(NSString *)string type:(orderType)type;
@end
#import "HCDCustomr.h"
#import "HCDMuttonOrder.h"
#import "HCDChickenOrder.h"
#import "HCDDuckOrder.h"
@implementation HCDCustomr
-(HCDOrder *)pushOrderWithString:(NSString *)string type:(orderType)type{
HCDOrder *order = nil;
switch (type) {
case orderTypeMutton:
order = [[HCDMuttonOrder alloc]initWithOrderString:string];
break;
case orderTypeChicken:
order = [[HCDChickenOrder alloc]initWithOrderString:string];
break;
case orderTypeDuck:
order = [[HCDDuckOrder alloc]initWithOrderString:string];
break;
}
return order;
}
@end
Copy the code
- call
#import "ViewController.h" #import "HCDWaiter.h" #import "HCDCustomr.h" #import "HCDWorker.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //waiter is used to receive various types of orders. Waiter is the recipient of the request. // Accept different orders from different customers and store them in waiter. Type represents different types of orders. HCDWaiter *waiter = [[HCDWaiter alloc]init]; HCDCustomr *customer = [[HCDCustomr alloc]init]; HCDOrder *customerOrder1 = [customer pushOrderWithString:@" customer wants ten "type:orderTypeMutton]; HCDOrder *customerOrder2 = [customer pushOrderWithString:@" orderTypeDuck "type:orderTypeDuck]; [waiter addOrder:customerOrder1]; [waiter addOrder:customerOrder2]; HCDCustomr *customer1 = [[HCDCustomr alloc]init]; HCDOrder *customer1Order1 = [customer1 pushOrderWithString:@" CustomTypechicken "type:orderTypeChicken]; HCDOrder *customer1Order1 = [Customer1 pushOrderWithString:@" CustomTypechicken" type:orderTypeChicken] HCDOrder *customer1Order2 = [customer1 pushOrderWithString:@" customer 2 wants 20 ducks "type:orderTypeDuck]; [waiter addOrder:customer1Order1]; [waiter addOrder:customer1Order2]; / / customer to cancel the Order [waiter deleteOrder: customer1Order2]; //waiter sends an order with a single column HCDWorker behind it as the implementer for the specific order. After receiving the command, start sending the command. [waiter notifyOrder]; } @endCopy the code