From the course: Practical Design Patterns in Swift
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Refactoring: Identifying the states - Swift Tutorial
From the course: Practical Design Patterns in Swift
Refactoring: Identifying the states
- [Instructor] We're going to refactor the CoffeeMachine class using the state design pattern. This class will act as a state machine from the caller's point of view. Clients will use the CoffeeMachine object, and should never interact directly with the state. I start by creating a protocol that defines the common interface for our states. We don't want to make this class available to clients, so I'll make it fileprivate, fileprivate protocol CoffeeMachineState. CoffeeMachineState will have the same methods as the CoffeeMachine class, so I add a method called isReadyToBrew, and a second method called Brew. The concrete states need to conform to this protocol. However, each state should only implement the interface that's relevant to that particular type, therefore I'm going to provide the default implementation for the CoffeeMachineState protocol in a protocol extension. These are dummy implementations that simply print a log console. So I'll just print that the method is not…
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.