[New learn] 设计模式思考
本文是对上文[New learn] 设计模式的思考总结
1.大框架
无论应用使用多少种设计模式和技巧,此模式都是应用的大框架。下图为本项目的基本架构图:

1.上图中大框架为经典的MVC模式。
2.Controller将View于Model分割开来。
3.Model不应该出现任何View的引用。同样的View中应该不会有任何Model的引用,哪怕是仅仅的import都不行。
4.View的数据需要将Model的数据转变成通用的基本书库结构,如字典,字符串等等。
5.View的数据请求需要向controller去取得,是通过委托(协议)
6.Controller内对于数据库,网络或者本地数据的取得封装经统一的API中,此API内部进行复杂的迭代,外部暴露给controller的只是统一的API即可。
7.对于运行时候的动态条用,可以通过NSNotificationCenter以观察者订阅消息来通信,同样不能跨Model和View。
2.小细节
a.父view删除全部子view:
[scroller.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[obj removeFromSuperview];
}];
b.KVO: 需要在销毁时候取消观察
[coverImage addObserver:self forKeyPath: context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"image"])
{
[indicator stopAnimating];
}
}
- (void)dealloc
{
[coverImage removeObserver:self forKeyPath:@"image"];
}
c.通知:需要在销毁时候取消观察
[[NSNotificationCenter defaultCenter] postNotificationName:@"BLDownloadImageNotification"
object:self
userInfo:@{@"imageView":coverImage, @"coverUrl":albumCover}];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadImage:) name:@"BLDownloadImageNotification" object:nil];
- (void)downloadImage:(NSNotification*)notification
{
UIImageView *imageView = notification.userInfo[@"imageView"];
NSString *coverUrl = notification.userInfo[@"coverUrl"];
imageView.image = [persistencyManager getImage:[coverUrl lastPathComponent]];
if (imageView.image == nil)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ), ^{
UIImage *image = [httpClient downloadImage:coverUrl];
dispatch_sync(dispatch_get_main_queue(), ^{
imageView.image = image;
[persistencyManager saveImage:image filename:[coverUrl lastPathComponent]];
});
});
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
d.单例写法:
+ (LibraryAPI*)sharedInstance
{
// 1 创建一个静态属性,属性将成为类属性,用于记录当前实例
static LibraryAPI *_sharedInstance = nil;
// 2 创建一次性执行标记位,确保第三步骤只会被执行一次
static dispatch_once_t oncePredicate;
// 3 使用GCD方法来执行初始化方法,此方法将根据oncePredicate来执行,值执行一次
dispatch_once(&oncePredicate, ^{
_sharedInstance = [[LibraryAPI alloc] init];
});
return _sharedInstance;
}
e.配置变量保存和读取:
- (void)saveCurrentState
{
// When the user leaves the app and then comes back again, he wants it to be in the exact same state
// he left it. In order to do this we need to save the currently displayed album.
// Since it's only one piece of information we can use NSUserDefaults.
[[NSUserDefaults standardUserDefaults] setInteger:currentAlbumIndex forKey:@"currentAlbumIndex"];
[[LibraryAPI sharedInstance] saveAlbums];
}
- (void)loadPreviousState
{
currentAlbumIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"currentAlbumIndex"];
[self showDataForAlbumAtIndex:currentAlbumIndex];
}
f.图片读取:
- (void)saveImage:(UIImage*)image filename:(NSString*)filename
{
filename = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@", filename];
NSData *data = UIImagePNGRepresentation(image);
[data writeToFile:filename atomically:YES];
}
- (UIImage*)getImage:(NSString*)filename
{
filename = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@", filename];
NSData *data = [NSData dataWithContentsOfFile:filename];
return [UIImage imageWithData:data];
}
[New learn] 设计模式思考的更多相关文章
- [New learn] 设计模式
本文翻译自:http://www.raywenderlich.com/46988/ios-design-patterns iOS设计模式 - 你可能听到过这个术语,但是你知道是什么意思吗?虽然大多数的 ...
- 全栈设计模式套餐MVVM, RESTful, MVC的历史探索
众所周知, 软件开发时遵守一个规范的设计模式非常重要, 学习行业内主流的design pattern往往能够为你节省大部分时间. 根据我2年的全栈经验, 在Web应用程序领域最流行的, 并且若干年内不 ...
- [New learn]SDWebImage框架的基本使用
代码:https://github.com/xufeng79x/SDWebImage 1.简介 SDWebImage是一个第三方框架,它能够帮助我们有效管理应用图片下载,沙盒保存和内存保存的任务.通过 ...
- JSON转Model内部实现解析
一.思路: 1.通过模型类型获得所有的属性和其类型 2.对获得的json进行处理.类型处理 3.考虑字典键值和模型属性名不一致的情况 4.添加code用于归档 5.补充JSON转字典.字典转JSON. ...
- BUAA-OO-第一单元总结
BUAA-OO第一单元博客总结 第一次作业总结 (1)类关系图 第一次作业类图关系简单,仅有一个Poly封装类以及一个Main主类调用Poly,Poly封装类内部完成了包括对象构造,求导,生成字符串的 ...
- justjavac(迷渡)知乎live--<<前端工程师的入门与进阶>>听讲总结
知乎听讲总结 知乎live----jjc<前端工程师的入门进阶> git地址 内容 前端的基础知识,计算机专业基础知识感觉还行.前端后台都有做过,现在觉得自己要深入.但是只看框架源码和自己 ...
- Atitit 设计模式的本质思考】
Atitit 设计模式的本质思考] 1. 世界就是有模式构建的1 1.1. 多次模式与偶然模式1 1.2. 模式就是在一种场合下对某个问题的一个解决方案."1 1.3. 模式需要三样东西. ...
- 2018/2/14 设计模式学习笔记(一) 自己实现ArrayList,LinkedList和Iterator,以及在此过程中对于面向对象,面向接口,还有抽象类的一些思考感悟
因为本人目前为止学习编程不过七个月,所以后面的感悟对于一些大神来说可能嗤之以鼻,但对于一些刚刚入门的萌新来说在理解面向对象的思想上,以及抽象类和接口应该怎么设计等方面应该还是会有所帮助的 首先我们定义 ...
- php各种设计模式简单实践思考
前言 我一直觉得什么框架,版本,甚至语言对于一个coder来说真的不算什么,掌握一个特别高大上的一个框架或者是一个新的,少众的语言真的不算什么,因为你可以,我要花时间也可以,大家都是这样的.所以基本的 ...
随机推荐
- BZOJ2226 & SPOJ5971:LCMSum——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2226 题目大意:给定一个n,求lcm(1,n)+lcm(2,n)+……+lcm(n,n). ———— ...
- HDOJ.1263 水果(map)
水果 点我跳转到题面 点我一起学习STL-MAP 题意分析 给出多组测试数据,每组数据有多条信息.分别是水果种类,地点,和水果数目.每组信息要按照样例输出,并且输出要按照地点->水果种类的字典序 ...
- mysql 读写分离实现资料
以下很多链接需要 FQ才能看到,稍后会整理翻译成中文! Easy Read/Write Splitting with PHP’s MySQLnd https://blog.engineyard.com ...
- atom的快捷键,你hold住吗?
命令面板:Ctrl+Shift+P设置窗口:Ctrl+,另存为: Ctrl+Shift+S添加文件:Ctrl+O添加文件夹:Ctrl+Alt+O工程内查找 :Cmd+Shift+F a,m,delet ...
- Android Json解析与总结
一.JSON定义 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Progra ...
- mysql 读写分离,主从同步 理论
mysql主从复制中:第一步:master记录二进制日志.在每个事务更新数据完成之前,master在二进制日志记录这些改变.MySQL将事务写入二进制日志,即使事务中的语句都是交叉执行的.在事件写入二 ...
- String类的用法
练习如何创建一个类. package create; public class Newstring { String aa; public Newstring() { // TODO Auto-gen ...
- [Coding Practice] Maximum number of zeros in NxN matrix
Question: Input is a NxN matrix which contains only 0′s and 1′s. The condition is no 1 will occur in ...
- js遇到问题汇总
1.原生js获取同级的兄弟节点 <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...
- ZooKeeper Java例子(六)
A Simple Watch Client 为了向你介绍ZooKeeper Java API,我们开发了一个非常简单的监视器客户端.ZooKeeper客户端监视一个ZooKeeper节点的改变并且通过 ...