二十四、 命令模式

1. 何為模板方法模式

在軟件系統中,“行為請求者”與“行為實現者”通常呈現一種“緊耦合”,但在某些場合,比如要對行為進行“記錄、撤銷、事務”等處理,這種無法抵御變化的耦合是不合適的。在這種情況下,將一組行為抽象為對象,實現二者之間的松耦合,這就是命令模式(Command Pattern)。命令模式的UML圖見圖1-1:

圖1-1 命令模式

下面通過改變一個視圖的明暗程度來體會命令模式的優缺點。

2. 非命令模式

(1)首先在ViewController添加三個按鈕,并設置好相關屬性和監聽點擊事件:

typedef enum : NSUInteger {
    hAddButtonTag = 0x11,
    hDelButtonTag,
    hRolButtonTag,
} ViewControllerEnumValue;

@interface ViewController ()
/** 接受者,執行任務者 */
@property (nonatomic,strong)Receiver *receiver;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 畫出三個按鈕
    // 調亮按鈕 +
    UIButton* addBtn = [self addButtonWithTitle:@"+"
                                      withFrame:CGRectMake(30, 30, 40, 40)
                                     withAction:@selector(buttonsEvent:)
                                        withTag:hAddButtonTag];
    [self.view addSubview:addBtn];
    // 調暗按鈕 -
    UIButton* delBtn = [self addButtonWithTitle:@"-"
                                      withFrame:CGRectMake(100, 30, 40, 40)
                                     withAction:@selector(buttonsEvent:)
                                        withTag:hDelButtonTag];
    [self.view addSubview:delBtn];
    // 撤銷操作按鈕
    UIButton* rolBtn = [self addButtonWithTitle:@"RoolBack"
                                      withFrame:CGRectMake(170, 30, 100, 40)
                                     withAction:@selector(buttonsEvent:)
                                        withTag:hRolButtonTag];
    [self.view addSubview:rolBtn];
    
    self.receiver = [[Receiver alloc] init];
    [self.receiver setClientView:self.view];
}


-(void)buttonsEvent:(UIButton*)btn{
    if (btn.tag == hAddButtonTag) {
        
        [self.receiver makeViewLighter:0.1f];
        
    }else if (btn.tag == hDelButtonTag){
        
        [self.receiver makeViewDarker:0.1f];
        
    }else if (btn.tag == hRolButtonTag){
        
    }
}

#pragma mark - 添加同類按鈕的方法
// 增加相同按鈕的方法相同,所以抽離出來
-(UIButton*)addButtonWithTitle:(NSString*)title withFrame:(CGRect)frame withAction:(SEL)sel withTag:(ViewControllerEnumValue)tag{
    UIButton* btn = [[UIButton alloc] initWithFrame:frame];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn setTitle:@"??" forState:UIControlStateHighlighted];
    btn.layer.borderWidth = 1.0f;
    [btn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
    [btn setTag:tag];
    return btn;
}
@end

(2)然后創建任務執行者對象Receiver類,創建的實例對象負責執行調整View背景明亮暗度。

// Receiver任務執行者,有服務的對象,那么也有操作服務對象的具體行為

// 這里根據業務邏輯任務就是改變client的明亮程度

@interface Receiver : NSObject

/** 服務的對象 */
@property (nonatomic,strong)UIView *clientView;

// 增加亮度的行為
-(void)makeViewLighter:(CGFloat)quantity;
// 降低亮度的行為
-(void)makeViewDarker:(CGFloat)quantity;

@end
@interface Receiver ()
{
    CGFloat _hud;           // 色調
    CGFloat _saturation;    // 飽和度
    CGFloat _brightness;    // 亮度
    CGFloat _alpha;         // 透明度 alpha=1表示完全不透明
}
@end

@implementation Receiver

// 需要重寫clientView的set方法,因為Receiver最開始要做的就是set獲取UIView對象
// 在set對象的同時要獲取當前client的狀態,獲取當前狀態需要變量存儲狀態值
-(void)setClientView:(UIView *)clientView
{
    _clientView = clientView;
    
    UIColor *color = clientView.backgroundColor;
    [color getHue:&_hud saturation:&_saturation brightness:&_brightness alpha:&_alpha];
}

-(void)makeViewLighter:(CGFloat)quantity{
    _brightness += quantity;
    self.clientView.backgroundColor = [[UIColor alloc] initWithHue:_hud
                                                        saturation:_saturation
                                                        brightness:_brightness
                                                             alpha:_alpha];
}

-(void)makeViewDarker:(CGFloat)quantity{
    _brightness -= quantity;
    self.clientView.backgroundColor = [[UIColor alloc] initWithHue:_hud
                                                        saturation:_saturation
                                                        brightness:_brightness
                                                             alpha:_alpha];
    
}

