你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS運用設計形式開辟中對簡略工場和工場辦法形式的應用

iOS運用設計形式開辟中對簡略工場和工場辦法形式的應用

編輯:IOS開發綜合

簡略工場形式
正如斯形式的稱號一樣,簡略工場形式根本上是一切設計形式裡最簡略的一種,類與類之間的關系了如指掌。此次我就用許多處所常常舉的例子--盤算器,來講明這個形式。起首給年夜家展現一下類之間的構造圖:

201632592553288.jpg (500×241)

經由過程這張構造圖,可以清楚的看到,加法類、減法類、乘法類、除法類繼續自運算類,簡略工場類依附於運算類的實例化來完成響應的運算功效,好的,看起來其實不龐雜,讓我們直接展現一下代碼吧(鑒於今朝點點不支撐Objective C的代碼高亮,所以就直接寫啦,盡可能堅持整潔吧。另,為了照料像我一樣基本不是很好的同窗,我盡可能把代碼寫全,便利年夜家調試)。

留意:本文一切代碼均在ARC情況下編譯經由過程。

起首是運算類(父類):
接口文件:

#import <Foundation/Foundation.h>

@interface Operation :NSObject{
    double numberA;
    double numberB;
}
@property double numberA;
@property double numberB;
-(double) GetResult;
@end

完成文件:

#import"Operation.h"

@implementation Operation
@synthesize numberA, numberB;

-(double) GetResult{
    return    -1.0;      //此處默許前往-1.0,無其他意義
}

@end

加法類(運算子類):
接口文件:

#import "Operation.h"

@interface OperationAdd:Operation
@end

完成文件:

#import "OperationAdd.h"

@implementation OperationAdd

-(double) GetResult{
    double result =0;
    result =numberA+numberB;
    return result;
}

@end

減法類(運算子類):
接口文件:

#import "Operation.h"
@interface OperationSub:Operation
@end

完成文件:

#import "OperationSub.h"

@implementation OperationSub

-(double)GetResult{
    double result =0;
    result = numberA-numberB;
    return result;
}

@end

乘法類(運算子類)

#import "Operation.h"
@interface OperationMul:Operation
@end

完成文件:

#import "OperationMul.h"

@implementation OperationMul

-(double)GetResult{
    double result =0;
    result = numberA*numberB;
    return result;
}

@end

除法類(運算子類):
接口文件:

#import "Operation.h"

@interface OperationDiv:Operation
@end

完成文件:

#import "OperationDiv.h"

@implementation OperationDiv

-(double)GetResult{
    double result =0;
    @try{
        result = numberA/numberB;
    }
    @catch(NSException *exception) {
        NSLog(@"除數不克不及為0");
    }
    return result;
}

@end

上面是工場類(依附實力化運算類完成詳細功效):
接口文件:

#import <Foundation/Foundation.h>
#import "OperationAdd.h"
#import "OperationDiv.h"
#import "OperationSub.h"
#import "OperationMul.h"

@interface OperationFactory:NSObject
+(Operation*)CreateOperate:(char)operate;
@end

完成文件:

#import "OperationFactory.h"

+(Operation*)CreateOperate:(char)operate{
    Operation *oper;
    switch(operate) {
        case '+':
            oper = [[OperationAdd alloc]init];
            break;
        case '-':
            oper = [[OperationSub alloc]init];
            break;
        case '*':
            oper = [[OperationMul alloc]init];
            break;
        case '/':
            oper = [[OperationDiv alloc]init];
            break;
        default:
            oper = nil;
            break;
        }
        return oper;
}

詳細挪用

#import <Foundation/Foundation.h>
#import "OperationAdd.h"
#import "OperationDiv.h"
#import "OperationMul.h"
#import "OperationSub.h"
#import "OperationFactory.h"

int main (int argc,const char* argv[])
{
    @autoreleasepool{
        Operation *oper = [OperationFactory CreateOperate:'*'];
        [oper setNumberA:1];
        [oper setNumberB:2];
        double result = 0;
        result = [oper GetResult];
        NSLog(@"Result is %f", result);
    }
    return 0;
}

好啦,下面枚舉的是簡略工場形式的基本代碼。其實照樣挺簡略的,對吧,只要一層繼續關系,一個依附關系,在工場類外面用switch語句辨別須要實例化哪一種類型,以後停止盤算,獲得成果。

