Simple Factory Method(簡(jiǎn)單工廠模式)
定義一個(gè)用于創(chuàng)建對(duì)象的接口洋满,讓子類決定實(shí)例化哪一個(gè)類绝葡。工廠方法使一個(gè)類的實(shí)例化延遲到其子類带族。
.h
+ (id)createCalcute:(NSString *)calculatetype;
.m
+ (id<HCDCalculate>)createCalcute:(NSString *)calculatetype{
NSArray *calculateArray = @[@"+",@"-",@"*",@"/"];
CalculateType calType = [calculateArray indexOfObject:calculatetype];
switch (calType) {
case calcuteTypeAdd:
return [[HCDCalculateAdd alloc]init];
break;
case calcuteTypeMinus:
return [[HCDCalculateMinus alloc]init];
break;
case calcuteTypdeMultipy:
return [[HCDCalcuteMultiply alloc]init];
case calcuteTypeDivide:
return [[HCDCalculateDivide alloc]init];
}
}