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)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...
随机推荐
- ExtJs之Panel基本布局
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- HDU5568/BestCoder Round #63 (div.2) B.sequence2 dp+高精度
sequence2 Problem Description Given an integer array bi with a length of n, please tell me how many ...
- 凌乱的yyy
题目背景 快noip了,yyy很紧张! 题目描述 现在各大oj上有n个比赛,每个比赛的开始.结束的时间点是知道的. yyy认为,参加越多的比赛,noip就能考的越好(假的) 所以,他想知道他最多能参加 ...
- php脚本的执行过程(编译与执行相分离)
php脚本的执行过程(编译与执行相分离) 深入理解PHP代码的执行的过程 PHP程序的执行流程 Apache + PHP 的并发访问
- Linux系统新手学习的11点建议
随着Linux应用的扩展许多朋友开始接触Linux,根据学习Windwos的经验往往有一些茫然的感觉:不知从何处开始学起.这里介绍学习Linux的一些建议. 一.从基础开始:常常有些朋友在Linux论 ...
- CentOS系统配置redis
1.切换到/usr/sr cd /usr/src wget http://download.redis.io/releases/redis-3.2.0.tar.gz 2.解压,安装 tar x ...
- Struts2笔记——文件上传
文件上传概述 * 要想使用HTML 表单上传一个或多个文件, 必须把 HTML 表单的 enctype 属性设置为multipart/form-data,把它的 method 属性设置为post * ...
- IDL基础
先列后行 arr=indgen(3,4) SIZE(arr,/dimensions) print ,where(arr gt7) print,arr[where(arr gt 7)] print,wh ...
- 【重走Android之路】【Java面向对象基础(二)】细说String、StringBuffer和StringBuilder
[重走Android之路][基础篇(二)][Java面向对象基础]细说String.StringBuffer和StringBuilder 1.String String是Java中的一个final ...
- 如何在github上展示作品——为你的项目生成一个快速访问的网址如(DaisyWang88.github.io)
(这里值针对Windos系统的,因为本人用的是Window系统,暂时没有条件在其他平台上测试) 1.创建命名为 <userName>.github.io的仓库. 这里的u ...