As a continuation of Part 3: Architecture
Let’s talk about S.O.L.I.D. principles and how they can be applied in Swift.
S – The Single Responsibility Principle
Too strong class
This class works with logic, makes network requests and does navigation work.
So, let’s simplify it and rework it using The Single Responsibility Principle.
O – Open-Closed Principle (OCP)
It’s a principle for object-oriented design first described by Bertrand Meyer that says that “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”.
First – create protocol abstractions,
Then – create final implementation,
Do – extensions,
Do not do – changes.
L – Liskov Substitution Principle (LSP)
Derived classes must be substitutable for their base classes.
I – Interface Segregation Principle (ISP)
Make simple abstractions that clients need.
D – Dependency Inversion Principle
Depend on abstractions, not on specific classes/structures.
Abstractions Everywhere! Remember it!
This was a brief guide about S.O.L.I.D. practice in Swift code.
Read more: iOS Best Practices. Part 2: Swift Code Style >>>
Maxim Vialykh is CactusSoft iOS Tech Lead.
Technical Background
Programming languages: Swift, Objective-C, Java
Technologies and Platforms: iOS, Android, Windows Phone, Amazon S3, Google Cloud, Google APIs, Facebook APIs
Frameworks: PhoneGap, Xamarin
Tools: Invision, SVN, Git, PhoneGap, Titanium, Xamarin, Sketch, xCode, IntelliJ IDEA