Design Patterns in iOS — part 3
In my previous blogs i have written about creational and behavirol pattern, If you haven’t visited it yet ? Have a look on it to have an over all understanding about what we are going to discuss here.
In this blog, I’m writing about structural design patterns and how i used, why i chose that and what are the issues i faced while using it.
Structural Design Patterns :
We use them for simplifying the design by assembling objects and classes into larger structures, while keeping these structures flexible and efficient.
Here I’ll be discussing about
- Adapter
- Decorator
- Composite
Adapter pattern :
In this pattern we introduce a module named adapter, which actually helps in customising the objects without troubling the object. So how we do it ? Assume you have an object that needs to be converted into specific type that the object isn’t aware of and you want it to happen at specific point, So you can introduce a adapter that adapts the object into how we wanted it.
This actually removes a huge block of code that isn’t a part of object but its characteristics giving us the room to segregate the object code and its adaption and this improves the de clustering and make it more testable, generic. I personally used adapter for this reason.
So adapter can transform one thing into another, sometimes it’s called wrapper, because it wraps the object and provides a new interface around it. It’s like a software dongle for specific interfaces or legacy classes.
Here is a quick understanding of Adaptee, Adapter and Target.
There is a basic example of how we can achieve the adapter pattern in swift. Here is the link to my playground.
Decorator:
This pattern lets you add behaviours to object, that helps to modify the type of the object. Assume you have a base object, that needs to modify its behaviour and also needs to unique and not applied all over. So here we introduce a thing called wrapper (Its what inside a decorator), this wraps the object and manipulate the behaviour and give us the freedom to use the object based on the need.
Here the person is an object and the attire wearing that helps him to adapt to the climate is the wrapper. so he can remove and add based on the needs and does his job.
In our work scenario we introduce wrapper class those are responsible to change the behaviour of the object and ends in doing the specific task.Most developers have encountered this situation and you may have used it without knowing there is something its named after (I’ve at least). But on a high level perspective we do decorator using common implementations Extensions and Delegation.
I choose decorator pattern, as it avoids the chaos of adding a heap of code on to object thats not a single responsibility principle, on some places i segregate the behaviours as a generic component and used it as a wrapper that allows me to extend it.
here is a basic example using extension and delegation for decorator pattern.
Composite:
Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects.
To be frank its one of the most complex pattern i came across and the use case is too much limited based on the app your building. Using it without a proper structure may end up creating a large heap/chunk of objects those are not relatable but are the part of the object or component.
On a high level it adds the similar patterns in a tree like structure that has the branches consist of of objects those consist of sub classes.
The major breakthrough i found using this is it won’t break your code whenever u introduce a new behaviour its a solid work, but that may be a huge disadvantage too i would be difficult to provide a common interface whose functionality differs too much. Also it adapts the SOLID Principle.
Here is a code i found in the internet that would explain the composite pattern and how we achieve it in swift.
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 🙌.
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 ☕️