From the course: Practical Design Patterns in Swift

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Classical Adapter

Classical Adapter - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Classical Adapter

- [Instructor] Let's start with an example to show you the benefits of the adapter pattern. You can follow along with me by opening the playground project from the exercise files folder, chapter five, five two, begin. I've implemented a family of PaymentGateway classes that conform to the PaymentGateway protocol. The protocol declares the receivePayment method and a read only calculated property called totalPayments. The PayPal and Stripe classes adopt the PaymentGateway protocol. The receivePayment method adds the input argument's value to the PrivateTotal property. The totalPayment calculated property outputs a log message and returns the accumulated total. You could use these types like this. You create an instance of the paypal class, and then call it receivePayment method. Same with the stripe class. You create an instance, and call its method. Since they conform to the same protocol, they have the same methods. Now, to calculate the total payments received through all the…

Contents