国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - IOS - iOS應(yīng)用設(shè)計模式開發(fā)中對簡單工廠和工廠方法模式的運(yùn)用

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

2021-01-12 16:09iOS開發(fā)網(wǎng) IOS

這篇文章主要介紹了iOS應(yīng)用設(shè)計模式開發(fā)中對簡單工廠和工廠方法模式的運(yùn)用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下

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

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

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

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

首先是運(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,無其他意義
}

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


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

 

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

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

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

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

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

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

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

 

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产免费黄色 | 亚州中文字幕 | 中文久久久久久 | 免费久久99精品国产婷婷六月 | 91精品国产综合久久久久久 | 欧美日韩高清 | 亚洲精品电影在线观看 | 国产性猛交xxxx免费看久久 | 欧美日韩国产一区二区三区不卡 | 亚洲视频在线观看 | 亚洲国产色视频 | 国产在线高清 | 黄视频入口 | 国产欧美日韩一区二区三区 | 欧美成人精品一区二区三区 | 亚洲福利国产 | 日本一区二区在线观看视频 | 国产精品免费观看 | 亚洲综合一区二区 | 中文一二区 | 毛片网站在线 | 91国产视频在线 | 97人人爱| 日日摸夜夜 | 国产美女www爽爽爽免费视频 | 久久久精品亚洲 | 久综合网 | 国产精品视频一区二区三区 | 日韩看片 | 久久综合亚洲精品 | 婷婷网址 | 欧美999| 亚洲伦理 | 一级毛片av| 日韩一区二区在线观看 | 日韩精品影视 | 亚洲精品第一 | 美女超碰| 日本三级中文在线电影 | 色偷偷噜噜噜亚洲男人 | 亚洲精品视频在线免费播放 |