Skip to content

Instantly share code, notes, and snippets.

@jkyin
Created October 24, 2014 02:10
Show Gist options
  • Save jkyin/c4a1ec4908ec241c32a7 to your computer and use it in GitHub Desktop.
Save jkyin/c4a1ec4908ec241c32a7 to your computer and use it in GitHub Desktop.
iOS Design Patterns

iOS 设计模式 - 你可能很早就听过这个术语,但你知道它的具体含义吗?大部分开发者可能都认为设计模式非常重要,然而关于此主题的文章少之又少并且我们开发者在写代码时很少关注设计模式。

设计模式是指一些能够在软件设计的过程中解决常见问题的同时能够重复使用的解决方案。它们是一套设计好了的模版,旨在帮助你写代码时非常容易的理解和复用。它们还能帮你创建松耦合代码,如此一来你就能够不付吹灰之力地修改或替换代码中的模块。

如果你刚了解设计模式,那我正好有一些对你有用的消息!首先,你早就用过许多 iOS 设计模式,这要感谢 Cocoa 的构建方式并且最好的学习方式就是不停的使用它们。第二,本教程会让你了解 Cocoa 中用到的所有主要常见的 iOS 设计模式。

本教程分为好几个部分,一个设计模式为一个部分。在每部分中,你会看到以下这些说明:

  • 这个设计模式叫什么。

  • 为什么你应该使用它。

  • 如何使用,什么情况下才适合使用,和使用这个设计模式时的常见误区。

在这个教程中,你将会创建一个「音乐库应用」,它能显示你的专辑列表和相关的信息。

开发这款 app 的过程中,你会对大部分常见的 Cocoa 设计模式更熟悉。

  • 创建型模式:单例和抽象工厂。

  • 结构型模式:MVC 模式,桥接模式,适配器模式,外观模式和组合模式。

  • 行为型模式:观察者模式,备忘录,责任链和命令

不要被上面的误导了,以为这是篇理论型文章;你会在你的音乐 app 中大量使用这些设计模式的。在本教程的最后你的 app 看起来会是这样:

Getting Started

下载初始工程,解压 zip 文件,然后打开在 Xcode 中 BlueLibrary.xcodeproj

那里没有多少内容,仅仅是些默认的 ViewController 和没有实现代码的简单 HTTP 客户端。

提示:你知道在你创建新的工程时你的代码早就用到了设计模式吗? MVC,委托,协议,单例 - 什么都不用做就得到了!:]

在你进入第一个设计模式之前,你必须创建两个类来储存和显示专辑数据。

导航至 “File\New\File…”(或按下 Command+N 快捷键)。选中 iOS > Cocoa Touch 接着是 Objective-C class 然后单击 Next。设置类名为 Album 和继承自 NSObject。单击 NextCreate

打开 Album.h 接着在 @interface@end 中添加以下属性和方法原型:

@property (nonatomic, copy, readonly) NSString *title, *artist, *genre, *coverUrl, *year;
@property (nonatomic, copy, readonly) NSString *title, *artist, *genre, *coverUrl, *year;
 
- (id)initWithTitle:(NSString*)title artist:(NSString*)artist coverUrl:(NSString*)coverUrl year:(NSString*)year;

注意所有的属性是 readonly,因为在 Album 对象被创建后不需要改变它们。

这个方法是一个对象初始化程序,当你创建一个新的专辑时,你会传入专辑名,艺术家,专辑封面 URL,和年份。

现在打开 Album.m 并在 @implementation@end 中添加下面的代码:

- (id)initWithTitle:(NSString*)title artist:(NSString*)artist coverUrl:(NSString*)coverUrl year:(NSString*)year
{
    self = [super init];
    if (self)
    {
        _title = title;
        _artist = artist;
        _coverUrl = coverUrl;
        _year = year;
        _genre = @"Pop";
    }
    return self;
}

这里没什么独特的东西;就是些 init 方法来创建 Album 实例。

再次选择 File\New\File… 选中 Cocoa Touch 然后是 Objective-C class 接着单击 Next。设置类名为 ``,但是这一次把继承类设为 UIView。单击 `Next` 然后是 `Create`。

注意:如果你觉得键盘快捷键用起来更方便,那就可以直接使用 Command+N 来创建一个新文件,Command+Option+N 会创建一个新组,Command+B 可以直接运行程序。

打开 AlbumView.h 然后在 @interface@end 之间添加以下方法原型:

- (id)initWithFrame:(CGRect)frame albumCover:(NSString*)albumCover;

现在打开 AlbumView.m 然后用下面的代码替换所有 @implementation 之间的代码:

@implementation AlbumView
{
    UIImageView *coverImage;
    UIActivityIndicatorView *indicator;
}
 
- (id)initWithFrame:(CGRect)frame albumCover:(NSString*)albumCover
{
    self = [super initWithFrame:frame];
    if (self)
    {
 
        self.backgroundColor = [UIColor blackColor];
        // the coverImage has a 5 pixels margin from its frame
        coverImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, frame.size.width-10, frame.size.height-10)];
        [self addSubview:coverImage];
 
        indicator = [[UIActivityIndicatorView alloc] init];
        indicator.center = self.center;
        indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
        [indicator startAnimating];
        [self addSubview:indicator];
    }
    return self;
}
@end

这里你需要引起注意的第一件事是,这儿有一个叫 coverImage 的实例变量。这个变量代表专辑封面图片。第二个变量是一个指示器,表示当专辑封面被下载过程中会有一个旋转的活动指示器。

在这个初始化程序的实现部分,你设置了背景色为黑色,创建了一个带有 5 个像素外边距的 UIImageVIew 还有一个活动指示器。

注意:想要知道私有变量为什么会在实现文件中定义而不是在借口部分?这是因为 AlbumView 类的外部没有其他类需要知道这些变量的存在,它们只会在类的内部函数中被使用。如果你要创建一个二进制库或框架来给其他开发者使用的话,这个约定非常重要。

构建你的工程(Command+B)看看是否一切如期进行。一切良好?下面准备好你的第一个设计模式吧!:]

MVC - 设计模式之王

image

模型视图控制器(MVC)是 Cocoa 的构建模块之一并且毋庸置疑是所有设计模式中使用最广泛的。它根据你应用中各文件对象所扮演的角色来分门别类的归类,并且鼓励以任务角色来简洁高效地进行代码分离。这三个角色是:

  • 模型:这个对象是指那些保存你应用的数据和定义怎样操作数据的文件。例如,在你应用中模型是你的 Album 类。
  • 视图:这些文件是用来管理模型文件和用户交互控件的可视化呈现。基本上,所有的 UIView 和他们的子类都是视图。在你的应用中 AlbumView 类表示视图。
  • 控制器:控制器扮演一个协调者的角色来协调所有的工作。它从模型文件访问数据并在视图上显示出来,如有必要还会监听事件和修改数据。你能猜到哪个类是控制器类吗?对了,就是:ViewController

在你应用中实现这种设计模式的好处就是能够让你的每一个文件都归为某一组。视图和模型之间通过控制器来交流,用下面的图表可以更好的描述:

mvc0

