From the course: Practical Design Patterns in Swift
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Aim: Reduce complex conditional logic - Swift Tutorial
From the course: Practical Design Patterns in Swift
Aim: Reduce complex conditional logic
- [Instructor] The state design pattern allows an object to behave differently when its internal state changes. Instead of relying on complex conditional logic to manage internal state changes, we define an object-oriented state machine. The state pattern encapsulates the conditional behavior into dedicate types. This approach leads to a more flexible system and makes our code easier to maintain and extend. To apply the state pattern, we first need to identify the steps that change the behavior of an object. After that, we encapsulate the steps into dedicated state types. One of the common pitfalls is exposing the states to clients. The clients must not interact with the states directly. I'm going to illustrate the benefits of the state design pattern by getting rid of the complex conditional logic from an existing project.
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.