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: Implementing the states

Refactoring: Implementing the states - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Refactoring: Implementing the states

- [Instructor] All states but the Standby state need to access the coffee machine object to query its internal state. So let's add the context property to all the states that need to access the context. The context is of type CoffeeMachine. And I'll just copy and paste it to all the other states. Now let's start implementing our states. The FillWaterTankState should implement the isReadyToBrew method. I print a warning message if the water tank is empty, guard, and I query the context isWaterTankFilled property. We can't access it currently because it has a private protection level. So let's change that to fileprivate. And I'm going to do that for the other two properties as well. If the water tank is empty, I print a warning message. Then, I switch the machine's state to Standby mode and finally return false. This way, we indicate that there was an issue. So context state should be Standby. And let's return false. If the water tank is not empty, we switch to the next state that…

Contents