IOS设计模式之二(门面模式,装饰器模式)



- #import "Album.h"
- - (NSArray*)getAlbums;
- - (void)addAlbum:(Album*)album atIndex:(int)index;
- - (void)deleteAlbumAtIndex:(int)index;
- #import "PersistencyManager.h"
- #import "HTTPClient.h"
- @interfaceLibraryAPI () {
- PersistencyManager *persistencyManager;
- HTTPClient *httpClient;
- BOOL isOnline;
- }
- @end
- - (id)init{
- self = [super init];
- if (self) {
- persistencyManager = [[PersistencyManager alloc] init];
- httpClient = [[HTTPClient alloc] init];
- isOnline = NO;
- }
- return self;
- }
- -(NSArray*)getAlbums
- {
- return [persistencyManager getAlbums];
- }
- - (void)addAlbum:(Album*)album atIndex:(int)index
- {
- [persistencyManager addAlbum:album atIndex:index];
- if (isOnline)
- {
- [httpClient postRequest:@"/api/addAlbum" body:[album description]];
- }
- }
- - (void)deleteAlbumAtIndex:(int)index
- {
- [persistencyManager deleteAlbumAtIndex:index];
- if (isOnline)
- {
- [httpClient postRequest:@"/api/deleteAlbum" body:[@(index) description]];
- }
- }



- - (NSDictionary*)tr_tableRepresentation;
- - (NSDictionary*)tr_tableRepresentation
- {
- return @{@"titles":@[@"Artist", @"Album", @"Genre", @"Year"],
- @"values":@[self.artist, self.title, self.genre, self.year]};
- }

- #import "LibraryAPI.h"
- #import "Album+TableRepresentation.h"
- @interfaceViewController () {
- UITableView *dataTable;
- NSArray *allAlbums;
- NSDictionary *currentAlbumData;
- int currentAlbumIndex;
- }
- @end
- @interface ViewController () <UITableViewDataSource, UITableViewDelegate> {
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // 1
- self.view.backgroundColor = [UIColor colorWithRed:0.76f green:0.81f blue:0.87f alpha:1];
- currentAlbumIndex = 0;
- //2
- allAlbums = [[LibraryAPI sharedInstance] getAlbums];
- // 3
- // the uitableview that presents the album data
- dataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, self.view.frame.size.width, self.view.frame.size.height-120) style:UITableViewStyleGrouped];
- dataTable.delegate = self;
- dataTable.dataSource = self;
- dataTable.backgroundView = nil;
- [self.view addSubview:dataTable];
- }
- - (void)showDataForAlbumAtIndex:(int)albumIndex
- {
- // defensive code: make sure the requested index is lower than the amount of albums
- if (albumIndex < allAlbums.count)
- {
- // fetch the album
- Album *album = allAlbums[albumIndex];
- // save the albums data to present it later in the tableview
- currentAlbumData = [album tr_tableRepresentation];
- }
- else
- {
- currentAlbumData = nil;
- }
- // we have the data we need, let's refresh our tableview
- [dataTable reloadData];
- }
- [self showDataForAlbumAtIndex:currentAlbumIndex];

- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return [currentAlbumData[@"titles"] count];
- }
- - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
- if (!cell)
- {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
- }
- cell.textLabel.text = currentAlbumData[@"titles"][indexPath.row];
- cell.detailTextLabel.text = currentAlbumData[@"values"][indexPath.row];
- return cell;
- }

