From the course: Practical Design Patterns in Swift
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Adapter using type extensions - Swift Tutorial
From the course: Practical Design Patterns in Swift
Adapter using type extensions
- [Instructor] In Swift, we can implement the adapter pattern, without creating a new wrapper type. The type extension is a powerful feature that allows adding protocol conformance to a type without modifying its source code. We can even enhance built-in and third-party types whose code is inaccessible for us. I'm going to declare an AmazonPayments extension that adopts the PaymentGateway protocol. Let me scroll up a bit. I insert the extension just before our adapter. Extension AmazonPayments and it should conform to the PaymentGateway protocol. I'm going to copy the receivePayment and the totalPayment calculated properties from the adapter. And now I can remove the wrapper. So, let's get rid of the AmazonPaymentsAdapter and we'll do some refactoring here. Instead of the AmazonPaymentsAdapter we'll be using AmazonPayments directly. And let's rename this constant as well. We have another occurrence, let's delete it. And now we don't have any compile issues any more. So, we managed to…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.