
最近在看公司的項(xiàng)目,了解到了typhoon框架及面向切面編程思想,以往都是面向?qū)ο缶幊?OOP).
首先先來了解下兩個概念:
OOP:
面向?qū)ο缶幊淌怯蓡蝹€能夠起到子程序作用的單元或?qū)ο蠼M合而成,將接口與實(shí)現(xiàn)分離的過程,每個對象都能夠接收信息、處理數(shù)據(jù)和向其它對象發(fā)送信息。主要包括:對象、類、數(shù)據(jù)抽象、繼承、動態(tài)綁定、數(shù)據(jù)封裝、多態(tài)性、消息傳遞。
面向切面編程(AOP):
通過預(yù)編譯方式和運(yùn)行期動態(tài)代理實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù), 針對業(yè)務(wù)處理過程中的切面進(jìn)行提取,它所面對的是處理過程中的某個步驟或階段,以獲得邏輯過程中各部分之間低耦合性的隔離效果。
總的概括來說OOP實(shí)際上是對對象的屬性和行為的封裝,而AOP對于這點(diǎn)就無從談起,但是AOP是處理某個步驟和階段的,從中進(jìn)行切面的提取,也就是說,如果幾個或更多個邏輯過程中,有重復(fù)的操作行為,AOP就可以提取出來,運(yùn)用動態(tài)代理,實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù)。兩者在一起使用揚(yáng)長補(bǔ)短最好不過了。
typhoon框架介紹
typhoon 是iOS 依賴注入框架,依賴注入就是你需要使用一個對象,你不需要自己去創(chuàng)建這個對象,而是通過系統(tǒng)的ioc容器為你創(chuàng)建一個,并交付給你。 通過ioc容器實(shí)現(xiàn)依賴注入,我們可以減少模塊和模塊,組件和組件之間的耦合,提高代碼的可維護(hù)性。你只要引用一個對象,不再創(chuàng)建init,直接使用
typhoon框架地址
typhoon框架使用
** 一、導(dǎo)入typhoon框架**
import “TyphoonAssembly.h”
二.創(chuàng)建class繼承TyphoonAssembly
三.注入一個對象(OTPCoderRequestDTO)
-(OTPCoderRequestDTO *)sendOTPCoderDTO:(NSString *)phone{
return [TyphoonDefinition withClass:[OTPCoderRequestDTO class] configuration:^(TyphoonDefinition *definition) {
[definition injectMethod:@selector(generateParamWithPhoneStr:withType:) parameters:^(TyphoonMethod *method) {
[method injectParameter:phoneStr];
[method injectParameter:[NSNumber numberWithInt:OTPCodeType_reg]];
}];
}];
}
**四、需要在info.plist文件中創(chuàng)建一個nsarray,名字為TyphoonInitialAssemblies,在里面添加你實(shí)現(xiàn)了TyphoonAssembly 協(xié)議的類名。 **
typhoon Interface Methods
1.類方法注入
- (UIWindow *)mainWindow
{
return [TyphoonDefinition withClass:[UIWindow class] configuration:^(TyphoonDefinition *definition)
{
[definition useInitializer:@selector(initWithFrame:) parameters:^(TyphoonMethod *initializer)
{
[initializer injectParameterWith:[NSValue valueWithCGRect:[[UIScreen mainScreen] bounds]]];
}];
[definition injectProperty:@selector(rootViewController) with:[self navViewController]];
}];
}
2.屬性注入
[definition injectProperty:@selector(requestDTO) with:requestDTO];
3.普通方法注入
[definition injectMethod:@selector(generateParamWithPhone:) parameters:^(TyphoonMethod *method) {
[method injectParameterWith:phone];
}];
4.如果你需要在注入一個對象之前或之后做一些操作
//之后
[definition performAfterInjections:@selector(generateParamWithString:) parameters:parameters:^(TyphoonMethod *method) {
[method injectParameterWith:string];
}];
//之前
[definition performBeforeInjections:@selector(generateParamWithString:) parameters:parameters:^(TyphoonMethod *method) {
[method injectParameterWith:string];
}];
5.注入集合
[definition injectProperty:@selector(dataArray) with:@[@“1”,@“2”]];
以上基本就是typhoon框架的使用方法了。
下邊是typhoon框架地址https://github.com/appsquickly/Typhoon