Hello Readers, CoolMonkTechie heartily welcomes you in this article.
In this article, We will learn about most popular design principles SOLID in Swift. We will see that how SOLID is applicable for Swift. Now a days , a Maintainable and Reusable component is just a dream. Maybe not. SOLID principles, may be the way.
A famous quote about learning is :
” Change is the end result of all true learning.“
So Let’s begin.
Origin of the acronym SOLID
SOLID is an acronym named by Robert C. Martin (Uncle Bob). It represents 5 principles of object-oriented programming :
- Single responsibility Principle
- Open/Closed Principle
- Liskov Substitution Principle
- Interface Segregation Principle
- Dependency Inversion Principle
If we apply these five principles:
- We will have flexible code, which we can easily change and that will be both reusable and maintainable.
- The software developed will be robust, stable and scalable (we can easily add new features).
- Together with the use of the Design Patterns, it will allow us to create software that is highly cohesive (that is, the elements of the system are closely related) and loosely coupled (the degree of dependence between elements is low).
So, SOLID can solve the main problems of a bad architecture:
- Fragility: A change may break unexpected parts—it is very difficult to detect if you don’t have a good test coverage.
- Immobility: A component is difficult to reuse in another project—or in multiple places of the same project—because it has too many coupled dependencies.
- Rigidity: A change requires a lot of efforts because affects several parts of the project.
Of course, as Uncle Bob pointed out in a his article, these are not strict rules, but just guidelines to improve the quality of your architecture.
” Principles will not turn a bad programmer into a good programmer. Principles have to be applied with judgement. If they are applied by rote it is just as bad as if they are not applied at all. “
Principles
The Single Responsibility Principle (SRP)
According to this principle, a class should have a reason, and only one, to change. That is, a class should only have one responsibility.
Now let’s describe Single Responsibility Principle says :
“THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE.“
Every time you create/change a class, you should ask yourself: How many responsibilities does this class have?
Let’s take a look into Swifty communication program.
import Foundation
class InterPlanetMessageReceiver {
func receiveMessage() {
print("Received the Message!")
}
func displayMessageOnGUI() {
print("Displaying Message on Screen!")
}
}
Now let’s understand what is Single Responsibility Principle (SRP) and how the above program doesn’t obey it.
SRP says, “Just because you can implement all the features in a single device, you shouldn’t”.
In Object Oriented terms it means: There should never be more than one reason for a class to change. It doesn’t mean you can’t have multiple methods but the only condition is that they should have one single purpose.
Why? Because it adds a lot of manageability problems for you in the long run.
Here, the InterPlanetMessageReceiver class does the following:
- It receives the message.
- It renders it on UI.
And, two applications are using this InterPlanetMessageReceiver class:
- A messaging application uses this class to receive the message
- A graphical application uses this class to draw the message on the UI
Do you think it is violating the SRP?
YES, The InterPlanetMessageReceiver class is actually performing two different things. First, it handles the messaging, and second, displaying the message on GUI. This causes some interesting problems:
- Swifty must include the GUI into the messaging application and also while deploying the messaging application, we must include the GUI library.
- A change to the InterPlanetMessageReceiver class for the graphical application may lead to a change, build, and test for the messaging application, and vice-versa.
Swifty got frustrated with the amount of changes it required. He thought it would be a minute job but now he has already spent hours on it. So he decided do make a change into his program and fix this dependency.
This is what Swifty came up with
import Foundation
// Handles received message
class InterPlanetMessageReceiver {
func receive() {
print("Received the Message!")
}
}
// Handles the display part
class InterPlanetMessageDisplay {
func displayMessageOnGUI() {
print("Displaying Message on Screen!")
}
}
Here’s how Swifty explained this:
InterPlanetMessageReceiver class will be used by the messaging application, and the InterPlanetMessageDisplay class will be used by the graphical application. We could even separate the classes into two separate files, and that will allow us not to touch the other in case a change is needed to be implemented in one.
Finally, Swifty noted down :Why we need SRP?
- Each responsibility is an agent of change.
- Code becomes coupled if classes have more than one responsibility.
Open/Closed Principle
According to this principle, we must be able to extend the a class without changing its behaviour. This is achieved by abstraction.
Now let’s describe Open/Closed Principle says :
” SOFTWARE ENTITIES (CLASSES, MODULES, FUNCTIONS, ETC.) SHOULD BE OPEN FOR EXTENSION, BUT CLOSED FOR MODIFICATION. “
If you want to create a class easy to maintain, it must have two important characteristics:
- Open for extension: You should be able to extend or change the behaviours of a class without efforts.
- Closed for modification: You must extend a class without changing the implementation.
Let’s see our swifty example. Swifty was quite happy with these change and later he celebrated it with a drink in Swiftzen’s best pub and there his eyes fell upon an artifact hanging on the front wall and he found all the symbols he received in the message. Quickly, he opened his diary and completed deciphering all those shapes.
Next day when he returned back, he thought why not fix the DrawGraphic class which draws only circle shape, to include the rest of the shapes and display the message correctly.
// This is the DrawGraphic
class DrawGraphic {
func drawShape() {
print("Circle is drawn!")
}
}
// Updated Class code
enum Shape {
case circle
case rectangle
case square
case triangle
case pentagon
case semicircle
}
class circle {
}
// This is the DrawGraphic
class DrawGraphic {
func drawShape(shape: Shape) {
switch shape {
case .circle:
print("Circle is drawn")
case .rectangle:
print("Rectangle is drawn")
case square:
print("Square is drawn")
case triangle:
print("Triangle is drawn")
case pentagon:
print("Pentagon is drawn")
case semicircle:
print("Semicircle is drawn")
default:
print("Shape not provided")
}
}
}
Swifty was not happy with these changes, what if in future a new shape shows up, after all he saw in the artifacts that there were around 123 shapes. This class will become one fat class. Also, DrawGraphics class is used by other applications and so they also have to adapt to this change. it was nightmare for Swifty.
Open Closed Principle solves nightmare for Swifty. At the most basic level, this means, you should be able to extend a class behavior without modifying it. It’s just like I should be able to put on a dress without doing any change to my body. Imagine what would happen if for every dress I have to change my body.
After hours of thinking, Swifty came up with below implementation of DrawGraphic class.
protocol Draw {
func draw()
}
class Circle: Draw {
func draw() {
print("Circle is drawn!")
}
}
class Rectangle: Draw {
func draw() {
print("Rectangle is drawn!")
}
}
class DrawGraphic {
func drawShape(shape: Draw) {
shape.draw()
}
}
let circle = Circle()
let rectangle = Rectangle()
let drawGraphic = DrawGraphic()
drawGraphic.drawShape(shape: circle) // Circle is drawn!
drawGraphic.drawShape(shape: rectangle) // Rectangle is drawn!
Since the DrawGraphic is responsible for drawing all the shapes, and because the shape design is unique to each individual shape, it seems only logical to move the drawing for each shape into its respective class.
That means the DrawGraphic still have to know about all the shapes, right? Because how does it know that the object it’s iterating over has a draw method? Sure, this could be solved with having each of the shape classes inherit from a protocol: the Draw protocol (this can be an abstract class too).
Circle and Rectangle classes holds a reference to the protocol, and the concrete DrawGraphic class implements the protocol Draw class. So, if for any reason the DrawGraphic implementation is changed, the Circle and Rectangle classes are not likely to require any change or vice-versa.
Liskov Subsitution Principle
This principle, introduced by Barbara Liskov in 1987, states that in a program any class should be able to be replaced by one of its subclasses without affecting its functioning.
Now let’s describe Liskov Substitution Principle says :
” FUNCTIONS THAT USE POINTERS OR REFERENCES TO BASE CLASSES MUST BE ABLE TO USE OBJECTS OF DERIVED CLASSES WITHOUT KNOWING IT.“
Inheritance may be dangerous and you should use composition over inheritance to avoid a messy codebase. Even more if you use inheritance in an improper way.
This principle can help you to use inheritance without messing it up.
Let’s see our swifty example. Swifty was implementing the SenderOrigin class to know whether the sender is from a Planet or not.
The Sender class looked something like this
Class Planet {
func orbitAroundSun() {
}
}
class Earth: Planet {
func description() {
print("It is Earth!")
}
}
class Pluto: Planet {
func description() {
print("It is Pluto!")
}
}
class Sender {
func senderOrigin(planet: Planet) {
planet.description()
}
}
In the class design, Pluto should not inherit the Planet class because it is a dwarf planet, and there should be a separate class for Planet that has not cleared the neighborhood around its orbit and Pluto should inherit that.
So the principle says that Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.
Swifty whispered it is the polymorphism. Yes it is. “Inheritance” is usually described as an “is a” relationship. If a “Planet” is a “Dwarf”, then the “Planet” class should inherit the “Dwarf” class. Such “Is a” relationships are very important in class designs, but it’s easy to get carried away and end up with a wrong design and a bad inheritance.
The “Liskov’s Substitution Principle” is just a way of ensuring that inheritance is used correctly.
In the above case, both Earth and Pluto can orbit around the Sun but Pluto is not a planet. It has not cleared the neighborhood around its orbit. Swifty understood this and changed the program.
class Planet {
func oribitAroundSun() {
print("This planet Orbit around Sun!")
}
}
class Earth: Planet {
func description() {
print("Earth")
}
}
class DwarfPlanet: Planet {
func notClearedNeighbourhoodOrbit() {
}
}
class Pluto: DwarfPlanet {
func description() {
print("Pluto")
}
}
class Sender {
func senderOrigin(from: Planet) {
from.description()
}
}
let pluto = Pluto()
let earth = Earth()
let sender = Sender()
sender.senderOrigin(from: pluto) // Pluto
sender.senderOrigin(from: earth) // Earth
Here, Pluto inherited the planet but added the notClearedNeigbourhood method which distinguishes a dwarf and regular planet.
- If LSP is not maintained, class hierarchies would be a mess, and if a subclass instance was passed as parameter to methods, strange behavior might occur.
- If LSP is not maintained, unit tests for the base classes would never succeed for the subclass.
Swifty can design objects and apply LSP as a verification tool to test the hierarchy whether inheritance is properly done.
Interface Segregation Principle
The Principle of segregation of the interface indicates that it is better to have different interfaces (protocols) that are specific to each client, than to have a general interface. In addition, it indicates that a client would not have to implement methods that he does not use.
Now let’s describe Interface Segragation Principle says :
” CLIENTS SHOULD NOT BE FORCED TO DEPEND UPON INTERFACES THAT THEY DO NOT USE.“
This principle introduces one of the problems of object-oriented programming: the fat interface.
An interface is called “fat” when has too many members/methods, which are not cohesive and contains more information than we really want. This problem can affect both classes and protocols.
Let’s continue our swifty example. Swifty was quite astonished with the improvement in his program. All the changes were making more sense. Now, it was time to share this code with different planet. Swiftzen 50% GDP was dependent on selling softwares and many planet has requested and signed MOU for the Inter Planet communication system.
Swifty was ready to sell the program and but he was not satisfied with current client interface. Let’s us look into it.
protocol interPlanetCommunication {
func switchOnAntenna()
func setAntennaAngle()
func transmitMessage()
func receivedMessage()
func displayMessageOnGUI()
}
Now for anyone who want to use interPlanetCommunication, he has to implement all the five methods even-though they might not required.
So the principle says that Many client-specific interfaces are better than one general purpose interface. The principle ensures that Interfaces are developed so that each of them have their own responsibility and thus they are specific, easily understandable, and re-usable.
Swifty quickly made changes to his program interface:
protocol antenna {
func switchOnAntenna()
func setAntennaAngle()
}
protocol message {
func transmitMessage()
func receivedMessage()
}
protocol dispaly {
func displayMessageOnGUI()
}
Dependency Inversion Principle
According to the Dependency inversion principle:
“HIGH LEVEL MODULES SHOULD NOT DEPEND UPON LOW LEVEL MODULES. BOTH SHOULD DEPEND UPON ABSTRACTIONS.”
“ABSTRACTIONS SHOULD NOT DEPEND UPON DETAILS. DETAILS SHOULD DEPEND UPON ABSTRACTIONS.”
This principle tries to reduce the dependencies between modules, and thus achieve a lower coupling between classes.
This principle is the right one to follow if you believe in reusable components.
DIP is very similar to Open-Closed Principle: the approach to use, to have a clean architecture, is decoupling the dependencies. You can achieve it thanks to abstract layers.
Let’s continue our swifty example. Before finally shipping the program to all the clients, Swifty was trying to fix the password reminder class which looks like this.
class PasswordReminder {
func connectToDatabase(db: SwiftZenDB) {
print("Database Connected to SwiftzenDB")
}
func sendReminder() {
print("Send Reminder")
}
}
PasswordReminder class is dependent on a lower level module i.e. database connection. Now, let suppose that you want to change the database connection from Swiftzen to Objective-Czen, you will have to edit the PasswordReminder class.
Finally the last principle states that Entities must depend on abstractions not on concretions.
To fix above program Swifty made these changes:
protocol DBConnection {
func connection()
}
class SwiftzenDB: DBConnection {
func connection() {
print("Connected to SwiftzenDB")
}
}
class PasswordReminder {
func connectToDatabase(db: DBConnection) {
db.connection()
}
func sendReminder() {
print("Send Reminder")
}
}
The DBConnection protocol has a connection method and the SwiftzenDB class implements this protocol, also instead of directly type-hinting SwiftzenDB class in PasswordReminder, Swifty instead type-hint the protocol and no matter the type of database your application uses, the PasswordReminder class can easily connect to the database without any problems and OCP is not violated.
The point is rather than directly depending on the SwiftzenDB, the passwordReminder depends on the abstraction of some specification of Database so that if any the Database conforms to the abstraction, it can be connection with the PasswordReminder and it will work.
That’s all about in this article.
Conclusion
In this article, We understood about SOLID principles in Swift. We learnt that how SOLID is application for Swift. If we follow SOLID principles judiciously, we can increase the quality of our code. Moreover, our components can become more maintainable and reusable.
The mastering of these principles is not the last step to become a perfect developer, actually, it’s just the beginning. We will have to deal with different problems in our projects, understand the best approach and, finally, check if we are breaking some principles.
We have 3 enemies to defeat: Fragility, Immobility and Rigidity. SOLID principles are our weapons. We tried to explain the SOLID concepts in Swift easy way with examples.
Thanks for reading ! I hope you enjoyed and learned about SOLID Principles in Swift. Reading is one thing, but the only way to master it is to do it yourself.
Please follow and subscribe us on this blog and and support us in any way possible. Also like and share the article with others for spread valuable knowledge.
If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.
Thanks again Reading. HAPPY READING !!???