@end

效果如下:

效果.gif

其實到這里,Receive對象相當于ViewController的代理,代理完成控制屬于Viewcontroller管理和控制的View 的背景明亮暗度調整。只不過這個代理不是那么抽象而且也不是遵循了某個協議的。是具體而直接完成邏輯業務的。這個簡單的模式,在項目或者不考慮拓展性或者 某個模塊功能固定了,可以使用這個模式。

但是有的業務需求會需要記錄存儲和取出執行任務的或者信息傳遞命令的狀態,像這里如果要添加撤銷操作,就需要記錄之前的操作,然后根據之前的操作回 退反過來操作,這時候,命令模式就能比較方便的實現這個邏輯了。命令模式其實就是將發送的命令信息抽象成一個類,然后具體信息就創建具體的類,并通過回調 者添加并執行命令對象的執行命令涉及到的任務方法,同時存儲記錄這個命令,那么這時候因為每次操作都能存儲下來,所以再來設計撤銷操作就很容易了。

3. 命令模式

(1)需要遵守的協議

@protocol InvokerProtocol <NSObject>
/*
 * 這個協議是Invoker調用者要求的協議,希望遵循這個協議的類,實現了必須要實現的兩個方法
 * 因為這兩個方法,在Invoker中一定會被調用
 */
@required

/**
 *  命令的執行
 */
- (void)excute;

/**
 *  撤銷命令
 */
- (void)rollBackExcute;

@end

(2)變亮和變暗的命令

@interface LighterCommand : NSObject <InvokerProtocol>

- (instancetype)initWithReceiver:(Receiver*)receiver withParamter:(CGFloat)paramter;

@end


@interface LighterCommand ()
/** 接受者 */
@property (nonatomic, strong) Receiver *receiver;
/** 數值 */
@property (nonatomic, assign) CGFloat paramter;
@end

@implementation LighterCommand


- (instancetype)initWithReceiver:(Receiver*)receiver withParamter:(CGFloat)paramter
{
    self = [super init];
    if (self) {
        self.receiver = receiver;
        self.paramter = paramter;
    }
    return self;
}

/**
 *  命令的執行 思考一下,命令怎么執行讓任務實現?
 *  
 */
- (void)excute{
    [self.receiver makeViewLighter:self.paramter];
}

/**
 *  撤銷命令
 */
- (void)rollBackExcute{
    [self.receiver makeViewDarker:self.paramter];
}

@end
@interface DarkerCommand : NSObject  <InvokerProtocol>

- (instancetype)initWithReceiver:(Receiver*)receiver withParamter:(CGFloat)paramter;

@end


@interface DarkerCommand ()
/** 接受者 */
@property (nonatomic, strong) Receiver *receiver;
/** 數值 */
@property (nonatomic, assign) CGFloat paramter;
@end

@implementation DarkerCommand


- (instancetype)initWithReceiver:(Receiver*)receiver withParamter:(CGFloat)paramter
{
    self = [super init];
    if (self) {
        self.receiver = receiver;
        self.paramter = paramter;
    }
    return self;
}

/**
 *  命令的執行 思考一下,命令怎么執行讓任務實現?
 *
 */
- (void)excute{
    [self.receiver makeViewDarker:self.paramter];
}

/**
 *  撤銷命令
 */
- (void)rollBackExcute{
    [self.receiver makeViewLighter:self.paramter];
}

@end

(3)命令的管理者

@interface Invoker : NSObject

interfaceSingleton(Invoker);

/**
 *  添加指令操作
 *
 *  @param command 指令
 */
- (void)addExcute:(id<InvokerProtocol>)command;

/**
 *  回退操作
 */
-(void)rollBack;
@end


@interface Invoker ()
/** 存儲指令對象 */
@property (nonatomic,strong)NSMutableArray *commandArray;
@end

@implementation Invoker

implementationSingleton(Invoker);

-(NSMutableArray*)commandArray{
    if (_commandArray == nil) {
        NSLog(@"創建了一次NSMutableArray對象");
        _commandArray = [NSMutableArray array];
    }
    return _commandArray;
}

- (void)addExcute:(id<InvokerProtocol>)command{
    [command excute];
    NSLog(@"開始執行了");
    [self.commandArray addObject:command];
    NSLog(@"執行結束了");
}

-(void)rollBack{
    NSLog(@"撤銷操作");
    [self.commandArray.lastObject rollBackExcute];
    [self.commandArray removeLastObject];
}
@end

(4)Receiver任務執行者,和非命令模式下一樣

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容