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

Solution: Removing tight coupling

From the course: Swift 5: Protocol-Oriented Programming

Start my 1-month free trial

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…

Contents