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

Aim: Reduce complex conditional logic - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

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.

Contents