当前位置: 移动技术网 > IT编程>移动开发>IOS > 实例讲解设计模式中的命令模式在iOS App开发中的运用

实例讲解设计模式中的命令模式在iOS App开发中的运用

2019年07月24日  | 移动技术网IT编程  | 我要评论

黑暗天使巨刃,黑水玄蛇txt,股市春节放假安排2015

命令模式封装一个请求或行为作为一个对象。封装的请求比原的更加灵活,可以在对象之间传递,储存,动态修改,或放入一个队列。

那么让我们简要的说一下命令模式的特点。

  • 它能比较容易地设计一个命令队列;
  • 在需要的情况下,可以较容易地将命令记入日志;
  • 允许接收请求地一方决定是否要否决请求;
  • 可以容易地实现对请求地撤销和重做;
  • 由于加进新地具体命令类不影响其他的类,因此增加新的具体命令类很容易;
  • 把请求一个操作的对象与知道怎么执行一个操作的对象分隔开。

下面给出基本的类结构图:

201632492123555.jpg (448×315)

上面这张图是命令模式的类结构的基本图。其实从这张图中还可以扩展出很多,细节就不说了,给大家留一些想象的空间,呵呵!

还是老规矩,下面给出实例:

objective-c 示例:

command:

复制代码 代码如下:

//
//  nimocommand.h
//  commanddemo
//
 
#import <foundation/foundation.h>
 
@protocol nimocommand <nsobject>
 
- (void)execute;
 
@end

concretecommand:
复制代码 代码如下:

//
//  nimoconcretecommand.h
//  commanddemo
//
#import <foundation/foundation.h>
#import "nimocommand.h"
@class nimoreceiver;
 
@interface nimoconcretecommand : nsobject <nimocommand>
 
@property (nonatomic) nimoreceiver *receiver;
 
- (id)initwithreceiver:(nimoreceiver *)receiver;
 
@end

复制代码 代码如下:

//
//  nimoconcretecommand.m
//  commanddemo
//
 
#import "nimoconcretecommand.h"
#import "nimoreceiver.h"
 
 
@implementation nimoconcretecommand
 
- (void)execute
{
    [_receiver action];
}
 
- (id)initwithreceiver:(nimoreceiver *)receiver
{
    if (self = [super init]) {
        _receiver = receiver;
    }
    
    return self;
}
 
@end

receiver:
复制代码 代码如下:

//
//  nimoreceiver.h
//  commanddemo
//

#import <foundation/foundation.h>
 
@interface nimoreceiver : nsobject
 
- (void)action;
 
@end


复制代码 代码如下:

//
//  nimoreceiver.m
//  commanddemo
//

#import "nimoreceiver.h"
 
@implementation nimoreceiver
 
- (void)action
{
    nslog(@"实际执行");
}
 
@end


invoker:
复制代码 代码如下:

//
//  nimoinvoker.h
//  commanddemo
//
 
#import <foundation/foundation.h>
#import "nimocommand.h"
 
@interface nimoinvoker : nsobject
 
@property (nonatomic, weak) id<nimocommand> command;
 
- (void)executecommand;
 
@end

复制代码 代码如下:

//
//  nimoinvoker.m
//  commanddemo
//
 
#import "nimoinvoker.h"
 
 
@implementation nimoinvoker
 
- (void)executecommand
{
    [_command execute];
}
 
@end

client:
复制代码 代码如下:

//
//  main.m
//  commanddemo
//

#import <foundation/foundation.h>
#import "nimoreceiver.h"
#import "nimoinvoker.h"
#import "nimoconcretecommand.h"
 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
        nimoreceiver *receiver = [[nimoreceiver alloc] init];
        nimoconcretecommand *command = [[nimoconcretecommand alloc] initwithreceiver:receiver];
        
        nimoinvoker *invoker = [[nimoinvoker alloc] init];
        invoker.command = command;
        [invoker executecommand];
        
    }
    return 0;
}


running:

2015-08-13 22:49:56.412 commanddemo[1385:43303] 实际执行

cocoa touch框架中的命令模式:

nsinvocation对象

如下示例,client没有直接调用receiver的方法,而是用nsinvocation对象封装了运行时库向receiver发送执行消息所需的所有必要信息,这里的nsinvocation对象类似于上文中的concretecommand对象。

receiver:

复制代码 代码如下:

//
//  nimoreceiver.h
//  invocationdemo
//

#import <foundation/foundation.h>
 
@interface nimoreceiver : nsobject
 
- (int)printwithname:(nsstring *)name gender:(nsstring *)gender age:(int)age;
 
@end


复制代码 代码如下:

//
//  nimoreceiver.m
//  invocationdemo
//

#import "nimoreceiver.h"
 
@implementation nimoreceiver
 
- (int)printwithname:(nsstring *)name gender:(nsstring *)gender age:(int)age
{
    nslog(@"my name is %@, %@, %d years old.", name, gender, age);
    return 119;
}
 
@end


client:
复制代码 代码如下:

//
//  main.m
//  invocationdemo
//

#import <foundation/foundation.h>
#import "nimoreceiver.h"
 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //用receiver的实例创建nsinvocation对象,并把receiver的action作为选择器
        nimoreceiver *receiver = [[nimoreceiver alloc] init];
        nsstring *name = @"lee";
        nsstring *gender = @"male";
        int age = 28;
        sel sel = @selector(printwithname:gender:age:);
        nsmethodsignature *methodsignature = [[receiver class] instancemethodsignatureforselector:sel];
        nsinvocation *invocation = [nsinvocation invocationwithmethodsignature:methodsignature];
        [invocation settarget:receiver];
        [invocation setselector:sel];
        [invocation setargument:&name atindex:2];
        [invocation setargument:&gender atindex:3];
        [invocation setargument:&age atindex:4];
        [invocation retainarguments];
        [invocation invoke]; //通过调用nsinvocation对象的invoke方法,完成对receiver中action的调用
        
        int returnvalue = 0;
        [invocation getreturnvalue:&returnvalue];
        
        nslog(@"returnvalue: %d", returnvalue);
    }
    return 0;
}


running:

2015-08-14 13:37:44.162 invocationdemo[1049:36632] my name is lee, male, 28 years old.
2015-08-14 13:37:44.164 invocationdemo[1049:36632] returnvalue: 119

其实,单从类关系图中可以简单的看出,命令模式其实是把需求(invoker)和具体实现(receiver)通过命令层(command)进行了解耦。具体实现过程根据不同的命令进行了区分。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网