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)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...
随机推荐
- MyEclipse — Maven+Spring+Struts+Hibernate 整合 [学习笔记-4]
引入Hibernate 在pom.xml中加入jar包引用 <!-- hibernate4 --> <dependency> <groupId>org.hibern ...
- Things about single men that women hate
Things about single men that women hate为何你俘获不了女神的心?If you listen in to a group of single women talki ...
- CentOS如何开启ssh远程连接
假设VPS采用centos,再假设用较新版本6.5. VPS上可能没有安装桌面,但一般来说都会安装ssh,并且防火墙默认开放22端口. 那就从ssh开始. # 安装ssh,默认已安装好 # yum i ...
- k序数组排序
这道题,已知一个数组a,a[i]排好序后位于a[i-k]跟a[i+k]之间,问说该怎么最快地排序. 可以转换成多路归并问题, a[0]<a[k+1]<a[2k+2]<a[3k+3]. ...
- Javascript performance
I just went through some vedio related to javascript performance which is great, Here is the notes I ...
- Test Tools
1. http://www.dummytextgenerator.com/: Generate dummy text 2. fsutil file createnew D:\New.txt 1024: ...
- Java 字符串拼接方式
import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.StringUtils; impor ...
- [原]武大预选赛F题-(裸并查集+下标离散化+floyd最短路)
Problem 1542 - F - Countries Time Limit: 1000MS Memory Limit: 65536KB Total Submit: 266 Accepted: 36 ...
- NDK(10)Android.mk各属性简介,Android.mk 常用模板
参考 : http://blog.csdn.net/hudashi/article/details/7059006 本文内容: Android.mk简介, 各属性表, 常用Android.mk模板 1 ...
- [HIHO1322]树结构判定(并查集)
题目链接:http://hihocoder.com/problemset/problem/1322 给一个图,判断这个图是不是一棵树. 判定的方法:首先是连通图,其次所有点的入度都小于等于1. /* ...