iOS之走进精益编程01

时间:2023-03-09 12:56:19
iOS之走进精益编程01

Model类

.h

#import <Foundation/Foundation.h>

@interface Product : NSObject

@property (nonatomic,assign) NSNumber *weight;

@property (nonatomic,copy)   NSString *color;

@end

.m

无增加内容

viewcontroller.h

@interface ColorSpec :NSObject

@property (nonatomic, copy) NSString *color;

@end

@implementation ColorSpec

+ (instancetype)specWithColor:(NSString *)color

{

ColorSpec *spec = [[ColorSpec alloc] init];

spec.color = color;

return spec;

}

- (BOOL)satisfy:(Product *)product

{

return [product.color isEqualToString:_color];

}

@end

@interface BelowWeightSpec :NSObject

@property (nonatomic, assign) int limit;

@end

@implementation BelowWeightSpec

+ (instancetype)specWithBelowWeight:(float)limit

{

BelowWeightSpec *spec = [[BelowWeightSpec alloc] init];

spec.limit = limit;

return spec;

}

- (BOOL)satisfy:(Product *)product

{

return ([product.weight intValue] < _limit);

}

@end

@interface ViewController ()

{

NSMutableArray *_datas;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

[self setupModel];

}

- (void)setupModel{

_datas = [[NSMutableArray alloc]init];

for (int i = 0; i < 1000 ; i ++) {

Product *product = [Product new];

product.weight = [NSNumber numberWithInt:i%11];

product.color  = [self getColorWithInt:i];

[_datas addObject:product];

}

}

- (NSString *)getColorWithInt:(int)index{

switch (index%9) {

case 0:

{

return @"GREEN";

}

break;

case 1:

{

return @"BLACK";

}

break;

case 2:

{

return @"RED";

}

break;

case 3:

{

return @"WHIRT";

}

break;

case 4:

{

return @"GRAY";

}

break;

case 5:

{

return @"LIGHT";

}

break;

case 6:

{

return @"ORANGE";

}

break;

case 7:

{

return @"BLUE";

}

break;

case 8:

{

return @"YELLOW";

}

break;

default:

break;

}

return nil;

}

// 01.在仓库里找到所有颜色为红色的产品

- (NSArray *)findAllRedProducts:(NSArray *)products{

NSMutableArray *list = [@[]mutableCopy];

for (Product *obj in products) {

if ([obj.color isEqualToString:@"RED"]) {

[list addObject:obj];

}

}

return list;

}

// 02.在仓库中查找所有颜色为绿色的产品<参数化设置>

- (NSArray *)findProducts:(NSArray *)products byColor:(ColorSpec *)color

{

NSMutableArray *list = [@[] mutableCopy];

for (Product *product in products) {

if ([color satisfy:product]) {

[list addObject:product];

}

}

return list;

}

// 03.查找所有重量小于10的所有产品

- (NSArray *)findProducts:(NSArray *)products bySpec:(BelowWeightSpec *)spec

{

NSMutableArray *list = [@[] mutableCopy];

for (Product *product in products) {

if ([spec satisfy:product]) {

[list addObject:product];

}

}

return list;

}

- (IBAction)findREDClick:(UIButton *)sender {

NSArray *redArr = [self findProducts:_datas byColor:[ColorSpec specWithColor:@"RED"]];

NSLog(@"---- all RED :%tu",redArr.count);

NSArray *greenArr = [self findProducts:_datas byColor:[ColorSpec specWithColor:@"GREEN"]];

NSLog(@"---- all GREEN :%tu",greenArr.count);

NSArray *small10Arr = [self findProducts:_datas bySpec:[BelowWeightSpec specWithBelowWeight:10]];

NSLog(@"---- all < 10 : %tu",small10Arr.count);

}