Book Image

Swift 2 Design Patterns

By : Julien Lange
Book Image

Swift 2 Design Patterns

By: Julien Lange

Overview of this book

Table of Contents (15 chapters)
Swift 2 Design Patterns
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The state pattern


In the state pattern, a class behavior changes based on its state. This type of design pattern comes under the behavior pattern.

In the state pattern, we create objects that represent various states and a context object whose behavior varies as its state object changes.

Role

The role of this pattern is to adapt its behavior depending on the internal state of an object. It can be used when implementing the dependency of the state object if the condition statement becomes complex.

Design

The generic class diagram structure of the state pattern is as follows:

Participants

The participants in the state pattern are as follows:

  • StateMachine: This is a concrete class that describes a state machine's objects, which means that they have a set of states that can be described in a state transition diagram. This class has a reference to an instance of a sub class that implements the state abstract class and defines the current state.

  • IState: This is an abstract class that introduces you to...