你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 實例講授設計形式中的敕令形式在iOS App開辟中的應用

實例講授設計形式中的敕令形式在iOS App開辟中的應用

編輯:IOS開發綜合

敕令形式封裝一個要求或行動作為一個對象。封裝的要求比原的加倍靈巧,可以在對象之間傳遞,貯存,靜態修正,或放入一個隊列。

那末讓我們扼要的說一下敕令形式的特色。

  • 它能比擬輕易地設計一個敕令隊列;
  • 在須要的情形下,可以較輕易地將敕令記入日記;
  • 許可吸收要求地一方決議能否要否決要求;
  • 可以輕易地完成對要求地撤消和重做;
  • 因為加進新地詳細敕令類不影響其他的類,是以增長新的詳細敕令類很輕易;
  • 把要求一個操作的對象與曉得怎樣履行一個操作的對象分離隔。

上面給出根本的類構造圖:

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 App開辟中的應用】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved