0% found this document useful (0 votes)
4 views

7 Design Patterns Java Structural m7 Slides

The Flyweight Pattern is designed for efficient memory use by managing a large number of similar immutable objects, where most states can be extrinsic. It involves a Factory and encompasses creation and structure through components like Client, Factory, Flyweight, and ConcreteFlyweight. While beneficial for memory management, it can be complex and requires a solid understanding of Factory patterns.

Uploaded by

podam91581
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

7 Design Patterns Java Structural m7 Slides

The Flyweight Pattern is designed for efficient memory use by managing a large number of similar immutable objects, where most states can be extrinsic. It involves a Factory and encompasses creation and structure through components like Client, Factory, Flyweight, and ConcreteFlyweight. While beneficial for memory management, it can be complex and requires a solid understanding of Factory patterns.

Uploaded by

podam91581
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Flyweight Pattern

Bryan Hansen
twitter: bh5k | http://www.linkedin.com/in/hansenbryan
Concepts
▪ More efficient use of memory
▪ Large number of similar objects
▪ Immutable
▪ Most of the object states can be extrinsic
▪ Examples:
▪ java.lang.String
▪ java.lang.Integer#valueOf(int)
▪ Boolean, Byte, Character, Short, Long
Design

Pattern of patterns
Utilizes a Factory
Encompasses Creation and Structure
Client, Factory, Flyweight, ConcreteFlyweight
UML
Everyday Example - Integer
Integer  firstInt  =  Integer.valueOf(5);  
     
Integer  secondInt  =  Integer.valueOf(5);  
     
Integer  thirdInt  =  Integer.valueOf(10);  
     
System.out.println(System.identityHashCode(firstInt));  
System.out.println(System.identityHashCode(secondInt));  
System.out.println(System.identityHashCode(thirdInt));
Exercise Flyweight

Inventory Management System


Client, Catalog, Order, Item
Pitfalls
▪ Complex pattern
▪ Premature optimization
▪ Must understand Factory
▪ Not a graphical pattern
Contrast

Flyweight Facade
▪ Memory Optimization ▪ Refactoring Pattern
▪ Optimization Pattern ▪ Simplified Client
▪ Immutable Objects ▪ Provides a different interface
Flyweight Summary

• Great for Memory Management


• A little bit complex
• Used a lot by the core API

You might also like