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;
- }
转载于:https://www.cnblogs.com/sanjianghuiliu/p/3663918.html
IOS设计模式之二(门面模式,装饰器模式)相关推荐
- 设计模式(二)之装饰器模式
定义一个代表被装饰事物的接口: public interface Coffee {public String getCoffer();public double getPrice(); } 最初的具体 ...
- Java设计模式之结构型:装饰器模式
一.什么是装饰器模式: 当需要对类的功能进行拓展时,一般可以使用继承,但如果需要拓展的功能种类很繁多,那势必会生成很多子类,增加系统的复杂性,并且使用继承实现功能拓展时,我们必须能够预见这些拓展功能, ...
- 大聪明教你学Java设计模式 | 第七篇:装饰器模式
前言 大聪明在写代码的过程中发现设计模式的影子是无处不在,设计模式也是软件开发人员在软件开发过程中面临的一般问题的解决方案.大聪明本着"独乐乐不如众乐乐"的宗旨与大家分享一下设计模 ...
- 设计模式学习(三)——装饰器模式
前言 距离上一次正儿八经地写随笔已经有一段时间了,虽然2月10号有一篇关于泛型的小记,但是其实只是简单地将自己的学习代码贴上来,为了方便后续使用时查阅,并没有多少文字和理解感悟.之所以在今天觉得有必要 ...
- 从未这么明白的设计模式(三):装饰器模式
本文原创地址:jsbintask的博客(食用效果最佳),转载请注明出处! 同系列文章: 从未这么明白的设计模式(二):观察者模式 从未这么明白的设计模式(一):单例模式 前言 装饰器模式是为了运行时动 ...
- 设计模式(二)(装饰者模式、工厂方法模式、抽象工厂模式)
三:装饰者模式 装饰者模式动态地将责任附加到对象上.若要扩展功能,装饰者提供了比继承更有弹性的替代方案. 意图:动态地给一个对象添加一些额外的职责.就增加功能来说,装饰器模式相比生成子类更为灵活. 主 ...
- java设计模式(结构)--组合模式装饰器模式
一.组合模式 用途:将对象组合成树形结构以表示 "部分--整体" 的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 案例:在中文中,一句话是由词语组成的 ...
- 装饰器模式与代理模式的区别_JS设计模式(三):装饰器模式、代理模式
Do more 做的更多,比你上级给你安排的任务! 前言 在<不止代码>中提到了几个程序员典型的思维误区: 「 拜大牛为师 」「 业务代码一样很牛逼 」「 上班太忙没时间学习 」 我之前也 ...
- 设计模式系列:搞懂装饰器模式,增加自身技能
装饰器模式的定义:指在不改变原有对象的基础上,动态地给该对象增加一些额外的功能的模式,属于对象结构型模式. 装饰器模式的核心在于类的扩展,通过装饰器模式可以透明且动态的扩展类的功能. 装饰器模式的结构 ...
- 设计模式-结构模式-装饰器模式 decorater
装饰器模式:抽象构件 具体构件 抽象装饰 具体装饰 主要是在原结构不动的前提下 去增强功能 动态增强 zom package com.qf.decorator;//抽象构件 public interf ...
最新文章
- 去哪儿网产品经理的专属心得:产品经理的核心价值
- 三维重建:SLAM的粒度和工程化问题
- 什么是Google Play保护以及如何确保Android安全?
- 日本的危机感:想战胜中美,要举全国之力培养AI人才
- Linux 集群搭建
- 虚拟机安装win10专业版
- HTML 中划线 、 下划线 、上划线
- ra_customer_trx_all 的XLA会计科目
- Ubuntu Linux全方位学习,哪一种Ubuntu官方版本适合你?
- 腾讯开源开门红,云原生操作系统TencentOS Kernel正式登陆Github
- 支付宝小程序登录 -tp
- python计算球体体积_鬼斧神工:求n维球的体积
- 京东商品图片 自动下载 抓取 c# 爬虫
- *p++、*(p++)、(*p)++、*++p、++*p的区别
- 3D模型欣赏:白发小恶魔女 次世代角色设计 毛发细腻
- 基于物理的渲染PBR(二):挑战手写pbr和IBL环境光部分的见解
- 最新体感:超级街霸4
- PHP开发的微信公众平台消息接口具体流程
- ROS系列:常用组件(一)
- 电子发票的板式文件服务器地址是什么,税务Ukey电子发票配置菜单参数设置操作指南.pdf...