模型会通知控制器一些数据被修改了,然后依次,控制器在视图上更新数据。在用户执行某些操作的时候视图能够通知控制器,并且如有必要控制器要么会更新模型要么检索任何请求的数据。

你可能会想知道为什么你不能剖开控制器,然后在视图和模型中用同样的类来实现,因为这样看起来更简单。

这归根结底就是为了代码的分离和复用。理想上,视图应该完全从模型中分离出来。如果视图不再依赖模型中特定的实现,那么它就能够在不同模型之间重复使用,来呈现一些其他的数据。

举例说明,如果在以后你也想要添加电影和书籍到你的资料库中,那你仍可以使用这一样的 AlbumView 来显示你的电影和书籍对象。此外,如果你想要创建一个新的对象,它们会用到专辑,你同样能够简单的重复使用你的 Album 类,因为他不依赖任何视图。这就是 MVC 的强大之处!

如何使用 MVC 设计模式

首先,你需要确保项目中的每一个类文件要么是控制器,要么是模型或者要么是视图。不要把两种角色的功能都写到一个类文件里了。其实通过创建 AlbumAlbumView 这两个类时你就做到了我所说的。

第二,为了确保你按照这种工作方式来进行,你应该创建三个分组来保存代码。现在让我们新建三个名为 Modle,View,Contorller 文件夹。

Album.hAlbum.m 拖到 Model 分组。把 AlbumView.hAlbumView.m 拖到 View 分组,最后把 ViewController.hViewController.m 拖到 Controller 分组。

现在工程看起来就是这样的了:

image

Your project already looks a lot better without all those files floating around. Obviously you can have other groups and classes, but the core of the application is contained in these three categories. Now that your components are organized, you need to get the album data from somewhere. You’ll create an API class to use throughout your code to manage the data — which presents an opportunity to discuss your next design pattern — the Singleton. The Singleton Pattern The Singleton design pattern ensures that only one instance exists for a given class and that there’s a global access point to that instance. It usually uses lazy loading to create the single instance when it’s needed the first time. Note: Apple uses this approach a lot. For example: [NSUserDefaults standardUserDefaults], [UIApplication sharedApplication], [UIScreen mainScreen], [NSFileManager defaultManager] all return a Singleton object. You’re likely wondering why you care if there’s more than one instance of a class floating around. Code and memory is cheap, right? There are some cases in which it makes sense to have exactly one instance of a class. For example, there’s no need to have multiple Logger instances out there, unless you want to write to several log files at once. Or, take a global configuration handler class: it’s easier to implement a thread-safe access to a single shared resource, such as a configuration file, than to have many class modifying the configuration file possibly at the same time. How to Use the Singleton Pattern Take a look at the diagram below: singleton The above image shows a Logger class with a single property (which is the single instance), and two methods: sharedInstance and init. The first time a client sends the sharedInstance message, the property instance isn’t yet initialized, so you create a new instance of the class and return a reference to it. The next time you call sharedInstance, instance is immediately returned without any initialization. This logic promises that only one instance exists at all times. You’ll implement this pattern by creating a singleton class to manage all the album data. You’ll notice there’s a group called API in the project; this is where you’ll put all the classes that will provide services to your app. Inside this group create a new class with the iOS\Cocoa Touch\Objective-C class template. Name the class LibraryAPI, and make it a subclass of NSObject. Open LibraryAPI.h and replace its contents with the following: @interface LibraryAPI : NSObject

  • (LibraryAPI*)sharedInstance;

