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
Newsletter 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 flyweight pattern

This pattern can be used when the system needs to deal with a large number of similar objects. Instead of creating each element one by one, this pattern permits you to reuse an object that shares the same data.

Roles

The flyweight pattern is used to reduce the memory and resource usage of complex models that contain many hundreds and thousands of similar objects by reducing the number of objects created. It tries to reuse similar existing objects or creates a new one when no match is found.

This pattern can be used when:

  • We need to manipulate a lot of small similar objects
  • The cost (the memory/execution time) of this manipulation is high

Design

The following class diagram represents the generic structure of the pattern:

Design

Participants

There are three participants to the flyweight pattern, as follows:

  • Flyweight: This declares an interface that contains an intrinsic state and implements methods. These methods can receive and act on the extrinsic state of the flyweights.
  • FlyweightFactory...
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