iOS设计模式 (十一)命令模式

概念

命令模式:提供了一种封装方法调用细节的机制,基于这种机制我们可以实现延迟方法调用或者替换掉用该方法的组件。

UML

命令模式.png

UML图分析

  • Invoker 和 Command 组合关系,concreteCommand类实现Command的协议
  • Recevier和Client单向关联,传递Receiver的实例在concreteCommand类调Receiver的方法。

需求

命令模式.png

  • 降低亮度,增加亮度 ,回退一步三个按钮,降低,增加亮度定义view的backgroundColor,回退一步按照降低,增加按钮的顺序一一返回 .
具体实现
  • CommandProtocol 协议类
#import 

@protocol CommandProtocol 

//具体执行
- (void)execute;
//回退一步
- (void)backExecute;
@end
  • Receiver
#import 

@interface Receiver : NSObject
{
    CGFloat _white;
    CGFloat _alpha;
}

// 接受者view
@property (nonatomic, strong)UIView *receiverView;

//变暗的接口
- (void)mackDarker:(CGFloat)parameter;

//变亮的接口
- (void)macLighter:(CGFloat)pamameter;

@end

#import "Receiver.h"

@implementation Receiver

- (void)setReceiverView:(UIView *)receiverView
{
    _receiverView = receiverView;
    UIColor *color = _receiverView.backgroundColor;
    [color getWhite:&_white alpha:&_alpha];
}

- (void)mackDarker:(CGFloat)parameter
{
    _white -= parameter;
    //设置取值范围
    _white = MAX(0, _white);
    
    //设置背景颜色
    _receiverView.backgroundColor = [UIColor colorWithWhite:_white alpha:_alpha];
}

- (void)macLighter:(CGFloat)pamameter
{
    _white += pamameter;
    //设置取值范围
    _white = MIN(1, _white);
    
    //设置背景颜色
    _receiverView.backgroundColor = [UIColor colorWithWhite:_white alpha:_alpha];
}
@end
  • DarkCommand (concreteCommand类)
#import 
#import "Receiver.h"
#import "CommandProtocol.h"

@interface DarkCommand : NSObject  

- (instancetype)initWithDark:(Receiver *)receiver parameter:(CGFloat)parameter;

@end

#import "DarkCommand.h"

@interface DarkCommand ()
@property (nonatomic, strong)Receiver *receiver;
@property (nonatomic, assign)CGFloat  parameter;
@end

@implementation DarkCommand

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

- (void)execute
{
   [self.receiver mackDarker:self.parameter];
}

- (void)backExecute
{
   [self.receiver macLighter:self.parameter];
}
@end

  • LighterCommand (concreteCommand类)
#import 
#import "Receiver.h"
#import "CommandProtocol.h"

@interface LighterCommand : NSObject 

- (instancetype)initWithLighter:(Receiver *)receiver pamameter:(CGFloat)parameter;

@end

#import "LighterCommand.h"

@interface LighterCommand ()

@property (nonatomic, strong)Receiver *receiver;
@property (nonatomic, assign)CGFloat  parameter;
@end

@implementation LighterCommand


- (instancetype)initWithLighter:(Receiver *)receiver pamameter:(CGFloat)parameter
{
   self = [super init];
   if (self) {
       self.receiver = receiver;
       self.parameter = parameter;
   }
   return self;
}

- (void)execute
{
  [self.receiver macLighter:self.parameter];
}

- (void)backExecute
{
   [self.receiver mackDarker:self.parameter];
}

@end
  • Invoker
#import 
#import "CommandProtocol.h"

@interface Invoker : NSObject

+ (instancetype)shareInstance;

- (void)geBack;

- (void)addAndExcute:(id)command;

@end
#import "Invoker.h"

@interface Invoker ()

@property (nonatomic, strong)NSMutableArray *array;

@end

@implementation Invoker

+ (instancetype)shareInstance
{
    static Invoker *shareInstance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        shareInstance = [[self alloc] init];
        shareInstance.array = [NSMutableArray array];
        
    });
    
    return shareInstance;
}

- (void)geBack
{
    //1.获取数组中最后一个操作
    id  commandProtocol = self.array.lastObject;
    
    //2.操作调用,撤销步聚
    [commandProtocol backExecute];
    
    //3.删除最后的操作
    [self.array removeLastObject];
    
    
        NSLog(@"removeself.array--------:%ld",self.array.count);
    
}

- (void)addAndExcute:(id)command
{
    //1.把操作添加到数组
    [self.array addObject:command];
    
    //2.让操作调用实现的协议方法
    [command execute];
    
    NSLog(@"addself.array--------:%ld",self.array.count);
}

@end
  • ViewController
#import "ViewController.h"
#import "Receiver.h"
#import "DarkCommand.h"
#import "LighterCommand.h"
#import "Invoker.h"

@interface ViewController ()

@property (nonatomic, strong)Receiver *recevier;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.recevier = [[Receiver alloc] init];
    self.recevier.receiverView = self.view;
}

- (IBAction)btnAction:(UIButton *)sender {
    
    if (sender.tag == 10) {
        DarkCommand *darkCommand = [[DarkCommand alloc] initWithDark:self.recevier parameter:0.1];
        [[Invoker shareInstance] addAndExcute:darkCommand];
    }
    else if (sender.tag == 20){
      
        LighterCommand *lightCommand = [[LighterCommand alloc] initWithLighter:self.recevier pamameter:0.1];
       [[Invoker shareInstance] addAndExcute:lightCommand];
    }
    else {
        [[Invoker shareInstance] geBack];
    }
}
@end
Demo地址:https://github.com/defuliu/CommandPattern.git

你可能感兴趣的:(iOS设计模式 (十一)命令模式)