@end Now go to LibraryAPI.m and insert this method right after the @implentation line:

  • (LibraryAPI*)sharedInstance { // 1 static LibraryAPI *_sharedInstance = nil;

    // 2 static dispatch_once_t oncePredicate;

    // 3 dispatch_once(&oncePredicate, ^{ _sharedInstance = [[LibraryAPI alloc] init]; }); return _sharedInstance; } There’s a lot going on in this short method: Declare a static variable to hold the instance of your class, ensuring it’s available globally inside your class. Declare the static variable dispatch_once_t which ensures that the initialization code executes only once. Use Grand Central Dispatch (GCD) to execute a block which initializes an instance of LibraryAPI. This is the essence of the Singleton design pattern: the initializer is never called again once the class has been instantiated. The next time you call sharedInstance, the code inside the dispatch_once block won’t be executed (since it’s already executed once) and you receive a reference to the previously created instance of LibraryAPI. Note: To learn more about GCD and its uses, check out the tutorials Multithreading and Grand Central Dispatch and How to Use Blocks on this site. You now have a Singleton object as the entry point to manage the albums. Take it a step further and create a class to handle the persistence of your library data. Create a new class with the iOS\Cocoa Touch\Objective-C class template inside the API group. Name the class PersistencyManager, and make it a subclass of NSObject. Open PersistencyManager.h. Add the following import to the top of the file: #import "Album.h" Next, add the following code to PersistencyManager.h after the @interface line:

  • (NSArray*)getAlbums;

  • (void)addAlbum:(Album*)album atIndex:(int)index;

  • (void)deleteAlbumAtIndex:(int)index; The above are prototypes for the three methods you need to handle the album data. Open PersistencyManager.m and add the following code right above the @implementation line: @interface PersistencyManager () { // an array of all albums NSMutableArray *albums; } The above adds a class extension, which is another way to add private methods and variables to a class so that external classes will not know about them. Here, you declare an NSMutableArray to hold the album data. The array’s mutable so that you can easily add and delete albums. Now add the following code implementation to PersistencyManager.m after the @implementation line:

  • (id)init { self = [super init]; if (self) { // a dummy list of albums albums = [NSMutableArray arrayWithArray: @[[[Album alloc] initWithTitle:@"Best of Bowie" artist:@"David Bowie" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_david%20bowie_best%20of%20bowie.png" year:@"1992"], [[Album alloc] initWithTitle:@"It's My Life" artist:@"No Doubt" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_no%20doubt_its%20my%20life%20%20bathwater.png" year:@"2003"], [[Album alloc] initWithTitle:@"Nothing Like The Sun" artist:@"Sting" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_sting_nothing%20like%20the%20sun.png" year:@"1999"], [[Album alloc] initWithTitle:@"Staring at the Sun" artist:@"U2" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_u2_staring%20at%20the%20sun.png" year:@"2000"], [[Album alloc] initWithTitle:@"American Pie" artist:@"Madonna" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_madonna_american%20pie.png" year:@"2000"]]]; } return self; } In init you populate the array with five sample albums. If the above albums aren’t to your liking, feel free to replace them with the music you enjoy. :] Now add the following three methods to PersistencyManager.m:

  • (NSArray*)getAlbums { return albums; }

  • (void)addAlbum:(Album*)album atIndex:(int)index { if (albums.count >= index) [albums insertObject:album atIndex:index]; else [albums addObject:album]; }

  • (void)deleteAlbumAtIndex:(int)index { [albums removeObjectAtIndex:index]; } These methods allow you to get, add, and delete albums. Build your project just to make sure everything still compiles correctly. At this point, you might wonder where the PersistencyManager class comes in since it’s not a Singleton. The relationship between LibraryAPI and PersistencyManager will be further explored in the next section where you’ll look at the Facade design pattern. The Facade Design Pattern facade The Facade design pattern provides a single interface to a complex subsystem. Instead of exposing the user to a set of classes and their APIs, you only expose one simple unified API. The following image explains this concept: facade2 The user of the API is completely unaware of the complexity that lies beneath. This pattern is ideal when working with a large number of classes, particularly when they are complicated to use or difficult to understand. The Facade pattern decouples the code that uses the system from the interface and implementation of the classes you’re hiding; it also reduces dependencies of outside code on the inner workings of your subsystem. This is also useful if the classes under the facade are likely to change, as the facade class can retain the same API while things change behind the scenes. For example, if the day comes when you want to replace your backend service, you won’t have to change the code that uses your API as it wont’ change. How to Use the Facade Pattern Currently you have PersistencyManager to save the album data locally and HTTPClient to handle the remote communication. The other classes in your project should not be aware of this logic. To implement this pattern, only LibraryAPI should hold instances of PersistencyManager and HTTPClient. Then, LibraryAPI will expose a simple API to access those services. Note: Usually, a singleton exists for the lifetime of the app. You shouldn’t keep too many strong pointers in the singleton to other objects, since they won’t be released until the app is closed. The design looks like the following: design-patterns-facade-uml LibraryAPI will be exposed to other code, but will hide the HTTPClient and PersistencyManager complexity from the rest of the app. Open LibraryAPI.h and add the following import to the top of the file: #import "Album.h" Next, add the following method definitions to LibraryAPI.h:

  • (NSArray*)getAlbums;

  • (void)addAlbum:(Album*)album atIndex:(int)index;

  • (void)deleteAlbumAtIndex:(int)index; For now, these are the methods you’ll expose to the other classes. Go to LibraryAPI.m and add the following two imports: #import "PersistencyManager.h" #import "HTTPClient.h" This will be the only place where you import these classes. Remember: your API will be the only access point to your “complex” system. Now, add some private variables via a class extension (above the @implementation line): @interface LibraryAPI () { PersistencyManager *persistencyManager; HTTPClient *httpClient; BOOL isOnline; }

@end isOnline determines if the server should be updated with any changes made to the albums list, such as added or deleted albums. You now need to initialize these variables via init. Add the following code to LibraryAPI.m:

  • (id)init { self = [super init]; if (self) { persistencyManager = [[PersistencyManager alloc] init]; httpClient = [[HTTPClient alloc] init]; isOnline = NO; } return self; } The HTTP client doesn’t actually work with a real server and is only here to demonstrate the usage of the facade pattern, so isOnline will always be NO. Next, add the following three methods to LibraryAPI.m:

  • (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]]; } } Take a look at addAlbum:atIndex:. The class first updates the data locally, and then if there’s an internet connection, it updates the remote server. This is the real strength of the Facade; when some class outside of your system adds a new album, it doesn’t know — and doesn’t need to know — of the complexity that lies underneath. Note: When designing a Facade for classes in your subsystem, remember that nothing prevents the client from accessing these “hidden” classes directly. Don’t be stingy with defensive code and don’t assume that all the clients will necessarily use your classes the same way the Facade uses them. Build and run your app. You’ll see an incredibly exciting empty black screen like this: Blank screen You’ll need something to display the album data on screen — which is a perfect use for your next design pattern: the Decorator. The Decorator Design Pattern The Decorator pattern dynamically adds behaviors and responsibilities to an object without modifying its code. It’s an alternative to subclassing where you modify a class’ behavior by wrapping it with another object. In Objective-C there are two very common implementations of this pattern: Category and Delegation. Category Category is an extremely powerful mechanism that allows you to add methods to existing classes without subclassing. The new methods are added at compile time and can be executed like normal methods of the extended class. It’s slightly different from the classic definition of a decorator, because a Category doesn’t hold an instance of the class it extends. Note: Besides extending your own classes, you can also add methods to any of Cocoa’s own classes! How to Use Categories Imagine a situation in which you have an Album object that you want to present inside a table view: design-patterns-category1 Where will the album titles come from? Album is a Model object, so it doesn’t care how you present the data. You’ll need some external code to add this functionality to the Album class, but without modifying the class directly. You’ll create a category that is an extension of Album; it will define a new method that returns a data structure which can be used easily with UITableViews. The data structure will look like the following: delegate2 To add a Category to Album, navigate to File\New\File… and select the Objective-C category template — don’t select the Objective-C class out of habit! Enter TableRepresentation in the Category field and Album in the Category on field. Note: Did you notice the name of the new file? Album+TableRepresentation means you’re extending the Album class. This convention is important, because it’s easier to read and it prevents a collision with other categories you or someone else might create. Go to Album+TableRepresentation.h and add the following method prototype:

  • (NSDictionary*)tr_tableRepresentation; Notice there’s a tr_ at the beginning of the method name, as an abbreviation of the name of the category: TableRepresentation. Again, conventions like this will help prevent collisions with other methods! Note: If the name of a method declared in a category is the same as a method in the original class, or the same as a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime. This is less likely to be an issue if you’re using categories with your own classes, but can cause serious problems when using categories to add methods to standard Cocoa or Cocoa Touch classes. Go to Album+TableRepresentation.m and add the following method:

  • (NSDictionary*)tr_tableRepresentation { return @{@"titles":@[@"Artist", @"Album", @"Genre", @"Year"], @"values":@[self.artist, self.title, self.genre, self.year]}; } Consider for a moment how powerful this pattern can be: You’re using properties directly from Album. You have added to the Album class but you haven’t subclassed it. If you need to sub-class Album, you can still do that too. This simple addition lets you return a UITableView-ish representation of an Album, without modifying Album‘s code. Apple uses Categories a lot in the Foundation classes. To see how they do this, open NSString.h. Find @interface NSString, and you’ll see the definition of the class together with three categories: NSStringExtensionMethods, NSExtendedStringPropertyListParsing and NSStringDeprecated. Categories help keep the methods organized and separated into sections. Delegation The other Decorator design pattern, Delegation, is a mechanism in which one object acts on behalf of, or in coordination with, another object. For example, when you use a UITableView, one of the methods you must implement is tableView:numberOfRowsInSection:. You can’t expect the UITableView to know how many rows you want to have in each section, as this is application-specific. Therefore, the task of calculating the amount of rows in each section is passed on to the UITableView delegate. This allows the UITableView class to be independent of the data it displays. Here’s a pseudo-explanation of what’s going on when you create a new UITableView: delegate The UITableView object does its job of displaying a table view. However, eventually it will need some information that it doesn’t have. Then, it turns to its delegates and sends a message asking for additional information. In Objective-C’s implementation of the delegate pattern, a class can declare optional and required methods through a protocol. You’ll cover protocols a bit later in this tutorial. It might seem easier to just subclass an object and override the necessary methods, but consider that you can only subclass based on a single class. If you want an object to be the delegate of two or more other objects, you won’t be able to achieve this by subclassing. Note: This is an important pattern. Apple uses this approach in most of the UIKit classes: UITableView, UITextView, UITextField, UIWebView, UIAlert, UIActionSheet, UICollectionView, UIPickerView, UIGestureRecognizer, UIScrollView. The list goes on and on. How to Use the Delegate Pattern Go to ViewController.m and add the following imports to the top of the file: #import "LibraryAPI.h" #import "Album+TableRepresentation.h" Now, add these private variables to the class extension so that the class extension looks like this: @interface ViewController () { UITableView *dataTable; NSArray *allAlbums; NSDictionary *currentAlbumData; int currentAlbumIndex; }

@end Then, replace the @interface line in the class extension with this one: @interface ViewController () <UITableViewDataSource, UITableViewDelegate> { This is how you make your delegate conform to a protocol — think of it as a promise made by the delegate to fulfil the method’s contract. Here, you indicate that ViewController will conform to the UITableViewDataSource and UITableViewDelegate protocols. This way UITableView can be absolutely certain that the required methods are implemented by its delegate. Next, replace viewDidLoad: with this code:

  • (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]; } Here’s a breakdown of the above code: Change the background color to a nice navy blue color. Get a list of all the albums via the API. You don’t use PersistencyManager directly! This is where you create the UITableView. You declare that the view controller is the UITableView delegate/data source; therefore, all the information required by UITableView will be provided by the view controller. Now, add the following method to ViewController.m:

  • (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]; } showDataForAlbumAtIndex: fetches the required album data from the array of albums. When you want to present the new data, you just need to call reloadData. This causes UITableView to ask its delegate such things as how many sections should appear in the table view, how many rows in each section, and how each cell should look. Add the following line to the end of viewDidLoad [self showDataForAlbumAtIndex:currentAlbumIndex]; This loads the current album at app launch. And since currentAlbumIndex was previously set to 0, this shows the first album in the collection. Build and run your project; you’ll experience a crash with the following exception displayed in the debug console: UITableView Crash What’s going on here? You declared that ViewController as the UITableView‘s delegate and data source. But in doing so, you must conform to all the required methods — including tableView:numberOfRowsInSection: — which you haven’t done yet. Add the following code to ViewController.m anywhere between the @implementation and @end lines:

  • (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; } tableView:numberOfRowsInSection: returns the number of rows to display in the table view, which matches the number of titles in the data structure. tableView:cellForRowAtIndexPath: creates and returns a cell with the title and its value. Build and run your project. Your app should start and present you with the following screen: design pattern - stage1 Things are looking pretty good so far. But if you recall the first image showing the finished app, there was a horizontal scroller at the top of the screen to switch between albums. Instead of coding a single-purpose horizontal scroller, why not make it reusable for any view? To make this view reusable, all decisions about its content should be left to another object: a delegate. The horizontal scroller should declare methods that its delegate implements in order to work with the scroller, similar to how the UITableView delegate methods work. We’ll implement this when we discuss the next design pattern. The Adapter Pattern An Adapter allows classes with incompatible interfaces to work together. It wraps itself around an object and exposes a standard interface to interact with that object. If you’re familiar with the Adapter pattern then you’ll notice that Apple implements it in a slightly different manner – Apple uses protocols to do the job. You may be familiar with protocols like UITableViewDelegate, UIScrollViewDelegate, NSCoding and NSCopying. As an example, with the NSCopying protocol, any class can provide a standard copy method. How to Use the Adapter Pattern The horizontal scroller mentioned before will look like this: protocol1 To begin implementing it, right click on the View group in the Project Navigator, select New File… and create a class with the iOS\Cocoa Touch\Objective-C class template. Name the new class HorizontalScroller and make it subclass from UIView. Open HorizontalScroller.h and insert the following code after the @end line: @protocol HorizontalScrollerDelegate // methods declaration goes in here @end This defines a protocol named HorizontalScrollerDelegate that inherits from the NSObject protocol in the same way that an Objective-C class inherits from its parent. It’s good practice to conform to the NSObject protocol — or to conform to a protocol that itself conforms to the NSObject protocol. This lets you send messages defined by NSObject to the delegate of HorizontalScroller. You’ll soon see why this is important. You define the required and optional methods that the delegate will implement between the @protocol and @end lines. So add the following protocol methods: @required // ask the delegate how many views he wants to present inside the horizontal scroller

  • (NSInteger)numberOfViewsForHorizontalScroller:(HorizontalScroller*)scroller;

