Skip to content

Commit aa1f859

Browse files
committed
Merge pull request #35 from rakaramos/source
Add Wrapping APIs with Builder hint
2 parents 0dd42c1 + 1378a28 commit aa1f859

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
layout: post
3+
author: rafa
4+
title: "Wrapping API's using the Builder Pattern"
5+
date: 2016-05-02 22:36:49 +0200
6+
comments: false
7+
categories:
8+
---
9+
10+
The way I was introduced to the [Design Patterns](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612) lead me to think that those clever and neat solutions were meant to be used just in big softwares solutions. I never considered using them into the small pieces of software. What do I mean by that? Please, read on.
11+
12+
The Builder Pattern if defined as follows:
13+
14+
> Separate the construction of a complex object from its representation so that the same construction process can create different representations.
15+
16+
<!--more-->
17+
Now, consider for a while the creation of an `UIAlertView` in iOS.
18+
19+
```swift
20+
let alert = UIAlertView(title: "Question", message: "Do you like apples?", delegate: self, cancelButtonTitle: "I hate it!", otherButtonTitles: "Yes, I do!", "More of less")
21+
```
22+
23+
This is a long method call, right? But really, that's not the problem. The problem here is that our class has to conform to `UIAlertViewDelegate` in order to receive the alert result. Wouldn't be nicer to have that logic encapsulated? Well, go back and read the definition for the builder pattern, it fits like a glove, am I right?
24+
25+
An idea on how to wrap the builder pattern around the `UIAlertView` class is as above:
26+
27+
```swift
28+
class AlertBuilder: NSObject, UIAlertViewDelegate {
29+
30+
typealias AlertBuilderCompletion = Int -> Void
31+
32+
private var alertTitle: String = ""
33+
private var alertMessage: String = ""
34+
private var alertStyle: UIAlertViewStyle = .Default
35+
private var alertButtonTitles: [String] = []
36+
private var alertCompletion: AlertBuilderCompletion? = nil
37+
38+
func title(title: String) -> AlertBuilder {
39+
alertTitle = title
40+
return self
41+
}
42+
43+
func message(message: String) -> AlertBuilder {
44+
alertMessage = message
45+
return self
46+
}
47+
48+
func style(style: UIAlertViewStyle) -> AlertBuilder {
49+
alertStyle = style
50+
return self
51+
}
52+
53+
func buttonTitles(titles: String...) -> AlertBuilder {
54+
alertButtonTitles = alertButtonTitles + titles
55+
return self
56+
}
57+
58+
func show(completion: AlertBuilderCompletion? = nil) {
59+
let alertView = UIAlertView()
60+
alertView.delegate = self
61+
alertView.title = alertTitle
62+
alertView.message = alertMessage
63+
alertView.alertViewStyle = alertStyle
64+
65+
alertButtonTitles.forEach { title in
66+
alertView.addButtonWithTitle(title)
67+
}
68+
69+
alertCompletion = completion
70+
alertView.show()
71+
}
72+
73+
func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) {
74+
alertCompletion?(buttonIndex)
75+
}
76+
}
77+
```
78+
79+
Now, all that is necessary to use create an alert is:
80+
81+
```
82+
alert.title("Question")
83+
.message("Do you like apples?")
84+
.buttonTitles("Yes, I do!","More of less", "I hate it!")
85+
.show { index in
86+
print(index)
87+
}
88+
```
89+
90+
In the past, I would have used the first approach and lived with that. Of course, showing alerts to the user is a very tiny part of a real work application. But that's preciselly where I was wrong. This kind of applicability of the builder (among all other design patterns) is what makes software components reusable.
91+
And there are some other places where you could apply the same principle, for example `NSAttributedString` or `UIActionSheet`.
92+
93+
I hope you find that useful. Builder to the rescue!
94+
95+
P.S: Yes, yes I know that Apple has released `UIAlertController` and deprecated both `UIAlertView` and `UIActionSheet`. However, the idea is pretty much the same, alothough what Apple did is Factory instead of a Builder.

0 commit comments

Comments
 (0)