IOS设计模式之二(门面模式,装饰器模式)的更多相关文章
- 深入探索Java设计模式(三)之装饰器模式
装饰器模式使你可以在运行时使用类似于对象组成的技术来装饰类.这在我们希望实例化具有新职责的对象而无需对基础类进行任何代码更改的情况下尤其有用.本文是在学习完优锐课JAVA架构VIP课程—[框架源码专题 ...
- Java设计模式(七)Decorate装饰器模式
一.场景描述 (一)问题 系统中最初使用Crystal Report(水晶报表)工具生成报表,并将报表发送给客户端查看,此时定义一CrystalReport工具类即可完成水晶报表的生成工作. 后续报表 ...
- Java设计模式之(七)——装饰器模式
1.什么是装饰器模式? Attach additional responsibilities to an object dynamically keeping the same interface.D ...
- Decorator模式 装饰器模式
Android 使用了装饰器模式 1. 概述 若你从事过面向对象开发,实现给一个类或对象增加行为,使用继承机制,这是所有面向对象语言的一个基本特性.如果已经存在的一个类缺少某些方法,或者须要给方法添加 ...
- Python3-设计模式-装饰器模式
装饰器模式 动态的给原有对象添加一些额外的职责,面向切面编程(AOP),多用于和主业务无关,但又必须的业务,如:登录认证.加锁.权限检查等 Python代码实现示例 需求点: 1.在old_func( ...
- 十二、Decorator 装饰器模式
设计: 代码清单: Display public abstract class Display { public abstract int getColumns(); public abstract ...
- 设计模式总结篇系列:装饰器模式(Decorator)
在面向对象设计过程中,经常会遇到需要对现有的类的功能进行扩展,通常我们可以采用继承的方式.例如老罗最近在做手机,一开始需要定义手机所应具有的功能: interface Phone{ public vo ...
- Java设计模式12:装饰器模式
装饰器模式 装饰器模式又称为包装(Wrapper)模式.装饰器模式以多客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰器模式的结构 通常给对象添加功能,要么直接修改对象添加相应的功能, ...
- python设计模式之装饰器模式
装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...
随机推荐
- 未能加载文件或程序集"Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad
问题: 未能加载文件或程序集"Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3 ...
- ARM 汇编指令
ARM汇编程序特点: l 所有运算处理都是发生通用寄存器(一般是R0~R14)的之中.所有存储器空间(如C语言变量的本质就是一个存储器空间上的几个BYTE).的值的处理,都是要传送到通 ...
- eclipse引入tomcat
1.默认大家的tomcat都已经安装好了,这里打开eclipse,选择windows,下面的属性(preference). 2.进入到属性配置页,选择server -- runtime Envi ...
- 用eclipse创建maven项目
Maven是基于项目对象模型(POM),也可以进行模块化开发.并且是个强大的管理工具.本经验用eclipse来创建maven项目 步骤: 1.下载并正确安装eclipse 2.在eclipse上成功安 ...
- jmeter 异步子请求测试随笔
好久没写技术类的博客了,都不知道自己都在忙啥.... 最近陆续遇到了一些异步子请求的测试需求,比如打开某一个页面A,A页面里的js会再调用B,C,D,E等请求,针对这个页面的测试,我最近做了一些思考: ...
- sql主键的一点重要理解
sql只会读取数据,不会自动设置主键,所以绑定数据后要设置主键(前台) 不管是int或者uniqueidentifier只要类型对得上就可以用,int自增其实没什么太大优势(但是通常都会用自增来做,从 ...
- 计算机学院2014年“新生杯”ACM程序设计大赛
1440: 棋盘摆车问题 对于输入n,k: 1.当n<k时,无满足的摆法 2.否则 第一个车可以排n*n个位置(即整个棋盘),第二个可排(n-1)*(n-1)个位置,…… 正如排列组合一样,车与 ...
- linux下使用crontab定时备份MYSQL数据库的方法:
摘要 linux下使用crontab定时备份MYSQL数据库的方法: 只需按照下面3步做,一切都在你的掌控之下: 第一步:在服务器上配置备份目录代码: ------------------------ ...
- CSS3通配符
在 CSS3 中,追加了三个属性选择器分别为: [att*=val] ----内容包含 [att^=val] ----开头匹配 [att$=val] ----结尾匹配 示例: <!DOCTYPE ...
- 【解惑】Java动态绑定机制的内幕
在Java方法调用的过程中,JVM是如何知道调用的是哪个类的方法源代码? 这里面到底有什么内幕呢? 这篇文章我们就将揭露JVM方法调用的静态(static binding) 和动态绑定机制(auto ...