Simple Factory Method(简单工厂模式)
定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。
.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];
}
}