Flyweight pattern
, often use the flyweight pattern. In computer programming, the flyweight software design pattern refers to an object that minimizes memory usage by sharing some of its data with other similar objects. The flyweight pattern is one of twenty-three GoF design patterns. In other contexts, the idea of sharing data structures is called hash consing. The concept was coined and first extensively explored by Paul Calder and Mark Linton in 1990 to efficiently handle glyph information in a WYSIWYG document editor. Similar techniques were already used in other systems, however, as early as 1988.
, often use the flyweight pattern. In computer programming, the flyweight software design pattern refers to an object that minimizes memory usage by sharing some of its data with other similar objects. The flyweight pattern is one of twenty-three GoF design patterns. In other contexts, the idea of sharing data structures is called hash consing. The concept was coined and first extensively explored by Paul Calder and Mark Linton in 1990 to efficiently handle glyph information in a WYSIWYG document editor. Similar techniques were already used in other systems, however, as early as 1988. ==Overview== The flyweight pattern is useful when dealing with a large number of objects that share simple repeated elements which would use a large amount of memory if they were individually embedded. It is common to hold shared data in external data structures and pass it to the objects temporarily when they are used. A classic example are the data structures used representing characters in a word processor. Naively, each character in a document might have a glyph object containing its font outline, font metrics, and other formatting data. However, this would use hundreds or thousands of bytes of memory for each character. Instead, each character can have a reference to a glyph object shared by every instance of the same character in the document. This way, only the position of each character needs to be stored internally. As a result, flyweight objects can: store intrinsic state that is invariant, context-independent and shareable (for example, the code of character 'A' in a given character set) provide an interface for passing in extrinsic state that is variant, context-dependent and can't be shared (for example, the position of character 'A' in a text document) Clients can reuse Flyweight objects and pass in extrinsic state as necessary, reducing the number of physically created objects. // Defines Flyweight object that repeats itself. public class Flyweight { public string Name { get; set; } public string Location { get; set; } public string Website { get; set; } public byte[] Logo { get; set; } } public static class Pointer { public static readonly Flyweight Company = new Flyweight { Name = "ABC", Location = "XYZ", Website = "www.example.com" }; } public class MyObject { public string Name { get; set; } public string Company => Pointer.Company.Name; } import std; template using TreeMap = std::map ; using String = std::string; using StringView = std::string_view; template using HashMap = std::unordered_map ; // Instances of Tenant will be the Flyweights class Tenant { private: const String name; public: explicit Tenant(StringView name): name{name} {} nodiscard String getName() const noexcept { return name; } }; // Registry acts as a factory and cache for Tenant flyweight objects class Registry { private: HashMap tenants; public: Registry() = default; nodiscard Tenant& findByName(StringView name) { if (!tenants.contains(name)) { tenants[name] = Tenant{name}; } return tenants[name]; } }; // Apartment maps a unique tenant to their room number. class Apartment { private: TreeMap occupants; Registry registry; public: Apartment() = default; void addOccupant(StringView name, int room) { occupants[room] = ®istry.findByName(name); } void printTenants() { // room: int, tenant: Tenant for (const auto& [room, tenant] : occupants) { std::println("{} occupies room {}", tenant.name(), room); } } }; int main(int argc, char* argv[]) { Apartment apartment; apartment.addOccupant("David", 1); apartment.addOccupant("Sarah", 3); apartment.addOccupant("George", 2); apartment.addOccupant("Sarah", 12); apartment.addOccupant("Michael", 10); apartment.printTenants(); return 0; } name; } } class Order { private function __construct( private CoffeeFlavour $flavour, private int $tableNumber ) {} public static function create(string $flavourName, int $tableNumber): self { $flavour = CoffeeFlavour::intern($flavourName); return new self($flavour, $tableNumber); } public function __toString(): string { return "Serving {$this->flavour} to table {$this->tableNumber}"; } } class CoffeeShop { private array $orders = []; public function takeOrder(string $flavour, int $tableNumber) { $this->orders[] = Order::create($flavour, $tableNumber); } public function service() { print(implode(PHP_EOL, $this->orders).PHP_EOL); } } $shop = new CoffeeShop(); $shop->takeOrder("Cappuccino", 2); $shop->takeOrder("Frappe", 1); $shop->takeOrder("Espresso", 1); $shop->takeOrder("Frappe", 897); $shop->takeOrder("Cappuccino", 97); $shop->takeOrder("Frappe", 3); $shop->takeOrder("Espresso", 3); $shop->takeOrder("Cappuccino", 3); $shop->takeOrder("Espresso", 96); $shop->takeOrder("Frappe", 552); $shop->takeOrder("Cappuccino", 121); $shop->takeOrder("Espresso", 121); $shop->service(); print("CoffeeFlavor objects in cache: ".CoffeeFlavour::flavoursInCache().PHP_EOL); ==See also== Copy-on-write Memoization Multiton
References
- Gamma, Erich ; Richard Helm ; Ralph Johnson ; John Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software . Addison-Wesley . pp. 205–206 . ISBN 978-0-201-63361-0 . Wikipedia revision 1361148690, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1361148690
Cite this reference
MLA "Gamma, Erich ; Richard Helm ; Ralph Johnson ; John Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software . Addison-Wesley . pp. 205–206 . ISBN 978-0-201-63361-0 . Wikipedia revision 1361148690, retrieved 2026-11-18." Gamma, Erich ; Richard Helm ; Ralph Johnson ; John Vlissides (1995). 2026-11-17. https://en.wikipedia.org/wiki/Erich_Gamma. Accessed 24 November 2026.
Chicago "Gamma, Erich ; Richard Helm ; Ralph Johnson ; John Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software . Addison-Wesley . pp. 205–206 . ISBN 978-0-201-63361-0 . Wikipedia revision 1361148690, retrieved 2026-11-18." Gamma, Erich ; Richard Helm ; Ralph Johnson ; John Vlissides (1995). 2026-11-17. https://en.wikipedia.org/wiki/Erich_Gamma.
APA Gamma, Erich ; Richard Helm ; Ralph Johnson ; John Vlissides (1995). (2026). Gamma, Erich ; Richard Helm ; Ralph Johnson ; John Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software . Addison-Wesley . pp. 205–206 . ISBN 978-0-201-63361-0 . Wikipedia revision 1361148690, retrieved 2026-11-18. Retrieved November 24, 2026, from https://en.wikipedia.org/wiki/Erich_Gamma
- Glyphs: Flyweight Objects for User Interfaces · retrieved 2026-11-17 · rev 1361148690
Cite this reference
MLA "Glyphs: Flyweight Objects for User Interfaces." Calder, Paul R. 2026-11-17. https://en.wikipedia.org/wiki/ACM_SIGGRAPH. Accessed 24 November 2026.
Chicago "Glyphs: Flyweight Objects for User Interfaces." Calder, Paul R. 2026-11-17. https://en.wikipedia.org/wiki/ACM_SIGGRAPH.
APA Calder, Paul R. (2026). Glyphs: Flyweight Objects for User Interfaces. Retrieved November 24, 2026, from https://en.wikipedia.org/wiki/ACM_SIGGRAPH
- Weinand, Andre; Gamma, Erich; Marty, Rudolf (1988). ET++—an object oriented application framework in C++ . OOPSLA (Object-Oriented Programming Systems, Languages and Applications). San Diego, California, United States. pp. 46– 57. CiteSeerX 10.1.1.471.8796 . doi : 10.1145/62083.62089 . ISBN 0-89791-284-5 . {{ cite conference }} : Cite uses deprecated parameter |citeseerx= ( help ) Wikipedia revision 1361148690, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1361148690
Cite this reference
MLA "Weinand, Andre; Gamma, Erich; Marty, Rudolf (1988). ET++—an object oriented application framework in C++ . OOPSLA (Object-Oriented Programming Systems, Languages and Applications). San Diego, California, United States. pp. 46– 57. CiteSeerX 10.1.1.471.8796 . doi : 10.1145/62083.62089 . ISBN 0-89791-284-5 . {{ cite conference }} : Cite uses deprecated parameter |citeseerx= ( help ) Wikipedia revision 1361148690, retrieved 2026-11-18." Weinand, Andre; Gamma, Erich; Marty, Rudolf (1988). 2026-11-17. https://en.wikipedia.org/wiki/OOPSLA. Accessed 24 November 2026.
Chicago "Weinand, Andre; Gamma, Erich; Marty, Rudolf (1988). ET++—an object oriented application framework in C++ . OOPSLA (Object-Oriented Programming Systems, Languages and Applications). San Diego, California, United States. pp. 46– 57. CiteSeerX 10.1.1.471.8796 . doi : 10.1145/62083.62089 . ISBN 0-89791-284-5 . {{ cite conference }} : Cite uses deprecated parameter |citeseerx= ( help ) Wikipedia revision 1361148690, retrieved 2026-11-18." Weinand, Andre; Gamma, Erich; Marty, Rudolf (1988). 2026-11-17. https://en.wikipedia.org/wiki/OOPSLA.
APA Weinand, Andre; Gamma, Erich; Marty, Rudolf (1988). (2026). Weinand, Andre; Gamma, Erich; Marty, Rudolf (1988). ET++—an object oriented application framework in C++ . OOPSLA (Object-Oriented Programming Systems, Languages and Applications). San Diego, California, United States. pp. 46– 57. CiteSeerX 10.1.1.471.8796 . doi : 10.1145/62083.62089 . ISBN 0-89791-284-5 . {{ cite conference }} : Cite uses deprecated parameter |citeseerx= ( help ) Wikipedia revision 1361148690, retrieved 2026-11-18. Retrieved November 24, 2026, from https://en.wikipedia.org/wiki/OOPSLA
- Implementing Flyweight Patterns in Java · retrieved 2021-06-11 · rev 1361148690
Cite this reference
MLA "Implementing Flyweight Patterns in Java." 2021-06-11. https://www.developer.com/design/implementing-flyweight-patterns-in-java/. Accessed 24 November 2026.
Chicago "Implementing Flyweight Patterns in Java." 2021-06-11. https://www.developer.com/design/implementing-flyweight-patterns-in-java/.
APA encyclopegia.org. (2021). Implementing Flyweight Patterns in Java. Retrieved November 24, 2026, from https://www.developer.com/design/implementing-flyweight-patterns-in-java/
- The Flyweight design pattern - Structure and Collaboration · retrieved 2017-08-11 · rev 1361148690
Cite this reference
MLA "The Flyweight design pattern - Structure and Collaboration." 2017-08-11. http://w3sdesign.com/?gr=s06&ugr=struct. Accessed 24 November 2026.
Chicago "The Flyweight design pattern - Structure and Collaboration." 2017-08-11. http://w3sdesign.com/?gr=s06&ugr=struct.
APA encyclopegia.org. (2017). The Flyweight design pattern - Structure and Collaboration. Retrieved November 24, 2026, from http://w3sdesign.com/?gr=s06&ugr=struct
- Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software . Addison Wesley. pp. 195ff . ISBN 978-0-201-63361-0 . {{ cite book }} : CS1 maint: multiple names: authors list ( link ) Wikipedia revision 1361148690, retrieved 2026-11-18 · retrieved 2026-11-17 · rev 1361148690
Cite this reference
MLA "Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software . Addison Wesley. pp. 195ff . ISBN 978-0-201-63361-0 . {{ cite book }} : CS1 maint: multiple names: authors list ( link ) Wikipedia revision 1361148690, retrieved 2026-11-18." Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). 2026-11-17. https://archive.org/details/designpatternsel00gamm/page/195. Accessed 24 November 2026.
Chicago "Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software . Addison Wesley. pp. 195ff . ISBN 978-0-201-63361-0 . {{ cite book }} : CS1 maint: multiple names: authors list ( link ) Wikipedia revision 1361148690, retrieved 2026-11-18." Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). 2026-11-17. https://archive.org/details/designpatternsel00gamm/page/195.
APA Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). (2026). Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software . Addison Wesley. pp. 195ff . ISBN 978-0-201-63361-0 . {{ cite book }} : CS1 maint: multiple names: authors list ( link ) Wikipedia revision 1361148690, retrieved 2026-11-18. Retrieved November 24, 2026, from https://archive.org/details/designpatternsel00gamm/page/195
Cite this article
MLA
"Flyweight pattern." encyclopegia.org, 2026, https://encyclopegia.org/wiki/flyweight-pattern. Accessed 24 November 2026.
Chicago
"Flyweight pattern." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/flyweight-pattern.
APA
encyclopegia.org. (2026). Flyweight pattern. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/flyweight-pattern
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON