Simplifying iOS code by using Design Patterns
In software development, a design pattern is a general reusable solution to a problem. A design pattern is a description of how to solve a problem that can be used in different situations.
In this Article we will be implementing three design patterns by using swift, that will help us to make our code much cleaner and provide a reusable template for solving commonly occurring problems on iOS
In this article, we will be discussing better solutions using Design Patterns for a simple application for publishing a form. You can find complete code for the example application here.
Suppose that we want to be able to save multiple versions of a form before publishing it and we want to have the ability to go back to any of the previous versions and use that to make modifications, also we want to have Undo-functionality for the latest version and we want to provide some form of validation.
Our first design Pattern that we will be using is called Memento.
Memento pattern is used to capture the current state of an object and store it in such a manner that it can be restored at a later time without breaking the rules of encapsulation.
What solution can the Memento pattern bring for us?
- Save an internal state to a (memento) object
- Restore to a previous state from a (memento) object.
We want our application to be able to persist our form versions using User Defaults.
First We need to create a FormState object that represents the data that will be persisted when we want to save the changes. This is also known as Originator
We need to create a caretaker (a client) that can request a memento from the originator (to save the internal state of the originator) and pass a memento back to the originator (to restore to a previous state).
This enables to save and restore the internal state of an originator without violating its encapsulation.
This can be used as follows:
And Retrieve state:
In our example, we want to be able to save multiple states for a single form
Our caretaker then will turn to:
Now we can save and retrieve multiple versions for our form, but this leads to a complicated User defaults code. Lets try to clean it up by using next design pattern.
The facade pattern is used to define a simplified interface to a more complex subsystem.
In the example above we can see that working with user defaults in particular with saving array of data leads to messy code. Usually, in the iOS Development world, we see lots of copy-pasting of UserDefaults code that contributes to creating bad code.
Let us try to refactor it by using facade for User Defaults.
Our code now can transform to following:
Using the facade pattern our UserDefaults Code becomes much simpler.
If you checked our example app you could see that when the user wants to publish data we validate like following:
In this article, I will only discuss one part of the Validation code that was made easy using Factory Pattern.
The factory pattern is used to provide a client with a set of related or dependent objects. The “family” of objects created by the factory are determined at run-time.
To make it easier to get validated text from UITextField we can write the following code
To make it easier we can create a
ValidatorFactory and use Validator objects to determine validity.
Our extension code can simply become:
If you want to check the complete validation code and reading the article on simple validations on iOS, you can read the article here.
In this article we have explained and provided an implementation using swift for Memento, Facade and Factory patterns, these patterns can help us to solve and provide cleanup for common iOS problems.
Learning Design Patterns can help every developer to broaden up their knowledge on providing solutions for everyday development problems and allow them to write more reusable and cleaner code.
If you enjoyed this post make sure to like and share it.
If you want to access the complete project and demo code that provides you can find it on Github.
If you have any questions or comments feel free to leave a note here or email me at firstname.lastname@example.org.