Wrapper design pattern by example using SFAuthenticationSession and ASWebAuthenticationSession
Long, long time ago (a couple of years ;)) I was reading a book called Design Patterns: Elements of Reusable Object-Oriented Software. I was a young software developer then and I really liked that book. But there was a problem. I wasn’t able to use its potential because I didn’t have enough experience to know how to use those pattern correctly.
Recently I heard a lot of comments that some people after reading that book start using most of those pattern in any place they think they could use it. This is of course wrong. When you are young developer it’s really nice to know that that book exists and after some time of developing software you realising that you are using those pattern naturally and quite often you are giving them names different then you can find in any book of design patterns.
Today I would like to talk about one of design patterns called Wrapper. Wikipedia says something like this about it: “Wrapper pattern is a software design pattern that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.”
The problem we are facing working with
ASWebAuthenticationSession is as follows:
SFAuthenticationSession was introduces in iOS 11 and deprecated in iOS 12.
ASWebAuthenticationSession was introduced in iOS 12.
If you are lucky as an iOS application developers and you have to support at most one previous version of iOS there is some nice way how to hide usage of those classes under one simple class.
The really nice property of those two implementations is the fact that they have the same methods with the same signatures. We can use that fact and create a protocol with them:
Having that protocol we can extend
ASWebAuthenticationSession and add conformance to
AuthenticationSessionProtocol and because the fact that those classes have those methods implemented already we don’t have to do anything else.
Now we cen create our wrapper class which will also conform to
To be honest that’s it. We have simple wrapper which nicely hides usage of
ASWebAuthenticationSession and nicely adapts to the system version which is installed on a device which is running our application.
My github: https://github.com/adiki/
Next article: A way to handle NSNotification’s observation token
Previous article: How to pollute your code with external dependency (and how to not do that)?