// ask the delegate to return the view that should appear at

  • (UIView*)horizontalScroller:(HorizontalScroller*)scroller viewAtIndex:(int)index;

// inform the delegate what the view at has been clicked

  • (void)horizontalScroller:(HorizontalScroller*)scroller clickedViewAtIndex:(int)index;

@optional // ask the delegate for the index of the initial view to display. this method is optional // and defaults to 0 if it's not implemented by the delegate

  • (NSInteger)initialViewIndexForHorizontalScroller:(HorizontalScroller*)scroller; Here you have both required and optional methods. Required methods must be implemented by the delegate and usually contain some data that is absolutely required by the class. In this case, the required details are the number of views, the view at a specific index, and the behaviour when the view is tapped. The optional method here is the initial view; if it’s not implemented then the HorizontalScroller will default to the first index. Next, you’ll need to refer to your new delegate from within the HorizontalScroller class definition. But the protocol definition is below the class definition and so is not visible at this point. What can you do? The solution is to forward declare the protocol so that the compiler (and Xcode) knows that such a protocol will be available. To do this, add the following code above the @interface line: @protocol HorizontalScrollerDelegate; Still in HorizontalScroller.h, add the following code between the @interface and @end statements: @property (weak) id delegate;

  • (void)reload; The attribute of the property you created above is defined as weak. This is necessary in order to prevent a retain cycle. If a class keeps a strong pointer to its delegate and the delegate keeps a strong pointer back to the conforming class, your app will leak memory since neither class will release the memory allocated to the other. The id means that the delegate can only be assigned classes that conform to HorizontalScrollerDelegate, giving you some type safety. The reload method is modelled after reloadData in UITableView; it reloads all the data used to construct the horizontal scroller. Replace the contents of HorizontalScroller.m with the following code: #import "HorizontalScroller.h"

// 1 #define VIEW_PADDING 10 #define VIEW_DIMENSIONS 100 #define VIEWS_OFFSET 100

// 2 @interface HorizontalScroller () @end

// 3 @implementation HorizontalScroller { UIScrollView *scroller; }

