命令

iOS设计模式-组合

原理图

Command.png

说明

在软件系统中, "行为请求者"与"行为实现者"通常呈现一种"紧耦合". 但在某些场合, 不如要对行为进行"记录, 撤销\重做\事务"等处理, 这种无法抵御变化的紧耦合是不合适的. 在这种情况下, 如何将"行为请求者"与"行为实现者"解耦, 将一组行为抽象为对象, 实现二者之间的松耦合, 这就是命令模式.

模式结构

  • Command

    定义命令的接口, 声明执行的方法.

  • ConcreteCommand

    命令接口实现对象, 是"虚"的实现; 通常会持有接收者, 并调用接收者的功能来完成命令要执行的操作.

  • Invoker

    要求命令对象执行请求, 通常会持有命令对象, 可以持有很多的命令对象. 这个是客户端真正触发命令并要求命令执行相应操作的地方, 也就是说相当于使用命令对象的入口.

  • Client

    创建具体的命令对象, 并且设置命令对象的接收者. 注意这个不是我们常规意义上的客户端, 而是在组装命令对象和接收者, 把这个Client称为装配者会更好理解, 因为真正使用命令的客户端是从Invoker来触发执行.

模式分析

  1. 命令模式的本质是对命令进行封装, 将发出命令的责任和执行命令的责任分隔开.
  2. 每一个命令都是一个操作: 请求的一方发出请求, 要求执行一个操作; 接收的一方收到请求, 并执行操作.
  3. 命令模式允许请求的一方和接收的一方独立开来, 使得请求的一方不必知道接收的一方的接口, 更不必知道请求是怎样被接收, 以及操作是否被执行, 何时被执行, 以及是怎么执行.
  4. 命令模式使请求本身成为一个对象, 这个对象和其他对象一样可以被存储和传递.
  5. 命令模式的关键在于引入抽象命令接口, 且发送者针对抽象命令接口编程, 只有实现了抽象命令接口的具体命令才能与接收者相关联.

代码实现

Client

//
//  ViewController.m
//  Command
//
//  Created by mye on 2019/3/29.
//  Copyright © 2019 mye. All rights reserved.
//

#import "ViewController.h"
#import "Light.h"
#import "SwitchOnCommand.h"
#import "SwitchOffCommand.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // client
    
    // receiver
    Light *light = [[Light alloc] init];
    // concreteCommand
    SwitchOnCommand *turnOn = [[SwitchOnCommand alloc] initWithLight:light];
    SwitchOffCommand *turnOff = [[SwitchOffCommand alloc] initWithLight:light];
    // Invoker command
    [turnOn execute];
    [turnOff execute];
}


@end

CommandProtocol

//
//  CommandProtocol.h
//  Command
//
//  Created by mye on 2019/3/29.
//  Copyright © 2019 mye. All rights reserved.
//

#import 

NS_ASSUME_NONNULL_BEGIN

@protocol CommandProtocol 

- (void)execute;

@end

NS_ASSUME_NONNULL_END

ConcreteCommand

//
//  SwitchOnCommand.h
//  Command
//
//  Created by mye on 2019/3/29.
//  Copyright © 2019 mye. All rights reserved.
//

#import 
#import "Light.h"
#import "CommandProtocol.h"

NS_ASSUME_NONNULL_BEGIN

@interface SwitchOnCommand : NSObject 

- (instancetype)initWithLight:(Light *)light;

@end

NS_ASSUME_NONNULL_END

//
//  SwitchOnCommand.m
//  Command
//
//  Created by mye on 2019/3/29.
//  Copyright © 2019 mye. All rights reserved.
//

#import "SwitchOnCommand.h"

@interface SwitchOnCommand()
{
    Light *light_;
}

@end

@implementation SwitchOnCommand

- (instancetype)initWithLight:(Light *)light {
    self = [super init];
    if (self) {
        light_ = light;
    }
    return self;
}

#pragma mark CommandProtocol method
- (void)execute {
    [light_ turnOff];
}

@end

//
//  SwitchOffCommand.h
//  Command
//
//  Created by mye on 2019/3/29.
//  Copyright © 2019 mye. All rights reserved.
//

#import 
#import "Light.h"
#import "CommandProtocol.h"

NS_ASSUME_NONNULL_BEGIN

@interface SwitchOffCommand : NSObject 

- (instancetype)initWithLight:(Light *)light;

@end

NS_ASSUME_NONNULL_END

//
//  SwitchOffCommand.m
//  Command
//
//  Created by mye on 2019/3/29.
//  Copyright © 2019 mye. All rights reserved.
//

#import "SwitchOffCommand.h"

@interface SwitchOffCommand()
{
    Light *light_;
}

@end

@implementation SwitchOffCommand

- (instancetype)initWithLight:(Light *)light {
    self = [super init];
    if (self) {
        light_ = light;
    }
    return self;
}

#pragma mark CommandProtocol method
- (void)execute {
    [light_ turnOn];
}

@end

receiver

//
//  Light.h
//  Command
//
//  Created by mye on 2019/3/29.
//  Copyright © 2019 mye. All rights reserved.
//

#import 

NS_ASSUME_NONNULL_BEGIN

@interface Light : NSObject

/**
 开灯
 */
- (void)turnOn;

/**
 关灯
 */
- (void)turnOff;

@end

NS_ASSUME_NONNULL_END

//
//  Light.m
//  Command
//
//  Created by mye on 2019/3/29.
//  Copyright © 2019 mye. All rights reserved.
//

#import "Light.h"

@implementation Light

- (void)turnOff {
    NSLog(@"关灯");
}

- (void)turnOn {
    NSLog(@"开灯");
}

@end

你可能感兴趣的:(命令)