命令模式

2019-03-02  本文已影响0人  沫粢泪虞

1.定义

面向协议编程,将所有的功能抽象为协议
第一点:将一个请求  封装成一个对象,从而让用户使用不同的请求将客户端参数化
第二点:对请求排队或者记录请求日志,以及支持撤销操作

2.应用场景

当需要将方法调用包装成一个对象,一延迟方法调用,或者让其他组件在对其内部实现细节不了解的情况下进行调用的时候可以使用命令模式
场景一:应用程序支持撤销和恢复
场景二:记录请求日志,当系统故障这些命令可以重新被执行
场景三:想用对象参数化一个动作以执行操作,并且用不同命令对象来替换回调函数

3.角色划分

自己写框架、看框架,分析每一个类是什么角色
角色一:接收者
角色二:命令接口(协议)
角色三:具体的命令 (任务:遵循协议/命令接口)
角色四:请求者  (任务:执行命令)

4.具体案例

每个角色之间的关系和调用
案例:电脑关机和开机

4.1确定角色:

1.命令接口:Computer_CommandProtocol
2.具体分类->实现类
    两个:开机、关机
    开机:MacStartupCommand 遵循< Computer_CommandProtocol >
    关机:MacShutdownCommand 遵循< Computer_CommandProtocol >
3.接收者->电脑接收命令->MacComputer
4.请求者->invoker

4.2关联角色:

首先:关联请求者 MacInvoker
其次:实现接受者 MacComputer
最后:实现命令角色 MacStartupCommand  MacShutdownCommand

4.3相关代码:

4.3.1 Computer_CommandProtocol

Computer_CommandProtocol.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@protocol Computer_CommandProtocol <NSObject>
//命令接口
- (void)execute;
@end

NS_ASSUME_NONNULL_END

4.3.2 MacStartupCommand

MacStartupCommand.h
#import <Foundation/Foundation.h>
#import "Computer_CommandProtocol.h"
#import "MacComputer.h"

NS_ASSUME_NONNULL_BEGIN
//命令角色特点:持有接收者的引用
@interface MacStartupCommand : NSObject <Computer_CommandProtocol>
- (instancetype)init:(MacComputer *)computer;
@end

NS_ASSUME_NONNULL_END
MacStartupCommand.m
#import "MacStartupCommand.h"

@interface MacStartupCommand ()
@property (strong, nonatomic) MacComputer *computer;
@end

@implementation MacStartupCommand

- (instancetype)init:(MacComputer *)computer
{
    self = [super init];
    if (self) {
        _computer = computer;
    }
    return self;
}

//实现协议
- (void)execute {
    //具体实现
    [_computer startup];
}

@end

4.3.3 MacShutdownCommand

MacShutdownCommand.h

#import <Foundation/Foundation.h>
#import "Computer_CommandProtocol.h"
#import "MacComputer.h"

NS_ASSUME_NONNULL_BEGIN

@interface MacShutdownCommand : NSObject <Computer_CommandProtocol>
- (instancetype)init:(MacComputer *)computer;
@end

NS_ASSUME_NONNULL_END

MacShutdownCommand.m

#import "MacShutdownCommand.h"
@interface MacShutdownCommand ()
@property (strong, nonatomic) MacComputer *computer;
@end

@implementation MacShutdownCommand
- (instancetype)init:(MacComputer *)computer
{
    self = [super init];
    if (self) {
        _computer = computer;
    }
    return self;
}

//实现协议
- (void)execute {
    //具体实现
    [_computer shutdown];
}

@end

4.3.4 MacComputer

MacComputer.h

NS_ASSUME_NONNULL_BEGIN

@interface MacComputer : NSObject
//开机
- (void)startup;
//关机
- (void)shutdown;
@end

NS_ASSUME_NONNULL_END

MacComputer.h

#import "MacComputer.h"
//接收者
@implementation MacComputer
//开机
- (void)startup {
    NSLog(@"开机");
}
//关机
- (void)shutdown {
    NSLog(@"关机");
}

@end

4.3.5 MacInvoker

MacInvoker.h

#import <Foundation/Foundation.h>
#import "MacStartupCommand.h"
#import "MacShutdownCommand.h"

NS_ASSUME_NONNULL_BEGIN
//请求者->执行命令
//持有命令对象的引用
@interface MacInvoker : NSObject
//所有的命令通过客户端来决定(或者通过函数内部决定也可以)
- (instancetype)init:(MacStartupCommand *)startup shutDown:(MacShutdownCommand *)shutDown ;
//开机
- (void)startup;
//关机
- (void)shutdown;
@end

NS_ASSUME_NONNULL_END

MacInvoker.m

#import "MacInvoker.h"

@interface MacInvoker ()
@property (strong, nonatomic) id <Computer_CommandProtocol> startupCommand;
@property (strong, nonatomic) id <Computer_CommandProtocol> shutDownCommand;
//父类引用指向子类的实例对象(面向对象编程)
//@property (strong, nonatomic) MacStartupCommand *startupCommand;
//@property (strong, nonatomic) MacShutdownCommand *shutDownCommand;


@end
//请求者
@implementation MacInvoker
- (instancetype)init:(MacStartupCommand *)startup shutDown:(MacShutdownCommand *)shutDown {
    self = [super init];
    if(self) {
        _startupCommand = startup;
        _shutDownCommand = shutDown;
    }
    return self;
}

- (void)startup {
    //调用命令
    [_startupCommand execute];
}

- (void)shutdown {
    //调用命令
    [_shutDownCommand execute];
}

@end
上一篇 下一篇

猜你喜欢

热点阅读