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

iOS應(yīng)用設(shè)計(jì)模式開(kāi)發(fā)中對(duì)簡(jiǎn)單工廠和工廠方法模式的運(yùn)用

 更新時(shí)間:2016年03月25日 09:26:10   投稿:goldensun  
這篇文章主要介紹了iOS應(yīng)用設(shè)計(jì)模式開(kāi)發(fā)中對(duì)簡(jiǎn)單工廠和工廠方法模式的運(yùn)用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下

簡(jiǎn)單工廠模式
正如此模式的名稱一樣,簡(jiǎn)單工廠模式基本上是所有設(shè)計(jì)模式里最簡(jiǎn)單的一種,類與類之間的關(guān)系一目了然。這次我就用很多地方經(jīng)常舉的例子--計(jì)算器,來(lái)說(shuō)明這個(gè)模式。首先給大家展示一下類之間的結(jié)構(gòu)圖:

201632592553288.jpg (500×241)

通過(guò)這張結(jié)構(gòu)圖,可以清晰的看到,加法類、減法類、乘法類、除法類繼承自運(yùn)算類,簡(jiǎn)單工廠類依賴于運(yùn)算類的實(shí)例化來(lái)實(shí)現(xiàn)相應(yīng)的運(yùn)算功能,好的,看起來(lái)并不復(fù)雜,讓我們直接展示一下代碼吧(鑒于目前點(diǎn)點(diǎn)不支持Objective C的代碼高亮,所以就直接寫啦,盡量保持整齊吧。另,為了照顧像我一樣基礎(chǔ)不是很好的同學(xué),我盡量把代碼寫全,方便大家調(diào)試)。

注意:本文所有代碼均在ARC環(huán)境下編譯通過(guò)。

首先是運(yùn)算類(父類):
接口文件:

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

#import <Foundation/Foundation.h>

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


實(shí)現(xiàn)文件:
復(fù)制代碼 代碼如下:

#import"Operation.h"

@implementation Operation
@synthesize numberA, numberB;

-(double) GetResult{
    return    -1.0;      //此處默認(rèn)返回-1.0,無(wú)其他意義
}

@end


加法類(運(yùn)算子類):
接口文件:
復(fù)制代碼 代碼如下:

#import "Operation.h"

@interface OperationAdd:Operation
@end


實(shí)現(xiàn)文件:
復(fù)制代碼 代碼如下:

#import "OperationAdd.h"

@implementation OperationAdd

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

@end


減法類(運(yùn)算子類):
接口文件:
復(fù)制代碼 代碼如下:

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

實(shí)現(xiàn)文件:
復(fù)制代碼 代碼如下:

#import "OperationSub.h"

@implementation OperationSub

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

@end


乘法類(運(yùn)算子類)
復(fù)制代碼 代碼如下:

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

實(shí)現(xiàn)文件:
復(fù)制代碼 代碼如下:

#import "OperationMul.h"

@implementation OperationMul

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

@end


除法類(運(yùn)算子類):
接口文件:
復(fù)制代碼 代碼如下:

#import "Operation.h"

@interface OperationDiv:Operation
@end


實(shí)現(xiàn)文件:
復(fù)制代碼 代碼如下:

#import "OperationDiv.h"

@implementation OperationDiv

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

@end


下面是工廠類(依賴實(shí)力化運(yùn)算類實(shí)現(xiàn)具體功能):
接口文件:
復(fù)制代碼 代碼如下:

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

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


實(shí)現(xiàn)文件:
復(fù)制代碼 代碼如下:

#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;
}


具體調(diào)用
復(fù)制代碼 代碼如下:

#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;
}


好啦,上面羅列的是簡(jiǎn)單工廠模式的基礎(chǔ)代碼。其實(shí)還是挺簡(jiǎn)單的,對(duì)吧,只有一層繼承關(guān)系,一個(gè)依賴關(guān)系,在工廠類里面用switch語(yǔ)句判別需要實(shí)例化哪種類型,之后進(jìn)行計(jì)算,獲取結(jié)果。

工廠方法模式
上面關(guān)于簡(jiǎn)單工廠模式中就有提到過(guò)一次關(guān)于“工廠類”模式。為了幫助大家能夠回憶一下簡(jiǎn)單工廠模式,在這里提一下簡(jiǎn)單工廠模式的優(yōu)點(diǎn),簡(jiǎn)單工廠模式的最大優(yōu)點(diǎn)在于工廠類中包含了必要的邏輯判斷,根據(jù)客戶端的選擇條件動(dòng)態(tài)實(shí)例化相關(guān)的類,對(duì)于客戶端來(lái)說(shuō),去除了與具體產(chǎn)品的依賴。其實(shí),工廠方法模式是簡(jiǎn)單工廠模式的進(jìn)一步抽象和推廣。由于使用了多態(tài)性,工廠方法模式保持了簡(jiǎn)單工廠模式的優(yōu)點(diǎn),而且克服了它的缺點(diǎn)。但缺點(diǎn)是,由于每加一個(gè)產(chǎn)品,就需要加一個(gè)產(chǎn)品工廠的類,增加了額外的開(kāi)發(fā)量。

下面還是以計(jì)算器為例子,詳細(xì)介紹工廠方法模式,還是老樣子,先向大家展示一下類結(jié)構(gòu)圖。

201632592617967.jpg (500×507)

上面這張圖向大家展示了各個(gè)類之間的關(guān)系。其實(shí)和簡(jiǎn)單工廠模式不同的是,類圖的右邊抽象工廠接口是相比簡(jiǎn)單工廠模式多出來(lái)的抽象接口。

下面直接上代碼吧,別的不多說(shuō)了。

注意:本文所有代碼均在ARC環(huán)境下編譯通過(guò)。

