From the course: Swift 5: Protocol-Oriented Programming
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Solution: Removing tight coupling
From the course: Swift 5: Protocol-Oriented Programming
Solution: Removing tight coupling
(upbeat music) - [Narrator] Welcome back. Did you manage to get rid of the tight coupling between the payment controller and the amazon service and etsy service classes? Now, here's how I solved this problem. Let's take a look at the two service classes. Both classes define the same functionality. They defer only in their property and method names. By creating a common protocol that defines these requirements, we introduce a level of extraction. The first step toward a loosely coupled system. So, I create a public protocol called "service". I'm going to declare the property that represents the total profit, first. I call it "total", is of type "float", and it is gettable. The add method increases the total by the amount provided in the payment argument. We need a payment parameter of type "float", and that's our method. I have to make it mutating, because it modifies the instance. Next, I make the amazon service…
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.
Contents
-
-
-
What is protocol-oriented programming?2m 15s
-
Introducing the protocol5m 18s
-
Adopting protocols1m 54s
-
Conforming to protocols via extensions3m 38s
-
Understanding polymorphism4m
-
Protocol inheritance3m 41s
-
Adopting multiple protocols5m 50s
-
Protocol extensions3m 23s
-
Challenge: Removing tight coupling3m 13s
-
Solution: Removing tight coupling8m 33s
-
-
-
-
-