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);
}
iOS之走进精益编程01的更多相关文章
- [C#] 走进异步编程的世界 - 开始接触 async/await(转)
原文链接:http://www.cnblogs.com/liqingwen/p/5831951.html 走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 ...
- [C#] 走进异步编程的世界 - 开始接触 async/await
走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 C# 5.0 引入的 async/await,但在控制台输出示例时经常会采用 C# 6.0 的 $&qu ...
- [C#] 走进异步编程的世界 - 剖析异步方法(上)
走进异步编程的世界 - 剖析异步方法(上) 序 这是上篇<走进异步编程的世界 - 开始接触 async/await 异步编程>(入门)的第二章内容,主要是与大家共同深入探讨下异步方法. 本 ...
- [C#] 走进异步编程的世界 - 剖析异步方法(下)
走进异步编程的世界 - 剖析异步方法(下) 序 感谢大家的支持,这是昨天发布<走进异步编程的世界 - 剖析异步方法(上)>的补充篇. 目录 异常处理 在调用方法中同步等待任务 在异步方法中 ...
- [C#] 走进异步编程的世界 - 在 GUI 中执行异步操作
走进异步编程的世界 - 在 GUI 中执行异步操作 [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5877042.html 序 这是继<开始接 ...
- 并发编程 01—— ThreadLocal
Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...
- 走进异步编程的世界 - 开始接触 async/await
[C#] 走进异步编程的世界 - 开始接触 async/await 走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 C# 5.0 引入的 async ...
- iOS开发资源整理【01】
一.网站 Code4App 开发者常用库分享网站 GitHub git是一个优秀的分布式版本控制系统 stackoverflow 技术在线问答网站 CocoaChi ...
- iOS开发Swift篇(01) 变量&常量&元组
iOS开发Swift篇(01) 变量&常量&元组 说明: 1)终于要写一写swift了.其实早在14年就已经写了swift的部分博客,无奈时过境迁,此时早已不同往昔了.另外,对于14年 ...
随机推荐
- onclicklistener到底怎么用?
转载地址:http://blog.csdn.net/dickren123/article/details/7216975 相信很多像我一样的新手学习Android开发会遇到这个问题,通过这几天的归类和 ...
- Asp.Net获取IP的方法
服务端: //方法一 HttpContext.Current.Request.UserHostAddress; //方法二 HttpContext.Current.Request.ServerVari ...
- win8下安装ubuntu双系统
终于成功在win8下安装成功ubuntu13.10, 安装方法来源于http://forum.ubuntu.org.cn/viewtopic.php?t=446557 下面的文件是该楼主的将安装ubu ...
- [转载]word尾注插入参考文献——前人经验+自己总结
1. 以尾注的方式插入第一个参考文献. 将光标定位于word文档中将要插入参考文献的位置,按“插入/引用/脚注和尾注”.出现一菜单,选择“尾注”,“文档结尾”,编号格式为“1,2,3”.按“插入”按钮 ...
- leetcode 104
104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...
- jsp-status 404错误的解决方法汇总
接下来的解决方法实在一下情况下进行的: 1.tomcat配置是对的,能打开tomcat的主页(网址:http://localhost:8080/),如图, 但是在输入具体网址的时候,例如:http:/ ...
- Jquery在线引用地址:
转自:http://www.cnblogs.com/lkf18/archive/2012/12/11/2813241.html 1. 很多网站都是使用这种方式引入,客户的浏览器可能已经缓存过了 jqu ...
- Spring框架搭建遇到的问题汇总
1.The resource is not on the build path of a Java project 然后把相应的依赖加入构建路径 2.Type mismatch: cannot con ...
- Apache无法访问 Forbidden
如图: 打开 找到: <Directory "E:/wamp/www/"> # # Possible values for the Options directive ...
- 【风马一族_php】NO0_搭建web服务器
原文来自:http://www.cnblogs.com/sows/p/5977996.html (博客园的)风马一族 侵犯版本,后果自负 安装apache apache是一种B/S结构的软件,apa ...