Operation類接口

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

#import <Foundation/Foundation.h>

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


Operation類實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

#import "Operation.h"

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


OperationAdd類接口
復(fù)制代碼 代碼如下:

#import "Operation.h"

@interface OperationAdd :Operation
@end


OperationAdd類實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

#import "OperationAdd.h"

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


OperationDiv類接口
復(fù)制代碼 代碼如下:

#import "Operation.h"

@interface OperationDiv :Operation
@end


OperationDiv類實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

#import "OperationDiv.h"

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


OperationMul類接口
復(fù)制代碼 代碼如下:

#import "Operation.h"

@interface OperationMul :Operation
@end
OperationMul類實(shí)現(xiàn)

#import "OperationMul.h"

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


OperationSub類接口
復(fù)制代碼 代碼如下:

#import "Operation.h"

@interface OperationSub :Operation
@end


OperationSub類實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

#import "OperationSub.h"

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


IFactory類接口
復(fù)制代碼 代碼如下:

#import <Foundation/Foundation.h>

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


IFactory類實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

#import "IFactory.h"

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


AddFactory類接口
復(fù)制代碼 代碼如下:

#import "IFactory.h"

@interface AddFactory :IFactory
@end


AddFactory類實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

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

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


SubFactory類接口
復(fù)制代碼 代碼如下:

#import "IFactory.h"

@interface SubFactory :IFactory
@end


SubFactory類實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

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

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


MulFactory類接口
復(fù)制代碼 代碼如下:

#import "IFactory.h"

@interface MulFactory :IFactory
@end


MulFactory類實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

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

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


DivFactory類接口
復(fù)制代碼 代碼如下:

#import "IFactory.h"

@interfaceDiv Factory :IFactory
@end


DivFactory類實(shí)現(xiàn)
復(fù)制代碼 代碼如下:

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

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


Main方法調(diào)用
復(fù)制代碼 代碼如下:

#import <Foundation/Foundation.h>
#import "OperationAdd.h"
#import "AddFactory.h" //加法工廠,你可以根據(jù)需要添加其他運(yùn)算工廠

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的類代碼。

相關(guān)文章

  • iOS使用UIScorllView實(shí)現(xiàn)兩指縮放功能

    iOS使用UIScorllView實(shí)現(xiàn)兩指縮放功能

    兩指縮放功能不僅可以用UIPinchGestureRecognizer手勢(shì)來(lái)實(shí)現(xiàn),還能用UIScorllView來(lái)實(shí)現(xiàn),UIScrollView可以輕松的實(shí)現(xiàn)最大與最小縮放值,以及滾動(dòng)的效果,效果非常棒,具體實(shí)例代碼大家參考下本文吧
    2017-03-03
  • iOS導(dǎo)航欄對(duì)控制器view的影響詳解

    iOS導(dǎo)航欄對(duì)控制器view的影響詳解

    這篇文章主要給大家介紹了關(guān)于iOS導(dǎo)航欄對(duì)控制器view的影響的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • iOS Swift控制器轉(zhuǎn)場(chǎng)動(dòng)畫示例代碼

    iOS Swift控制器轉(zhuǎn)場(chǎng)動(dòng)畫示例代碼

    這篇文章主要給大家介紹了關(guān)于iOS Swift控制器轉(zhuǎn)場(chǎng)動(dòng)畫的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • 判斷iOS應(yīng)用是否開(kāi)放HTTP權(quán)限的方法

    判斷iOS應(yīng)用是否開(kāi)放HTTP權(quán)限的方法

    這篇文章主要為大家詳細(xì)介紹了判斷iOS應(yīng)用是否開(kāi)放HTTP權(quán)限的方法,感興趣的小伙伴們可以參考一下
    2016-03-03
  • 實(shí)例講解iOS應(yīng)用開(kāi)發(fā)中UIPickerView滾動(dòng)選擇欄的用法

    實(shí)例講解iOS應(yīng)用開(kāi)發(fā)中UIPickerView滾動(dòng)選擇欄的用法

    這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中UIPickerView滾動(dòng)選擇欄的用法,示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-04-04
  • iOS自定義選擇框代碼分享

    iOS自定義選擇框代碼分享

    這篇文章主要為大家分享了iOS自定義選擇框的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • iOS應(yīng)用開(kāi)發(fā)中使UITextField實(shí)現(xiàn)placeholder屬性的方法

    iOS應(yīng)用開(kāi)發(fā)中使UITextField實(shí)現(xiàn)placeholder屬性的方法

    這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中使UITextField實(shí)現(xiàn)placeholder屬性的方法,示例代碼為傳統(tǒng)的Objective-C語(yǔ)言,需要的朋友可以參考下
    2016-04-04
  • CAMediaTiming ( 時(shí)間協(xié)議)詳解及實(shí)例代碼

    CAMediaTiming ( 時(shí)間協(xié)議)詳解及實(shí)例代碼

    這篇文章主要介紹了CAMediaTiming / 時(shí)間協(xié)議詳解及實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下
    2016-12-12
  • IOS開(kāi)發(fā)QQ空間/朋友圈類界面的搭建

    IOS開(kāi)發(fā)QQ空間/朋友圈類界面的搭建

    本篇文章主要介紹了iOS開(kāi)發(fā)之類似朋友圈的社交界面實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11
  • iOS開(kāi)發(fā)之如何給View添加指定位置的邊框線詳解

    iOS開(kāi)發(fā)之如何給View添加指定位置的邊框線詳解

    這篇文章主要給大家介紹了iOS開(kāi)發(fā)之如何給View添加指定位置的邊框線的相關(guān)資料,給view加邊框很容易,重點(diǎn)是如何給指定邊框加邊框,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-10-10

最新評(píng)論