Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Swift 2 Design Patterns

You're reading from   Swift 2 Design Patterns Build robust and scalable iOS and Mac OS X game applications

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher
ISBN-13 9781785887611
Length 224 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Julien Lange Julien Lange
Author Profile Icon Julien Lange
Julien Lange
Arrow right icon
View More author details
Toc

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:

Design

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...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image