阅读 213

ios-命令模式

  • 命令模式是一种行为设计模式,他可将请求转换成一个包含与请求相关的所有信息的独立对象。
  • 该转换让你能根据不同的请求将方法参数化,延迟请求执行或将其放入队列中,且能实现可撤销操作。
  • 举例饭店厨师、服务员、客户下单
    • 用户1点菜
      • 1.下单羊肉串
      • 2.下单鸭肉串
    • 用户2点菜
      • 1.下单鸡肉串
      • 2.下单鸭肉串
      • 3.取消鸭肉串
    • 服务员就开始添加命令,并在最后删除了一条命令加入到自己的队列中,交给了厨师
    • 厨师收到订单列表开始执行命令

image.png

#import <Foundation/Foundation.h>
#import "HCDOrder.h"
#import "HCDWorker.h"

@interface HCDOrder : NSObject

@property(nonatomic,copy)NSString *orderString;

-(instancetype)initWithOrderString:(NSString *)orderString;
//执行命令
-(void)executeOrder;
@end

#import "HCDOrder.h"

@implementation HCDOrder
-(instancetype)initWithOrderString:(NSString *)orderString{
    self = [super init];
    if (self) {
        _orderString = orderString;
    }
    return self;
}
@end

复制代码
#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 : HCDOrder

@end

#import "HCDChickenOrder.h"

@implementation HCDChickenOrder

-(void)executeOrder{
    NSLog(@"烤鸡");
    [[HCDWorker sharedWorker] doChickenWork:self.orderString];
}
@end

#import "HCDOrder.h"

@interface HCDDuckOrder : HCDOrder

@end

#import "HCDDuckOrder.h"

@implementation HCDDuckOrder

-(void)executeOrder{
    NSLog(@"烤鸭");
    [[HCDWorker sharedWorker] doChickenWork:self.orderString];
}
@end
复制代码
#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(@"厨师考羊肉:%@",work);
}
-(void)doChickenWork:(NSString *)work{
    NSLog(@"厨师考鸡肉:%@",work);
}

-(void)doDuckWork:(NSString *)work{
    NSLog(@"厨师烤鸭肉:%@",work);
}

@end

复制代码
#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;
@end

#import "HCDWaiter.h"

@interface HCDWaiter ()
//用于存储命令列表
@property(nonatomic,strong)NSMutableArray *orderList;
@end

@implementation HCDWaiter

-(instancetype)init{
    self = [super init];
    if (self) {
        _orderList = [NSMutableArray array];
    }
    return self;
}
-(void)addOrder:(HCDOrder *)order{
    NSLog(@"添加Order");
    [self.orderList addObject:order];
}
-(void)deleteOrder:(HCDOrder *)order{
    NSLog(@"取消Order");
    [self.orderList removeObject:order];
}
/*
 命令接收完毕,开始执行命令
 */
-(void)notifyOrder{
    NSLog(@"====开始执行Order===");
    for (HCDOrder *order in self.orderList) {
        [order executeOrder];
    }
}
@end

复制代码
#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

复制代码
  • 调用
#import "ViewController.h"
#import "HCDWaiter.h"
#import "HCDCustomr.h"
#import "HCDWorker.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //waiter用于接收各种类型的order。waiter是请求接收者。
    //接收不同customer产生的不同order,并且都存入waiter这个接受者中,type表示不同类型的order。
    HCDWaiter *waiter = [[HCDWaiter alloc]init];
    //顾客一
    HCDCustomr *customer = [[HCDCustomr alloc]init];
    HCDOrder *customerOrder1 = [customer pushOrderWithString:@"顾客一要十串羊肉" type:orderTypeMutton];
     HCDOrder *customerOrder2 = [customer pushOrderWithString:@"顾客一要十串鸭肉" type:orderTypeDuck];
    [waiter addOrder:customerOrder1];
    [waiter addOrder:customerOrder2];
    //顾客二
    HCDCustomr *customer1 = [[HCDCustomr alloc]init];
    HCDOrder *customer1Order1 = [customer1 pushOrderWithString:@"顾客二要二十串鸡肉" type:orderTypeChicken];
        HCDOrder *customer1Order2 = [customer1 pushOrderWithString:@"顾客二要二十串鸭肉" type:orderTypeDuck];
    [waiter addOrder:customer1Order1];
    [waiter addOrder:customer1Order2];
    //顾客取消Order
    [waiter deleteOrder:customer1Order2];
    //waiter发送order,背后有一个HCDWorker这个单列作为行为实现者来处理具体的order。命令接收完毕,开始发送命令。
    [waiter notifyOrder];
    
}


@end

复制代码
文章分类
iOS
文章标签