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 strategy pattern


When you need to change part of an object's algorithm at runtime, without modifying the client, the strategy pattern is the appropriate pattern to be used.

It removes an algorithm from its host class and moves it to a separate class. The algorithm part that can change is the strategy. Every strategy uses the same interface. The class using the strategy pattern delegates the treatment of the algorithm to the strategy.

Roles

The strategy pattern is used to create an interchangeable family of algorithms from which the required process is chosen at runtime.

The algorithm changes don't affect the client part. This pattern can be used in the following cases:

  • The behavior of a class can be implemented by different algorithms where some of them are better in terms of execution time or memory consumption

  • Choosing the appropriate algorithm with if conditions instruction complexify the code

  • A system has similar classes where only the behavior changes; in this case, the strategy pattern...

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