Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Swift 2 Design Patterns
  • Table Of Contents Toc
Swift 2 Design Patterns

Swift 2 Design Patterns

By : Lange
3 (2)
close
close
Swift 2 Design Patterns

Swift 2 Design Patterns

3 (2)
By: Lange

Overview of this book

Swift is a multi-paradigm language. It has expressive features familiar to those used to work with modern functional languages, while also keeping the object-oriented features of Objective-C. It remains compatible with Apple’s legacy codes and frameworks. A design pattern systematically names, motivates, and explains a general design that addresses a recurring design problem in object-oriented systems. It describes the problem, the solution, when to apply the solution, and its consequences. It also gives implementation hints and examples. Knowledge about design patterns is also one of the best ways to make you different compared to other low-level developers. This book shows you how to use Swift 2 to learn about 23 Gang of Four (GoF) design patterns, and is organized into three categories. The book will present you the five creational patterns, followed by the seven structural patterns, and finishing with the 11 behavioral patterns as defined by the GoF. Each chapter will introduce the pattern by defining its role, which common problems the pattern should be used for, its generic UML representation, how each objects presented in the class diagram participate in the pattern, and what the role of each of these objects is. The book then presents you with a concrete case as an illustration that will be used to implement the pattern using Swift.
Table of Contents (10 chapters)
close
close
9
Index

The builder pattern

Unlike the abstract factory pattern, which will produce parts of products of the same family, the builder pattern will help us build the finalized product that consists of several parts.

Roles

The main purpose of the builder pattern is to abstract the building of complex objects from its actual construction. Having the same construction process can create different representations of the product.

This pattern can be used when:

  • A client needs to construct complex objects without having to know its implementation
  • A client needs to construct complex objects that need to have several implementations or representations

Design

The following figure shows the generic UML class diagram of the builder pattern:

Design

Participants

This pattern is quite simple as it has only a few participants:

  • Director: This class constructs the product using the interface of the AbstractBuilder class.
  • AbstractBuilder: This class defines the method signature that allows the construction of all the parts of the product, and it contains a signature of a method that returns the product once this is built.
  • ConcreteBuilder: This is the Concrete class that implements the method of the AbstractBuilder class.
  • Product: This is the finalized product. The product contains all the parts of the watch.

Collaborations

The client creates the ConcreteBuilder and Director classes. The Director class will then build an object if the client asks him to do so by invoking the constructor and returns the finalized product to the client.

Illustration

Using the AbstractFactory method, we can use the builder pattern to build a watch. As we've seen that a watch has several parts: a dial and band. A watch can have two sizes too, and as we have already seen, the representation of the dial or band depends on the size of the watch too.

Implementation

If we want to build some watches that are represented with a dial and band, we will define a Director class that will define the construction order of all the parts of our watches and return the finalized watch to the client.

The Director class will call all the constructors who are in charge to construct one part of the watch. To implement this, we will reuse the existing code of the abstract factory pattern and add the following code.

Open the Builder.playground file in Xcode to see the added code at the bottom of the file:

//Our builder1
class BuilderGoldMilanese38mmWatch: AbstractWatchBuilder {
  override func buildDial() {
    watch.band = MilaneseBand(size: BandSize.SM)
  }
  override func buildBand() {
    watch.dial = GoldDial(size: WatchSize._38mm)
  }
}

//Our builder2
class BuilderAluminiumSportand42mmWatch:AbstractWatchBuilder {
  override func buildDial() {
    watch.band = SportBand(size: BandSize.ML)
  }
  override func buildBand() {
    watch.dial = AluminiumDial(size: WatchSize._42mm)
  }
}

//our Director class
class Director {
  var builder: AbstractWatchBuilder?
  init(){
    
  }
  
  func buildWatch(builder: AbstractWatchBuilder){
    builder.buildBand()
    builder.buildDial()
  }
}

Usage

To simulate our client, we will tell our director to create two watches:

  • A 42 mm aluminium dial with a sports band
  • A 38 mm gold dial with a milanese band

The code for the example is as follows:

//We will build 2 Watches :
//First is the Aluminium Dial of 42mm with Sport Band
let director = Director()
var b1 = BuilderAluminiumSportand42mmWatch()
director.buildWatch(b1)

// our watch 1
var w1 = b1.getResult()
w1.band?.color
w1.band?.type.rawValue
w1.band?.size.rawValue
w1.dial?.size.rawValue
w1.dial?.material.rawValue

//Our 2nd watch is a Gold 38mm Dial with Milanese Band
var b2 = BuilderGoldMilanese38mmWatch ()
director.buildWatch(b2)

// Our watch 1
var w2 = b2.getResult()
w2.band?.color
w2.band?.type.rawValue
w2.band?.size.rawValue
w2.dial?.size.rawValue
w2.dial?.material.rawValue

The result is shown in Playground like this:

Usage

Note

Swift allows the use of closure that simplifies the creation of our complex objects. Regarding the example that we provided earlier, we can write the following code to build our two watches.

Implementation using closures

Here, we don't need to use the Director and ConcreteBuilder classes. Instead, we will tell our Watch class that the builder will be in the closure.

In the previous example, remove the Director, AbstractBuilder, and ConcreteBuilder classes.

We just need to write the Watch class, as shown in the following code (you can find the following code in the BuilderClosures.playground file accompanying this chapter):

//our Product Class : a Watch
//The builder will be in the closure
class Watch{
  var dial:IWatchDial?
  var band:IWatchBand?
  typealias buildWatchClosure = (Watch) -> Void
  
  init(build:buildWatchClosure){
    build(self)
  }
}

Then, to simulate our client, we can write the following code which will call the appropriate constructor assigned to the band or dial property of the Watch object:

//Simulate our clients

let Gold42mmMilaneseWatch = Watch(build: {
  $0.band = MilaneseBand(size: BandSize.ML)
  $0.dial = GoldDial(size: WatchSize._42mm)
})

The result is as follows:

Implementation using closures
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Swift 2 Design Patterns
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon