Design Patterns in Swift: Part II— Behavioral Design Pattern

Lubaba Hasnain
Jan 1, 2019 · 8 min read
Image for post
Image for post

In the first article of the series, I discussed about Creational Design Patterns.
Now I will describe another set of patterns called Behavioral Design Patterns.

Behavioral Design Pattern deals with how objects interact. It describes how objects communicates with each other and how the steps of a task is broken among different objects so as to provide more flexibility and make the code more testable.

Let us jump to these 10 behavioral design patterns:

1. Chain of Responsibility

There is an enum called Level to breakdown the Sports management into three levels: state, national & international. First let us create a class called Sports which will just hold the current sports management level.

Then we have the protocol called GameManagement which can pass the responsibility along the handler chain. The classes StateSportsTeam, NationalSportsTeam & InternationalSportsTeam implement this protocol. If the sports level does not belong to their management, they pass the responsibility to the higher management (or the handlers chain).

Running the code in the playground:

let stateSportsTeam = StateSportsTeam()
let nationalSportsTeam = NationalSportsTeam()
let internationalSportsTeam = InternationalSportsTeam()
stateSportsTeam.nextLevelManagement = nationalSportsTeam
nationalSportsTeam.nextLevelManagement = internationalSportsTeam
let sports1 = Sports(level:
stateSportsTeam.manage(sports: sports1)
let sports2 = Sports(level: Level.national)
stateSportsTeam.manage(sports: sports2)
let sports3 = Sports(level: Level.state)stateSportsTeam.manage(sports: sports3)Output:
Managed by International Sports Management
Managed by National Sports Management
Managed by State Sports Management

2. Command Pattern

Let us follow the below example.
The Invoker is the heart of the pattern. It can call the execute() method to perform an action without being aware of the concrete class ConcreteCommand and what it does. It only knows about the abstract type Command protocol.

ConcreteCommand class conforms to Command protocol and takes and instance of Receiver object in the initializer. It then calls the receiver object to perform certain task/operation.

Receiver is the class that does the actual work. It can be any business logic, database request or network operation.
Client is the class that creates the ConcreteCommand object and pass it into the invoker. It can be your view controller class.

Trying out the code:

let client = Client()

3. Iterator Pattern

Ever wondered how for-in loop works in Swift?
Swift has a built-in Sequence protocol which helps in creating iterators. The Sequence protocol gives sequential access to its elements by creating an iterator. IteratorProtocol is a type that supplies the values of a sequence one at a time.

The class MyBestFilms implements the Sequence protocol that creates a custom iterator. Every time the next() function is called, it returns the next element and store the current index.
This is absolutely great! You can create your own custom sequence.

Illustrating the point:

let myFilms = MyBestFilms(films: ["Godfather Trilogy", "Silence of the Lambs", "Call Me By Your Name"])for film in myFilms {
Godfather Trilogy
Silence of the Lambs
Call Me By Your Name

4. Mediator Pattern

So, for example, there are two teams competing with each other to get selected in the finals of a game. Team A is The Avengers and Team B is The League of Extraordinary Gentlemen.
So instead of directly communicating with each other, they register to the mediator object and then they can send messages to each other via Mediator.

Two protocols are defined:
Protocol Receiver implemented by the Teams which has the abstract receive method that takes a message as a parameter.

Protocol Sender implemented by the Mediator which has the abstract method send that takes message and a Receiver type object as a parameter.
Our mediator iterates through each team in its list of teams. If the team A is sending the request, team B receives it and vice-versa.

Notice that the teams are not aware of each other. There is no reference of team A in team B and team B in team A.

Note: NSNotificationCenter uses Mediator Pattern

Let us try it out.

let mediator = Mediator()
let teamA = TeamA(name: "The Avengers")
let teamB = TeamB(name: "The League of Extraordinary Gentlemen")
mediator.register(candidate: teamA)
mediator.register(candidate: teamB)
mediator.send(message: "Selected for final! from \(", sender: teamA)
mediator.send(message: "Not selected for final! from \(", sender: teamB)
The League of Extraordinary Gentlemen received: Selected for final! from The Avengers
The Avengers received: Not selected for final! from The League of Extraordinary Gentlemen

5. Memento Pattern

We’ve a protocol Memento with default implementation of save and retrieve methods.
save saves the `state` property with key in UserDefaults.
retrieve restores the `state` property using key from UserDefaults.
state is of type dictionary which stores the value with a unique key
key is used to retrieve the corresponding value from the state

Class Profile will implement Memento protocol. By implementing this protocol, we can save the entire class to persistent storage and then retrieve the state at later time.

Trying on playground:

var profile = Profile(name: "Nick Jonas", age: 36, key: "NickProfile")
profile = Profile(name: "Priyanka Chopra", age: 26, key: "PriyankaProfile")
let profile1Details = Profile(key: "PriyankaProfile")
let profileState = profile1Details.retrieveDetails()
["age": 26, "name": Priyanka Chopra]

6. Observer Pattern

So, it is a one-to-many relationship. The object whose state changes are called observable or subject and the objects which subscribe for changes on the observable are called observers.

The Observable protocol has three abstract methods:
add: for adding observers
remove: for removing observers
notify: for notifying all observers about its state change

Assume a scenario where an Apple Seller has to notify her customers about new apples arriving in her shop. As soon as new apples arrive, she notifies the customer who are added in the loop about the new apples. She can also remove the customers from the loop.

AppleSeller is the class that implements the Observable protocol. When the appleCount variable value changes, it calls the notify() method to notify the customers who are added to it.
Customer is a class that listens to the changes in the appleCount variable. As soon as this variable’s value changes, the customer gets updated via the update() method.

Here’s the usage:

let appleSeller = AppleSeller()let james = Customer(name: "James", observable: appleSeller, customerId: 101)
let david = Customer(name: "David", observable: appleSeller, customerId: 102)
appleSeller.appleCount = 10
appleSeller.remove(customer: james)
appleSeller.appleCount = 20
Hurry James! 10 apples arrived at shop.
Hurry David! 10 apples arrived at shop.
Hurry David! 20 apples arrived at shop.

7. State Pattern

First we define a protocol called Human which has a getter for getting the current state and a setter for setting the current state of the object.
The state will be carried by a class called Man.
The protocol ManState has three states: stand(), walk() and run(). It is implemented by three concrete classes: StandingState, WalkingState and RunningState.

To illustrate the point of this pattern:

let man = Man()let walkingState = WalkingState(man)
let runningState = RunningState(man)
The man is in walking position
Walking State
The man is in running position
Running State

You can see from the results that calling the same method on the same instance of a class is producing different results.

8. Strategy Pattern

We define a protocol called Strategy and it has a method named convert to convert the number into a format decided by the user at runtime.
We implement two algorithms: BinaryStrategy to convert number to binary and HexStrategy to convert number to hex.

The class Convert has a property called Strategy so any of the classes can be used which implement the Strategy protocol.
At runtime we pass the the class instance of type Strategy depending upon we choose the algorithm that does binary number conversion or we choose the algorithm that does hex number conversion.

Running the above code:

let binaryConvert = Convert(number: 2, strategy: BinaryStrategy())
let hexConvert = Convert(number: 123, strategy: HexStrategy())
Binary is 10
Hex is 7b

9. Template Pattern

In Template method pattern, classes interact using only base classes that implements the algorithm steps. In swift, we’ll use interface delegation for that.
Suppose, in an office there is an office schedule for all the employees.
Protocol Office has an abstract method called officeSchedule(). Class XYZOffice will implement this protocol.

All the employees have shared responsibilities like work and get payment for their work.
Protocol Employee has two abstract methods: work() and getPaid().
Different employees will do different kind of work but still they follow the same template of working and getting paid.

Illustrating the point:

let xyzOfficeDev = XYZOffice(employee: Developer())
let xyzOfficeManager = XYZOffice(employee: ProductManager())
Developer has worked 40 hours/week this month
Developer has earned Rs 50,000 this month
Product Manager has worked 55 hours/week this month
Product Manager has earned Rs 80,000 this month

10. Visitor Pattern

Each element accepts a visitor which does the logic in another class. So the structure of the visited class is not changed at all.
Each country should be able to accept a visitor. So we declare a protocol named Country which has a method to accept a visitor.
The visitor is named CountryVisitor.
Let us define concrete classes for our Country.

And now let’s implement the CountryVisitor protocol and showcase this design pattern.

Running in the playground

let countries: [Country] = [India(), Brazil(), China()]
let names = { (country: Country) -> String in
visitor = CountryVisitorName()
country.accept(visitor: visitor)
return visitor.visitorName
["Chen", "Ramesh", "Antonio"]

Here is the link to the playground files.

All Behavioral Design Patterns in Swift are fully covered in this article with working examples.

Behavioral Design pattern focuses on communication and interaction of objects, powerful dynamic interfaces and object composition which should be used for designing softwares.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store