欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

實(shí)例講解設(shè)計(jì)模式中的命令模式在iOS App開發(fā)中的運(yùn)用

 更新時(shí)間:2016年03月24日 09:22:24   作者:TonyGo  
這篇文章主要介紹了設(shè)計(jì)模式中的命令模式在iOS App開發(fā)中的運(yùn)用,文中還講到了Cocoa框架下使用的例子,實(shí)例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下

命令模式封裝一個(gè)請(qǐng)求或行為作為一個(gè)對(duì)象。封裝的請(qǐng)求比原的更加靈活,可以在對(duì)象之間傳遞,儲(chǔ)存,動(dòng)態(tài)修改,或放入一個(gè)隊(duì)列。

那么讓我們簡(jiǎn)要的說一下命令模式的特點(diǎn)。

  • 它能比較容易地設(shè)計(jì)一個(gè)命令隊(duì)列;
  • 在需要的情況下,可以較容易地將命令記入日志;
  • 允許接收請(qǐng)求地一方?jīng)Q定是否要否決請(qǐng)求;
  • 可以容易地實(shí)現(xiàn)對(duì)請(qǐng)求地撤銷和重做;
  • 由于加進(jìn)新地具體命令類不影響其他的類,因此增加新的具體命令類很容易;
  • 把請(qǐng)求一個(gè)操作的對(duì)象與知道怎么執(zhí)行一個(gè)操作的對(duì)象分隔開。

下面給出基本的類結(jié)構(gòu)圖:

201632492123555.jpg (448×315)

上面這張圖是命令模式的類結(jié)構(gòu)的基本圖。其實(shí)從這張圖中還可以擴(kuò)展出很多,細(xì)節(jié)就不說了,給大家留一些想象的空間,呵呵!

還是老規(guī)矩,下面給出實(shí)例:

Objective-C 示例:

Command:

復(fù)制代碼 代碼如下:

//
//  NimoCommand.h
//  CommandDemo
//
 
#import <Foundation/Foundation.h>
 
@protocol NimoCommand <NSObject>
 
- (void)execute;
 
@end

ConcreteCommand:
復(fù)制代碼 代碼如下:

//
//  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

復(fù)制代碼 代碼如下:

//
//  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:
復(fù)制代碼 代碼如下:

//
//  NimoReceiver.h
//  CommandDemo
//

#import <Foundation/Foundation.h>
 
@interface NimoReceiver : NSObject
 
- (void)action;
 
@end


復(fù)制代碼 代碼如下:

//
//  NimoReceiver.m
//  CommandDemo
//

#import "NimoReceiver.h"
 
@implementation NimoReceiver
 
- (void)action
{
    NSLog(@"實(shí)際執(zhí)行");
}
 
@end


Invoker:
復(fù)制代碼 代碼如下:

//
//  NimoInvoker.h
//  CommandDemo
//
 
#import <Foundation/Foundation.h>
#import "NimoCommand.h"
 
@interface NimoInvoker : NSObject
 
@property (nonatomic, weak) id<NimoCommand> command;
 
- (void)executeCommand;
 
@end

復(fù)制代碼 代碼如下:

//
//  NimoInvoker.m
//  CommandDemo
//
 
#import "NimoInvoker.h"
 
 
@implementation NimoInvoker
 
- (void)executeCommand
{
    [_command execute];
}
 
@end

Client:
復(fù)制代碼 代碼如下:

//
//  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] 實(shí)際執(zhí)行

Cocoa Touch框架中的命令模式:

NSInvocation對(duì)象

如下示例,Client沒有直接調(diào)用Receiver的方法,而是用NSInvocation對(duì)象封裝了運(yùn)行時(shí)庫(kù)向Receiver發(fā)送執(zhí)行消息所需的所有必要信息,這里的NSInvocation對(duì)象類似于上文中的ConcreteCommand對(duì)象。

Receiver:

復(fù)制代碼 代碼如下:

//
//  NimoReceiver.h
//  InvocationDemo
//

#import <Foundation/Foundation.h>
 
@interface NimoReceiver : NSObject
 
- (int)printWithName:(NSString *)name gender:(NSString *)gender age:(int)age;
 
@end


復(fù)制代碼 代碼如下:

//
//  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:
復(fù)制代碼 代碼如下:

//
//  main.m
//  InvocationDemo
//

#import <Foundation/Foundation.h>
#import "NimoReceiver.h"
 
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //用Receiver的實(shí)例創(chuàng)建NSInvocation對(duì)象,并把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]; //通過調(diào)用NSInvocation對(duì)象的invoke方法,完成對(duì)Receiver中action的調(diào)用
        
        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

其實(shí),單從類關(guān)系圖中可以簡(jiǎn)單的看出,命令模式其實(shí)是把需求(Invoker)和具體實(shí)現(xiàn)(Receiver)通過命令層(Command)進(jìn)行了解耦。具體實(shí)現(xiàn)過程根據(jù)不同的命令進(jìn)行了區(qū)分。

相關(guān)文章

  • iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件

    iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件

    這篇文章主要介紹了iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-11-11
  • 淺談iOS 數(shù)據(jù)結(jié)構(gòu)之鏈表

    淺談iOS 數(shù)據(jù)結(jié)構(gòu)之鏈表

    這篇文章主要介紹了淺談iOS 數(shù)據(jù)結(jié)構(gòu)之鏈表,本文詳細(xì)的介紹了單鏈表和雙鏈表,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-09-09
  • iOS Block解開多年以來(lái)一直的誤解

    iOS Block解開多年以來(lái)一直的誤解

    這篇文章主要給大家介紹了關(guān)于iOS Block多年以來(lái)一直的誤解如何解開的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • iOS實(shí)現(xiàn)控制屏幕常亮不變暗的方法示例

    iOS實(shí)現(xiàn)控制屏幕常亮不變暗的方法示例

    最近在工作中遇到了要將iOS屏幕保持常亮的需求,所以下面這篇文章主要給大家介紹了關(guān)于利用iOS如何實(shí)現(xiàn)控制屏幕常亮不變暗的方法,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-10-10
  • iOS如何利用一句話完成轉(zhuǎn)場(chǎng)動(dòng)畫

    iOS如何利用一句話完成轉(zhuǎn)場(chǎng)動(dòng)畫

    這篇文章主要給大家介紹了關(guān)于iOS如何利用一句話完成轉(zhuǎn)場(chǎng)動(dòng)畫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • 剖析iOS開發(fā)中Cocos2d-x的內(nèi)存管理相關(guān)操作

    剖析iOS開發(fā)中Cocos2d-x的內(nèi)存管理相關(guān)操作

    這篇文章主要介紹了剖析iOS開發(fā)中Cocos2d-x的內(nèi)存管理相關(guān)操作,Cocos2d-x是開發(fā)游戲的利器,需要的朋友可以參考下
    2015-10-10
  • iOS runtime forwardInvocation詳解及整理

    iOS runtime forwardInvocation詳解及整理

    這篇文章主要介紹了 iOS runtime forwardInvocation詳解及整理的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • IOS UI學(xué)習(xí)教程之設(shè)置UITextField各種屬性

    IOS UI學(xué)習(xí)教程之設(shè)置UITextField各種屬性

    這篇文章主要為大家詳細(xì)介紹了IOS UI學(xué)習(xí)教程之設(shè)置UITextField各種屬性,感興趣的小伙伴們可以參考一下
    2016-03-03
  • 詳解iOS App中圖片的線段涂鴉功能的添加方法

    詳解iOS App中圖片的線段涂鴉功能的添加方法

    這篇文章主要介紹了如何設(shè)計(jì)iOS App中圖片的線段涂鴉功能,也就是很多應(yīng)用中圖片上傳時(shí)帶有的編輯功能的基礎(chǔ),需要的朋友可以參考下
    2016-03-03
  • iOS異步下載圖片實(shí)例代碼

    iOS異步下載圖片實(shí)例代碼

    這篇文章主要介紹了iOS異步下載圖片實(shí)例代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-08-08

最新評(píng)論