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);

}

iOS之走进精益编程01的更多相关文章

  1. [C#] 走进异步编程的世界 - 开始接触 async/await(转)

    原文链接:http://www.cnblogs.com/liqingwen/p/5831951.html 走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 ...

  2. [C#] 走进异步编程的世界 - 开始接触 async/await

    走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 C# 5.0 引入的 async/await,但在控制台输出示例时经常会采用 C# 6.0 的 $&qu ...

  3. [C#] 走进异步编程的世界 - 剖析异步方法(上)

    走进异步编程的世界 - 剖析异步方法(上) 序 这是上篇<走进异步编程的世界 - 开始接触 async/await 异步编程>(入门)的第二章内容,主要是与大家共同深入探讨下异步方法. 本 ...

  4. [C#] 走进异步编程的世界 - 剖析异步方法(下)

    走进异步编程的世界 - 剖析异步方法(下) 序 感谢大家的支持,这是昨天发布<走进异步编程的世界 - 剖析异步方法(上)>的补充篇. 目录 异常处理 在调用方法中同步等待任务 在异步方法中 ...

  5. [C#] 走进异步编程的世界 - 在 GUI 中执行异步操作

    走进异步编程的世界 - 在 GUI 中执行异步操作 [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5877042.html 序 这是继<开始接 ...

  6. 并发编程 01—— ThreadLocal

    Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...

  7. 走进异步编程的世界 - 开始接触 async/await

    [C#] 走进异步编程的世界 - 开始接触 async/await   走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 C# 5.0 引入的 async ...

  8. iOS开发资源整理【01】

    一.网站 Code4App         开发者常用库分享网站 GitHub        git是一个优秀的分布式版本控制系统 stackoverflow    技术在线问答网站 CocoaChi ...

  9. iOS开发Swift篇(01) 变量&常量&元组

    iOS开发Swift篇(01) 变量&常量&元组 说明: 1)终于要写一写swift了.其实早在14年就已经写了swift的部分博客,无奈时过境迁,此时早已不同往昔了.另外,对于14年 ...

随机推荐

  1. org.springframework.core.NestedIOException

    今天遇到的一个小异常,报错信息如下: 2013-11-13 10:00:32 org.apache.catalina.core.StandardContext listenerStart严重: Exc ...

  2. [drp 7]转发和重定向的区别

    导读:类似于response.sendRedirect(request.getContextPath()+"/servlet/item/SearchItemServlet");和r ...

  3. hql 关联查询

    HQL 带的连接语句只能是实体与 该实体的属性 进行连接 其意义就是为了优化(通过延迟加载查询关联的属性)需要进行配置 from A left join A.B where (b.flag is nu ...

  4. Arch Linux 安装记录

    Arch Linux 安装记录 基本上参考wiki上的新手指南,使用arch 2014.6.1 iso安装 设置网络 有线网络 Arch Linux 默认开启DHCP. 静态ip 首先关闭DHCP:s ...

  5. Shell的概念

    Linux系统分为三个重要部分: 1:kernel(核心) 2:Shell 3:应用程序和工具

  6. php-resque学习笔记二(配置)

    1:前提 系统:CentOS PHP版本:PHP7     安装目录:/usr/local/php php-resque          安装目录:/home/software/php-resque ...

  7. ASP.NET MVC5 第4章

    参考资料<ASP.NET MVC5 高级编程>第5版 第4章 模型 本章所探讨的模型是要显示.保存.创建.更新和删除的对象. 基架指使用 MVC 提供的工具为每个模型对象的标准索引构建.创 ...

  8. 调试工具-fiddler

    本地资源替换线上调试 Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网 之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据(指cookie,html ...

  9. 日期转换(用DateTime的ParseExact方法解析特殊的日期时间)

    今天遇到一个特别的需求,需要从下面的字符串中转换成一个DateTime对象: [07-13 15:50:42] 主要问题是这个时间不是标准的时间,而是自定义的格式,即开头是月-日,然后是时间. 使用最 ...

  10. WCF帮助类

    using BJSW.ZTFX.Client.Silverlight.MapBusinessService; using System.ServiceModel; using System.SL.Ap ...