工場辦法形式
下面關於簡略工場形式中就有提到過一次關於“工場類”形式。為了贊助年夜家可以或許回想一下簡略工場形式,在這裡提一下簡略工場形式的長處,簡略工場形式的最年夜長處在於工場類中包括了需要的邏輯斷定,依據客戶真個選擇前提靜態實例化相干的類,關於客戶端來講,去除與詳細產物的依附。其實,工場辦法形式是簡略工場形式的進一步籠統和推行。因為應用了多態性,工場辦法形式堅持了簡略工場形式的長處,並且戰勝了它的缺陷。但缺陷是,因為每加一個產物,就須要加一個產物工場的類,增長了額定的開辟量。

上面照樣以盤算器為例子,具體引見工場辦法形式,照樣老模樣,先向年夜家展現一下類構造圖。

201632592617967.jpg (500×507)

下面這張圖向年夜家展現了各個類之間的關系。其實和簡略工場形式分歧的是,類圖的左邊籠統工場接口是比擬簡略工場形式多出來的籠統接口。

上面直接上代碼吧,其余不多說了。

留意:本文一切代碼均在ARC情況下編譯經由過程。

Operation類接口

#import <Foundation/Foundation.h>

@interface Operation :NSObject{
    double numberA;
    double numberB;
}
@property double numberA;
@property double numberB;
-(double) GetResult;
@end

Operation類完成

#import "Operation.h"

@implementation Operation
@synthesize numberA, numberB;
-(double) GetResult{
    return -1.0;
}
@end

OperationAdd類接口

#import "Operation.h"

@interface OperationAdd :Operation
@end

OperationAdd類完成

#import "OperationAdd.h"

@implementation OperationAdd
-(double) GetResult{
    double result =0;
    result = numberA+numberB;
    return result;
}
@end

OperationDiv類接口

#import "Operation.h"

@interface OperationDiv :Operation
@end

OperationDiv類完成

#import "OperationDiv.h"

@implementation OperationDiv
-(double)GetResult{
    double result =0;
    @try{
        result = numberA/numberB;
    }
    @catch(NSException *exception) {
        NSLog(@"除數不克不及為0");
    }
    return result;
}
@end

OperationMul類接口

#import "Operation.h"

@interface OperationMul :Operation
@end
OperationMul類完成

#import "OperationMul.h"

@implementation OperationMul
-(double)GetResult{
    double result =0;
    result = numberA*numberB;
    return result;
}
@end

OperationSub類接口

#import "Operation.h"

@interface OperationSub :Operation
@end

OperationSub類完成

#import "OperationSub.h"

@implementation OperationSub
-(double)GetResult{
    double result =0;
    result = numberA-numberB;
    return result;
}
@end

IFactory類接口

#import <Foundation/Foundation.h>

#import "Operation.h"
@interface IFactory :NSObject
-(Operation*)CreateOperation;
@end

IFactory類完成

#import "IFactory.h"

@implementation IFactory
-(Operation*)CreateOperation{
    return [[Operation alloc]init];
}
@end

AddFactory類接口

#import "IFactory.h"

@interface AddFactory :IFactory
@end

AddFactory類完成

#import "AddFactory.h"
#import "OperationAdd.h"

@implementation AddFactory
-(Operation*)CreateOperation{
    return [[OperationAdd alloc]init];
}
@end

SubFactory類接口

#import "IFactory.h"

@interface SubFactory :IFactory
@end

SubFactory類完成

#import "SubFactory.h"
#import "OperationSub.h"

@implementation SubFactory
-(Operation*)CreateOperation{
    return [[OperationSub alloc]init];
}
@end

MulFactory類接口

#import "IFactory.h"

@interface MulFactory :IFactory
@end

MulFactory類完成

#import "MulFactory.h"
#import "OperationMul.h"

@implementation MulFactory
-(Operation*)CreateOperation{
    return [[OperationMul alloc]init];
}
@end

DivFactory類接口

#import "IFactory.h"

@interfaceDiv Factory :IFactory
@end

DivFactory類完成

#import "DivFactory.h"
#import "OperationDiv.h"

@implementation DivFactory
-(Operation*)CreateOperation{
    return [[OperationDiv alloc]init];
}
@end

Main辦法挪用

#import <Foundation/Foundation.h>
#import "OperationAdd.h"
#import "AddFactory.h" //加法工場,你可以依據須要添加其他運算工場

int main (int argc,const char* argv[])
{
    @autoreleasepool{
        IFactory *operFactory = [[AddFactory alloc]init];
        Operation *oper = [operFactory CreateOperation];
        [oper setNumberA:1];
        [oper setNumberB:2];
        double result = [oper GetResult];
        NSLog(@"The result is %f", result);
    }
    return 0;
}

好啦,下面就是工場辦法形式的Objective C的類代碼。

【iOS運用設計形式開辟中對簡略工場和工場辦法形式的應用】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

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