@end Taking each comment block in turn: Define constants to make it easy to modify the layout at design time. The view’s dimensions inside the scroller will be 100 x 100 with a 10 point margin from its enclosing rectangle. HorizontalScroller conforms to the UIScrollViewDelegate protocol. Since HorizontalScroller uses a UIScrollView to scroll the album covers, it needs to know of user events such as when a user stops scrolling. Create the scroll view containing the views. Next you need to implement the initializer. Add the following method:

  • (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; scroller.delegate = self; [self addSubview:scroller]; UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollerTapped:)]; [scroller addGestureRecognizer:tapRecognizer]; } return self; } The scroll view completely fills the HorizontalScroller. A UITapGestureRecognizer detects touches on the scroll view and checks if an album cover has been tapped. If so, it notifies the HorizontalScroller delegate. Now add this mehtod:

  • (void)scrollerTapped:(UITapGestureRecognizer*)gesture { CGPoint location = [gesture locationInView:gesture.view]; // we can't use an enumerator here, because we don't want to enumerate over ALL of the UIScrollView subviews. // we want to enumerate only the subviews that we added for (int index=0; index<[self.delegate numberOfViewsForHorizontalScroller:self]; index++) { UIView *view = scroller.subviews[index]; if (CGRectContainsPoint(view.frame, location)) { [self.delegate horizontalScroller:self clickedViewAtIndex:index]; [scroller setContentOffset:CGPointMake(view.frame.origin.x - self.frame.size.width/2 + view.frame.size.width/2, 0) animated:YES]; break; } } } The gesture passed in as a parameter lets you extract the location via locationInView:. Next, you invoke numberOfViewsForHorizontalScroller: on the delegate. The HorizontalScroller instance has no information about the delegate other than knowing it can safely send this message since the delegate must conform to the HorizontalScrollerDelegate protocol. For each view in the scroll view, perform a hit test using CGRectContainsPoint to find the view that was tapped. When the view is found, send the delegate the horizontalScroller:clickedViewAtIndex: message. Before you break out of the for loop, center the tapped view in the scroll view. Now add the following code to reload the scroller:

  • (void)reload { // 1 - nothing to load if there's no delegate if (self.delegate == nil) return;

    // 2 - remove all subviews [scroller.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [obj removeFromSuperview]; }];

    // 3 - xValue is the starting point of the views inside the scroller CGFloat xValue = VIEWS_OFFSET; for (int i=0; i<[self.delegate numberOfViewsForHorizontalScroller:self]; i++) { // 4 - add a view at the right position xValue += VIEW_PADDING; UIView *view = [self.delegate horizontalScroller:self viewAtIndex:i]; view.frame = CGRectMake(xValue, VIEW_PADDING, VIEW_DIMENSIONS, VIEW_DIMENSIONS); [scroller addSubview:view]; xValue += VIEW_DIMENSIONS+VIEW_PADDING; }

    // 5 [scroller setContentSize:CGSizeMake(xValue+VIEWS_OFFSET, self.frame.size.height)];

    // 6 - if an initial view is defined, center the scroller on it if ([self.delegate respondsToSelector:@selector(initialViewIndexForHorizontalScroller:)]) { int initialView = [self.delegate initialViewIndexForHorizontalScroller:self]; [scroller setContentOffset:CGPointMake(initialView*(VIEW_DIMENSIONS+(2*VIEW_PADDING)), 0) animated:YES]; } } Stepping through the code comment-by-comment: If there’s no delegate, then there’s nothing to be done and you can return. Remove all the subviews previously added to the scroll view. All the views are positioned starting from the given offset. Currently it’s 100, but it can be easily tweaked by changing the #DEFINE at the top of the file. The HorizontalScroller asks its delegate for the views one at a time and it lays them next to each another horizontally with the previously defined padding. Once all the views are in place, set the content offset for the scroll view to allow the user to scroll through all the albums covers. The HorizontalScroller checks if its delegate responds to the initialViewIndexForHorizontalScroller: selector. This check is necessary because that particular protocol method is optional. If the delegate doesn’t implement this method, 0 is used as the default value. Finally, this piece of code sets the scroll view to center the initial view defined by the delegate. You execute reload when your data has changed. You also need to call this method when you add HorizontalScroller to another view. Add the following code to HorizontalScroller.m to cover the latter scenario:

  • (void)didMoveToSuperview { [self reload]; } The didMoveToSuperview message is sent to a view when it’s added to another view as a subview. This is the right time to reload the contents of the scroller. The last piece of the HorizontalScroller puzzle is to make sure the album you’re viewing is always centered inside the scroll view. To do this, you’ll need to perform some calculations when the user drags the scroll view with their finger. Add the following method (again to HorizontalScroller.m):

  • (void)centerCurrentView { int xFinal = scroller.contentOffset.x + (VIEWS_OFFSET/2) + VIEW_PADDING; int viewIndex = xFinal / (VIEW_DIMENSIONS+(2VIEW_PADDING)); xFinal = viewIndex * (VIEW_DIMENSIONS+(2VIEW_PADDING)); [scroller setContentOffset:CGPointMake(xFinal,0) animated:YES]; [self.delegate horizontalScroller:self clickedViewAtIndex:viewIndex]; } The above code takes into account the current offset of the scroll view and the dimensions and the padding of the views in order to calculate the distance of the current view from the center. The last line is important: once the view is centered, you then inform the delegate that the selected view has changed. To detect that the user finished dragging inside the scroll view, you must add the following UIScrollViewDelegate methods:

  • (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { if (!decelerate) { [self centerCurrentView]; } }

  • (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [self centerCurrentView]; } scrollViewDidEndDragging:willDecelerate: informs the delegate when the user finishes dragging. The decelerate parameter is true if the scroll view hasn’t come to a complete stop yet. When the scroll action ends, the the system calls scrollViewDidEndDecelerating. In both cases we should call the new method to center the current view since the current view probably has changed after the user dragged the scroll view. Your HorizontalScroller is ready for use! Browse through the code you’ve just written; you’ll see there’s not one single mention of the Album or AlbumView classes. That’s excellent, because this means that the new scroller is truly independent and reusable. Build your project to make sure everything compiles properly. Now that HorizontalScroller is complete, it’s time to use it in your app. Open ViewController.m and add the following imports: #import "HorizontalScroller.h" #import "AlbumView.h" Add HorizontalScrollerDelegate to the protocols that ViewController conforms to: @interface ViewController ()<UITableViewDataSource, UITableViewDelegate, HorizontalScrollerDelegate> Add the following instance variable for the Horizontal Scroller to the class extension: HorizontalScroller *scroller; Now you can implement the delegate methods; you’ll be amazed at how just a few lines of code can implement a lot of functionality. Add the following code to ViewController.m: #pragma mark - HorizontalScrollerDelegate methods

  • (void)horizontalScroller:(HorizontalScroller *)scroller clickedViewAtIndex:(int)index { currentAlbumIndex = index; [self showDataForAlbumAtIndex:index]; } This sets the variable that stores the current album and then calls showDataForAlbumAtIndex: to display the data for the new album. Note: It’s common practice to place methods that fit together after a #pragma mark directive. The compiler will ignore this line but if you drop down the list of methods in your current file via Xcode’s jump bar, you’ll see a separator and a bold title for the directive. This helps you organize your code for easy navigation in Xcode. Next, add the following code:

  • (NSInteger)numberOfViewsForHorizontalScroller:(HorizontalScroller*)scroller { return allAlbums.count; } This, as you’ll recognize, is the protocol method returning the number of views for the scroll view. Since the scroll view will display covers for all the album data, the count is the number of album records. Now, add this code:

  • (UIView*)horizontalScroller:(HorizontalScroller*)scroller viewAtIndex:(int)index { Album *album = allAlbums[index]; return [[AlbumView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) albumCover:album.coverUrl]; } Here you create a new AlbumView and pass it to the HorizontalScroller. That’s it! Only three short methods to display a nice looking horizontal scroller. Yes, you still need to actually create the scroller and add it to your main view but before doing that, add the following method:

  • (void)reloadScroller { allAlbums = [[LibraryAPI sharedInstance] getAlbums]; if (currentAlbumIndex < 0) currentAlbumIndex = 0; else if (currentAlbumIndex >= allAlbums.count) currentAlbumIndex = allAlbums.count-1; [scroller reload];

    [self showDataForAlbumAtIndex:currentAlbumIndex]; } This method loads album data via LibraryAPI and then sets the currently displayed view based on the current value of the current view index. If the current view index is less than 0, meaning that no view was currently selected, then the first album in the list is displayed. Otherwise, the last album is displayed. Now, initialize the scroller by adding the following code to viewDidLoad before[self showDataForAlbumAtIndex:0];: scroller = [[HorizontalScroller alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 120)]; scroller.backgroundColor = [UIColor colorWithRed:0.24f green:0.35f blue:0.49f alpha:1]; scroller.delegate = self; [self.view addSubview:scroller];

    [self reloadScroller]; The above simply creates a new instance of HorizontalScroller, sets its background color and delegate, adds the scroller to the main view, and then loads the subviews for the scroller to display album data. Note: If a protocol becomes too big and is packed with a lot of methods, you should consider breaking it into several smaller protocols. UITableViewDelegate and UITableViewDataSource are a good example, since they are both protocols of UITableView. Try to design your protocols so that each one handles one specific area of functionality. Build and run your project and take a look at your awesome new horizontal scroller: design-patterns-3 Uh, wait. The horizontal scroller is in place, but where are the covers? Ah, that’s right — you didn’t implement the code to download the covers yet. To do that, you’ll need to add a way to download images. Since all your access to services goes through LibraryAPI, that’s where this new method would have to go. However, there are a few things to consider first: AlbumView shouldn’t work directly with LibraryAPI. You don’t want to mix view logic with communication logic. For the same reason, LibraryAPI shouldn’t know about AlbumView. LibraryAPI needs to inform AlbumView once the covers are downloaded since the AlbumView has to display the covers. Sounds like a conundrum? Don’t despair, you’ll learn how to do this using the Observer pattern :] The Observer Pattern In the Observer pattern, one object notifies other objects of any state changes. The objects involved don’t need to know about one another – thus encouraging a decoupled design. This pattern’s most often used to notify interested objects when a property has changed. The usual implementation requires that an observer registers interest in the state of another object. When the state changes, all the observing objects are notified of the change. Apple’s Push Notification service is a global example of this. If you want to stick to the MVC concept (hint: you do), you need to allow Model objects to communicate with View objects, but without direct references between them. And that’s where the Observer pattern comes in. Cocoa implements the observer pattern in two familiar ways: Notifications and Key-Value Observing (KVO). Notifications Not be be confused with Push or Local notifications, Notifications are based on a subscribe-and-publish model that allows an object (the publisher) to send messages to other objects (subscribers/listeners). The publisher never needs to know anything about the subscribers. Notifications are heavily used by Apple. For example, when the keyboard is shown/hidden the system sends a UIKeyboardWillShowNotification/UIKeyboardWillHideNotification, respectively. When your app goes to the background, the system sends a UIApplicationDidEnterBackgroundNotification notification. Note: Open up UIApplication.h, at the end of the file you’ll see a list of over 20 notifications sent by the system. How to Use Notifications Go to AlbumView.m and insert the following code after [self addSubview:indicator]; in initWithFrame:albumCover:: [[NSNotificationCenter defaultCenter] postNotificationName:@"BLDownloadImageNotification" object:self userInfo:@{@"imageView":coverImage, @"coverUrl":albumCover}]; This line sends a notification through the NSNotificationCenter singleton. The notification info contains the UIImageView to populate and the URL of the cover image to be downloaded. That’s all the information you need to perform the cover download task. Add the following line to init in LibraryAPI.m, directly after isOnline = NO: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadImage:) name:@"BLDownloadImageNotification" object:nil]; This is the other side of the equation: the observer. Every time an AlbumView class posts a BLDownloadImageNotification notification, since LibraryAPI has registered as an observer for the same notification, the system notifies LibraryAPI. And LibraryAPI executes downloadImage: in response. However, before you implement downloadImage: you must remember to unsubscribe from this notification when your class is deallocated. If you do not properly unsubscribe from a notification your class registered for, a notification might be sent to a deallocated instance. This can result in application crashes. Add the following method to LibraryAPI.m:

  • (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } When this class is deallocated, it removes itself as an observer from all notifications it had registered for. There’s one more thing to do. It would probably be a good idea to save the downloaded covers locally so the app won’t need to download the same covers over and over again. Open PersistencyManager.h and add the following two method prototypes:

  • (void)saveImage:(UIImage*)image filename:(NSString*)filename;

  • (UIImage*)getImage:(NSString*)filename; And the method implementations to PersistencyManager.m:

  • (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]; } This code is pretty straightforward. The downloaded images will be saved in the Documents directory, and getImage: will return nil if a matching file is not found in the Documents directory. Now add the following method to LibraryAPI.m:

  • (void)downloadImage:(NSNotification*)notification { // 1 UIImageView *imageView = notification.userInfo[@"imageView"]; NSString *coverUrl = notification.userInfo[@"coverUrl"];

    // 2 imageView.image = [persistencyManager getImage:[coverUrl lastPathComponent]];

    if (imageView.image == nil) { // 3 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ UIImage *image = [httpClient downloadImage:coverUrl];

          // 4
          dispatch_sync(dispatch_get_main_queue(), ^{
              imageView.image = image;
              [persistencyManager saveImage:image filename:[coverUrl lastPathComponent]];
          });
      });
    

    }
    } Here’s a breakdown of the above code: downloadImage is executed via notifications and so the method receives the notification object as a parameter. The UIImageView and image URL are retrieved from the notification. Retrieve the image from the PersistencyManager if it’s been downloaded previously. If the image hasn’t already been downloaded, then retrieve it using HTTPClient. When the download is complete, display the image in the image view and use the PersistencyManager to save it locally. Again, you’re using the Facade pattern to hide the complexity of downloading an image from the other classes. The notification sender doesn’t care if the image came from the web or from the file system. Build and run your app and check out the beautiful covers inside your HorizontalScroller: design-patterns-4 Stop your app and run it again. Notice that there’s no delay in loading the covers because they’ve been saved locally. You can even disconnect from the Internet and your app will work flawlessly. However, there’s one odd bit here: the spinner never stops spinning! What’s going on? You started the spinner when downloading the image, but you haven’t implemented the logic to stop the spinner once the image is downloaded. You could send out a notification every time an image has been downloaded, but instead, you’ll do that using the other Observer pattern, KVO. Key-Value Observing (KVO) In KVO, an object can ask to be notified of any changes to a specific property; either its own or that of another object. If you’re interested, you can read more about this on Apple’s KVO Programming Guide. How to Use the KVO Pattern As mentioned above, the KVO mechanism allows an object to observe changes to a property. In your case, you can use KVO to observe changes to the image property of the UIImageView that holds the image. Open AlbumView.m and add the following code to initWithFrame:albumCover:, just after the [self addSubview:indicator]; line: [coverImage addObserver:self forKeyPath:@"image" options:0 context:nil]; This adds self, which is the current class, as an observer for the image property of coverImage. You also need to unregister as an observer when you’re done. Still in AlbumView.m, add the following code:

  • (void)dealloc { [coverImage removeObserver:self forKeyPath:@"image"]; } Finally, add this method:

  • (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"image"]) { [indicator stopAnimating]; } } You must implement this method in every class acting as an observer. The system executes this method every time the observed property changes. In the above code, you stop the spinner when the “image” property changes. This way, when an image is loaded, the spinner will stop spinning. Build and run your project. The spinner should disappear: design pattern - stage3 Note: Always remember to remove your observers when they’re deallocated, or else your app will crash when the subject tries to send messages to these non-existent observers! If you play around with your app a bit and terminate it, you’ll notice that the state of your app isn’t saved. The last album you viewed won’t be the default album when the app launches. To correct this, you can make use of the next pattern on the list: Memento. The Memento Pattern The memento pattern captures and externalizes an object’s internal state. In other words, it saves your stuff somewhere. Later on, this externalized state can be restored without violating encapsulation; that is, private data remains private. How to Use the Memento Pattern Add the following two methods to ViewController.m:

  • (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"]; }

  • (void)loadPreviousState { currentAlbumIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"currentAlbumIndex"]; [self showDataForAlbumAtIndex:currentAlbumIndex]; } saveCurrentState saves the current album index to NSUserDefaults – NSUserDefaults is a standard data store provided by iOS for saving application specific settings and data. loadPreviousState loads the previously saved index. This isn’t quite the full implementation of the Memento pattern, but you’re getting there. Now, Add the following line to viewDidLoad in ViewController.m before the scroller initialization: [self loadPreviousState]; That loads the previously saved state when the app starts. But where do you save the current state of the app for loading from? You’ll use Notifications to do this. iOS sends a UIApplicationDidEnterBackgroundNotification notification when the app enters the background. You can use this notification to call saveCurrentState. Isn’t that convenient? Add the following line to the end of viewDidLoad: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveCurrentState) name:UIApplicationDidEnterBackgroundNotification object:nil]; Now, when the app is about to enter the background, the ViewController will automatically save the current state by calling saveCurrentState. Now, add the following code:

  • (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } This ensures you remove the class as an observer when the ViewController is deallocated. Build and run your app. Navigate to one of the albums, send the app to the background using Command+Shift+H (if you are on the simulator) and then shut down your app. Relaunch, and check that the previously selected album is centered: No Change It looks like the album data is correct, but the scroller isn’t centered on the correct album. What gives? This is what the optional method initialViewIndexForHorizontalScroller: was meant for! Since that method’s not implemented in the delegate, ViewController in this case, the initial view is always set to the first view. To fix that, add the following code to ViewController.m:

  • (NSInteger)initialViewIndexForHorizontalScroller:(HorizontalScroller *)scroller { return currentAlbumIndex; } Now the HorizontalScroller first view is set to whatever album is indicated by currentAlbumIndex. This is a great way to make sure the app experience remains personal and resumable. Run your app again. Scroll to an album as before, stop the app, then relaunch to make sure the problem is fixed: Showing saved state If you look at PersistencyManager‘s init, you’ll notice the album data is hardcoded and recreated every time PersistencyManager is created. But tt’s better to create the list of albums once and store them in a file. How would you save the Album data to a file? One option is to iterate through Album‘s properties, save them to a plist file and then recreate the Album instances when they’re needed. This isn’t the best option, as it requires you to write specific code depending on what data/properties are there in each class. For example, if you later created a Movie class with different properties, the saving and loading of that data would require new code. Additionally, you won’t be able to save the private variables for each class instance since they are not accessible to an external class. That’s exactly why Apple created the Archiving mechanism. Archiving One of Apple’s specialized implementations of the Memento pattern is Archiving. This converts an object into a stream that can be saved and later restored without exposing private properties to external classes. You can read more about this functionality in Chapter 16 of the iOS 6 by Tutorials book. Or in Apple’s Archives and Serializations Programming Guide. How to Use Archiving First, you need to declare that Album can be archived by conforming to the NSCoding protocol. Open Album.h and change the @interface line as follows: @interface Album : NSObject Add the following two methods to Album.m:

  • (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.year forKey:@"year"]; [aCoder encodeObject:self.title forKey:@"album"]; [aCoder encodeObject:self.artist forKey:@"artist"]; [aCoder encodeObject:self.coverUrl forKey:@"cover_url"]; [aCoder encodeObject:self.genre forKey:@"genre"]; }

  • (id)initWithCoder:(NSCoder *)aDecoder { self = [super init]; if (self) { _year = [aDecoder decodeObjectForKey:@"year"]; _title = [aDecoder decodeObjectForKey:@"album"]; _artist = [aDecoder decodeObjectForKey:@"artist"]; _coverUrl = [aDecoder decodeObjectForKey:@"cover_url"]; _genre = [aDecoder decodeObjectForKey:@"genre"]; } return self; } You call encodeWithCoder: when you archive an instance of this class. Conversely, you call initWithCoder: when you unarchive an instance to create an instance of Album. It’s simple, yet powerful. Now that the Album class can be archived, add the code that actually saves and loads the list of albums. Add the following signature (or method prototype) to PersistencyManager.h:

  • (void)saveAlbums; This will be the method that’s called to save the albums. Now, add the method implementation to PersistencyManager.m:

  • (void)saveAlbums { NSString *filename = [NSHomeDirectory() stringByAppendingString:@"/Documents/albums.bin"]; NSData *data = [NSKeyedArchiver archivedDataWithRootObject:albums]; [data writeToFile:filename atomically:YES]; } NSKeyedArchiver archives the album array into a file called albums.bin. When you archive an object which contains other objects, the archiver automatically tries to recursively archive the child objects and any child objects of the children and so on. In this instance, the archival starts with albums, which is an array of Album instances. Since NSArray and Album both support the NSCopying interface, everything in the array is automatically archived. Now replace init in PersistencyManager.m with the following code:

  • (id)init { self = [super init]; if (self) { NSData *data = [NSData dataWithContentsOfFile:[NSHomeDirectory() stringByAppendingString:@"/Documents/albums.bin"]]; albums = [NSKeyedUnarchiver unarchiveObjectWithData:data]; if (albums == nil) { albums = [NSMutableArray arrayWithArray: @[[[Album alloc] initWithTitle:@"Best of Bowie" artist:@"David Bowie" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_david%20bowie_best%20of%20bowie.png" year:@"1992"], [[Album alloc] initWithTitle:@"It's My Life" artist:@"No Doubt" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_no%20doubt_its%20my%20life%20%20bathwater.png" year:@"2003"], [[Album alloc] initWithTitle:@"Nothing Like The Sun" artist:@"Sting" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_sting_nothing%20like%20the%20sun.png" year:@"1999"], [[Album alloc] initWithTitle:@"Staring at the Sun" artist:@"U2" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_u2_staring%20at%20the%20sun.png" year:@"2000"], [[Album alloc] initWithTitle:@"American Pie" artist:@"Madonna" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_madonna_american%20pie.png" year:@"2000"]]]; [self saveAlbums]; } } return self; } In the new code, NSKeyedUnarchiver loads the album data from the file, if it exists. If it doesn’t exist, it creates the album data and immediately saves it for the next launch of the app. You’ll also want to save the album data every time the app goes into the background. This might not seem necessary now but what if you later add the option to change album data? Then you’d want this to ensure that all your changes are saved. Add the following method signature to LibraryAPI.h:

  • (void)saveAlbums; Since the main application accesses all services via LibraryAPI, this is how the application will let PersitencyManager know that it needs to save album data. Now add the method implementation to LibraryAPI.m:

  • (void)saveAlbums { [persistencyManager saveAlbums]; } This code simply passes on a call to LibraryAPI to save the albums on to PersistencyMangaer. Add the following code to the end of saveCurrentState in ViewController.m: [[LibraryAPI sharedInstance] saveAlbums]; And the above code uses LibraryAPI to trigger the saving of album data whenever the ViewController saves its state. Build your app to check that everything compiles. Unfortunately, there’s no easy way to check if the data persistency is correct though. You can check the simulator Documents folder for your app in Finder to see that the album data file is created but in order to see any other changes you’d have to add in the ability to change album data. But instead of changing data, what if you added an option to delete albums you no longer want in your library? Additionally, wouldn’t it be nice to have an undo option if you delete an album by mistake? This provides a great opportunity to talk about the last pattern on the list: Command. The Command Pattern The Command design pattern encapsulates a request or action as an object. The encapsulated request is much more flexible than a raw request and can be passed between objects, stored for later, modified dynamically, or placed into a queue. Apple has implemented this pattern using the Target-Action mechanism and Invocation. You can read more about Target-Action in Apple’s documentation but Invocation uses the NSInvocation class which contains a target object, a method selector and some parameters. This object can be changed dynamically and executed when needed. It’s a perfect example of the Command pattern in action. It decouples the sending object from the receiving object or objects and can persist a request or a chain of requests. How to Use the Command Pattern Before you get into the invocation of actions, you need to set up the framework for undoing actions. So you must define the UIToolBar and the NSMutableArray needed for the undo stack. Add the following code to the class extension in ViewController.m where you defined all the other variables: UIToolbar *toolbar; // We will use this array as a stack to push and pop operation for the undo option NSMutableArray *undoStack; This creates a toolbar which will display the buttons for the new actions, as well as an array to act as the command queue. Add the following code to the beginning of viewDidLoad: (right before comment #2): toolbar = [[UIToolbar alloc] init]; UIBarButtonItem *undoItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemUndo target:self action:@selector(undoAction)]; undoItem.enabled = NO; UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *delete = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteAlbum)]; [toolbar setItems:@[undoItem,space,delete]]; [self.view addSubview:toolbar]; undoStack = [[NSMutableArray alloc] init]; The above code creates a toolbar with two buttons and a flexible space between them. It also creates an empty undo stack. The undo button is disabled here because the undo stack starts off empty. Also, note that the toolbar isn’t initiated with a frame, as the frame size set in viewDidLoad isn’t final. So set the final frame via the following block of code once the view frame is finalized by adding the code to ViewController.m:

  • (void)viewWillLayoutSubviews { toolbar.frame = CGRectMake(0, self.view.frame.size.height-44, self.view.frame.size.width, 44); dataTable.frame = CGRectMake(0, 130, self.view.frame.size.width, self.view.frame.size.height - 200); } You’ll add three method to ViewController.m for handling album management actions: add, delete, and undo. The first is the method for adding a new album:

  • (void)addAlbum:(Album*)album atIndex:(int)index { [[LibraryAPI sharedInstance] addAlbum:album atIndex:index]; currentAlbumIndex = index; [self reloadScroller]; } Here you add the album, set it as the current album index, and reload the scroller. Next comes the delete method:

  • (void)deleteAlbum { // 1 Album *deletedAlbum = allAlbums[currentAlbumIndex];

    // 2 NSMethodSignature *sig = [self methodSignatureForSelector:@selector(addAlbum:atIndex:)]; NSInvocation *undoAction = [NSInvocation invocationWithMethodSignature:sig]; [undoAction setTarget:self]; [undoAction setSelector:@selector(addAlbum:atIndex:)]; [undoAction setArgument:&deletedAlbum atIndex:2]; [undoAction setArgument:&currentAlbumIndex atIndex:3]; [undoAction retainArguments];

    // 3 [undoStack addObject:undoAction];

    // 4 [[LibraryAPI sharedInstance] deleteAlbumAtIndex:currentAlbumIndex]; [self reloadScroller];

    // 5 [toolbar.items[0] setEnabled:YES]; } There are some new and exciting features in this code, so consider each commented section below: Get the album to delete. Define an object of type NSMethodSignature to create the NSInvocation, which will be used to reverse the delete action if the user later decides to undo a deletion. The NSInvocation needs to know three things: The selector (what message to send), the target (who to send the message to) and the arguments of the message. In this example the message sent is delete’s opposite since when you undo a deletion, you need to add back the deleted album. After the undoAction has been created you add it to the undoStack. This action will be added to the end of the array, just as in a normal stack. Use LibraryAPI to delete the album from the data structure and reload the scroller. Since there’s an action in the undo stack, you need to enable the undo button. Note: With NSInvocation, you need to keep the following points in mind: The arguments must be passed by pointer. The arguments start at index 2; indices 0 and 1 are reserved for the target and the selector. If there’s a chance that the arguments will be deallocated, then you should call retainArguments. Finally, add the method for the undo action:

  • (void)undoAction { if (undoStack.count > 0) { NSInvocation *undoAction = [undoStack lastObject]; [undoStack removeLastObject]; [undoAction invoke]; }

    if (undoStack.count == 0) { [toolbar.items[0] setEnabled:NO]; } } The undo operation “pops” the last object in the stack. This object is always of type NSInvocation and can be invoked by calling … invoke. This invokes the command you created earlier when the album was deleted, and adds the deleted album back to the album list. Since you also deleted the last object in the stack when you “popped” it, you now check to see if the stack is empty. If it is, that means there are no more actions to undo. So you disable the Undo button. Build and run your app to test out your undo mechanism, delete an album (or two) and hit the Undo button to see it in action: design pattern - stage4 This is also a good place to test out whether changes to your album data is retained between sessions. Now, if you delete an album, send the app to the background, and then terminate the app, the next time you start the app the displayed album list should reflect the deletion. Where to go from here? Here’s the source code for the finished project: BlueLibrary-final There are two other design patterns that didn’t make their way into the app, but are important to mention: Abstract Factory (aka Class Cluster) and Chain of Responsibility (aka Responder Chain). Feel free to read up on these to expand your design pattern horizons. In this tutorial you saw how to harness the power of iOS design patterns to perform complicated tasks in a very straightforward and loosely coupled manner. You’ve learned a lot of iOS design patterns and concepts: Singleton, MVC, Delegation, Protocols, Facade, Observer, Memento, and Command. Your final code is loosely coupled, reusable, and readable. If another developer looks at your code, they’ll instantly understand what’s going on and what each class does in your app. The point isn’t to use a design pattern for every line of code you write. Instead, be aware of design patterns when you consider how to solve a particular problem, especially in the early stages of designing your app. They’ll make your life as a developer much easier and your code a lot better!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment