Design Patterns in iOS
Design patterns are typical solutions to commonly occurring problems in software design. They are like pre-made blueprints that you can customise to solve a recurring design problem in your code.
There are 3 different types of design patterns and each contains a set of patterns to achieve our goal. Every pattern has their own pros and cons, and its how we adapt and solve our problem wisely matters when it comes to ‘Design patterns ( There is always a difference of opinion it comes to choosing patterns)
These my POV on design patterns from my past experience.
As said there are 3 major types of patterns and will see those first and jump on to one by one.
- Creational : This deals with how we create objects and are responsible for their mechanism based on situation, improving reusability of code and flexibility.
- Structural : simplifying the design by assembling objects and classes into larger structures, while keeping these structures flexible and efficient.
- Behavioral : Identifies common communication patterns between entities and implement these patterns.
Now lets see what are the types of patterns among them:
Realised i can’t fit entire pattens in a blog. This blog only covers Factory, Singleton and Abstract.
Factory Method:
Factory Method is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
The Factory Method pattern suggests that you replace direct object construction calls with calls to a special factory method. The objects are still created via the new
operator, but it’s being called from within the factory method. Objects returned by a factory method are often referred to as products.
Above is an example of a method of creation object, the factory is responsible for creating the new product. Consumers just use the factory to make the product customised and use it through out the app.(Consumers here is the method that expects a new object).
Use the factory pattern whenever you want to separate out product creation logic, instead of having consumers create products directly.
A factory is very useful when you have a group of related products, such as polymorphic subclasses or several objects that implement the same protocol. For example, you can use a factory to inspect a network response and turn it into a concrete model subtype.
Singleton :
Singleton ensures that only one object of its kind exists and provides a single point of access to it for any other code. Singleton has almost the same pros and cons as global variables. Although they’re super-handy, they break the modularity of your code.
In my case, I’m more cautious about using singleton in my project i use it when i had a clear vision on the future of the object as it doesn’t need to be modified by external entities or having extensions or being a major subtle one i avoid it. The most important drawback of the singleton pattern is sacrificing transparency for convenience.
Creating a singleton was super easy and anyone can get onboarded easily, using them in a major app with proper idea and tracking it would be effective.
Builder Pattern :
Builder is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.
Explaining : Consider building a object which needs multiple data and are linked in a complex way, so it’s not achieved in a single way like in factory method. You may need to elaborate the process in factory and update the way. I feel builder is a extended factory method and acts like a real traditional factory. (Comical way)
Here is a small picture representation of how i used to achieve Builder Pattern
So, looking at the above diagram, you can see how builder is seprated from the codebase and the Point of contact for builder is with director which navigates te builder to make the object based on the requirement, above is a basic one but on real project the code and amount of classes used would really high(I would say it as a dis advantage ). But it does the work efficiently.
For all these patterns there are few example in my Github. Please do have a look.
Feel free to comment if you feel struck on any block. Let me know in comments if you felt it’s useful and feedbacks are welcome 🙌.
In my next blog i would be writing about Behavioural patterns.
Follow me on other social networks too.
LinkedIn: https://www.linkedin.com/in/shreethaanu-raveendran-7a6275b2/
Github : https://github.com/shreethaanu
Instagram : https://www.instagram.com/shreethaanu_blogs/
Portfolio : https://strlabz.com
Patreon : https://www.patreon.com/shreethaanu
If you like my post and wanna see more support me ☕️