© Kelt Dockins 2017

Kelt Dockins, Design Patterns in PHP and Laravel, 10.1007/978-1-4842-2451-9_14

14. Flyweight

Kelt Dockins

(1)Dolph, Arkansas, USA

$> git checkout flyweight

Intent

Use sharing to support large numbers of fine-grained objects efficiently.1

Applications

The flyweight pattern is used to reduce memory footprint. You will find this pattern helpful whenever you are dealing with a large number of objects that share the same properties or a few objects that share the same large-size data (e.g. images). To illustrate, say you have a Person object that has properties like name, gender, and phone number. Assume the average memory cost for each Person object is 1,000 bytes. When you allocate 100,000 Person objects , you end up with 100 million ...

Get Design Patterns in PHP and Laravel now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.