SOLID in Swift

Ahmed Adam
4 min readJul 14, 2023

--

SOLID Principles with swift examples.

Introduction

The SOLID principles are a set of design principles that aim to make software systems more maintainable, flexible, and scalable. These principles can be applied to iOS development using Swift to write cleaner, modular, and testable code. Here are the SOLID principles along with examples of how they can be implemented in iOS development.

Single Responsibility Principle (SRP):

The SRP states that a class should have only one reason to change. It promotes a modular approach by ensuring that each class or module has a single responsibility. This principle helps maintain code readability, understandability, and testability.

Consider a WeatherManager class that handles weather data retrieval and parsing. Instead of combining both responsibilities into a single class, we can separate them into two separate classes: WeatherDataRetriever and WeatherDataParser. This way, each class adheres to the SRP and can be independently modified or replaced.

class WeatherDataRetriever {
func retrieveData() async -> Data? {
// Fetch weather data from the network
}
}

class WeatherDataParser {
func parseData(_ data: Data) -> Weather {
// Parse the retrieved data and return a Weather object
}
}

If you are confused about, the async take a look at this article:

Open-Closed Principle (OCP):

The OCP states that classes should be open for extension but closed for modification. It emphasizes the use of abstraction and interfaces to allow new functionalities to be added without modifying existing code. This principle promotes code reusability, maintainability, and minimizes the risk of introducing bugs.

Suppose we have a PaymentProcessor class that handles various payment methods. Instead of modifying this class every time a new payment method is added, we can use protocol-oriented programming to define a PaymentMethod protocol and implement specific payment methods as separate classes.

protocol PaymentMethod {
func processPayment()
}

class CreditCardPayment: PaymentMethod {
func processPayment() {
// Process payment using credit card
}
}

class PayPalPayment: PaymentMethod {
func processPayment() {
// Process payment using PayPal
}
}

class PaymentProcessor {
func processPayment(with method: PaymentMethod) {
method.processPayment()
}
}

// Define payment methods
let creditCardPayment = CreditCardPayment()
let paypalPayment = PayPalPayment()

// Create a payment processor instance
let paymentProcessor = PaymentProcessor()

// Process payment using credit card
paymentProcessor.processPayment(with: creditCardPayment)

// Process payment using PayPal
paymentProcessor.processPayment(with: paypalPayment)

Liskov Substitution Principle (LSP):

The LSP states that objects of a superclass should be replaceable with objects of its subclasses without affecting the correctness of the program. In other words, subclasses should be able to be used interchangeably with their parent class without causing unexpected behaviors. This principle ensures code correctness and promotes code reuse and polymorphism.

Consider a simple example with a Animal superclass and its subclasses, Dog and Cat. Both subclasses implement the makeSound() method, allowing them to be substituted for the Animal class without altering the behavior of other parts of the code.

class Animal {
func makeSound() {
// Common implementation for all animals
}
}

class Dog: Animal {
override func makeSound() {
print("Dog!")
}
}

class Cat: Animal {
override func makeSound() {
print("Cat!")
}
}

Now, imagine we have a function called animalSound that takes an instance of the Animal class and calls its makeSound() method:

func animalSound(animal: Animal) {
animal.makeSound()
}

According to the LSP, we should be able to substitute instances of the Animal class with instances of its subclasses without affecting the behavior of the program. Let’s test this principle by passing different animal objects to the animalSound function.

let animal = Animal()
let dog = Dog()
let cat = Cat()

animalSound(animal: animal) // Output: "Common implementation for all animals"
animalSound(animal: dog) // Output: "Dog!"
animalSound(animal: cat) // Output: "Cat!"

Interface Segregation Principle (ISP):

The ISP states that clients should not be forced to depend on interfaces they do not use. It emphasizes the creation of specific and minimal interfaces to prevent clients from being burdened with unnecessary dependencies. This principle encourages modularity, reusability, and maintainability.

Suppose we have a MediaPlayer class that provides audio and video playback capabilities. Instead of forcing all clients to implement both audio and video playback methods, we can split the interfaces into separate protocols: AudioPlayer and VideoPlayer.

protocol AudioPlayer {
func playAudio()
func stopAudio()
}

protocol VideoPlayer {
func playVideo()
func stopVideo()
}

class MediaPlayer: AudioPlayer, VideoPlayer {
// Implement methods for both protocols
}

Dependency Inversion Principle (DIP):

The DIP states that high-level modules should not depend on low-level modules but should depend on abstractions. It promotes loose coupling by inverting the dependency relationships between modules.

Suppose we have a WeatherReporter class that needs to fetch weather data. Instead of directly depending on a specific data retrieval class, we can define a WeatherDataProvider protocol and inject its implementation into the WeatherReporter class.

protocol WeatherDataProvider {
func fetchWeatherData() async -> Weather?
}

class WeatherReporter {
private let dataProvider: WeatherDataProvider

init(dataProvider: WeatherDataProvider) {
self.dataProvider = dataProvider
}

func reportWeather() async {
if let weather = await dataProvider.fetchWeatherData() {
// Process and report weather data
}
}
}

Conclusion:

Implementing the SOLID principles in iOS development enables developers to create code that is more comprehensible, maintainable, and extensible. By following these principles, iOS developers can construct scalable and resilient applications that can easily adapt to future changes. With its expressive syntax and robust type system, Swift serves as a strong base for incorporating these principles and attaining top-notch code quality in iOS development.

--

--

Ahmed Adam

Senior iOS Engineer | Swift | SwiftUI | Objective-C | Agile | Scrum | Mobile Development |