From the course: Java Design Patterns: Structural

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Understand the Flyweight pattern

Understand the Flyweight pattern - Java Tutorial

From the course: Java Design Patterns: Structural

Start my 1-month free trial

Understand the Flyweight pattern

- Suppose you have hundreds of small objects in your application that use uploads of memory. The Flyweight Pattern uses sharing to make apps like these work more efficiently. Imagine a program that's used for creating and editing text documents. A user opens a document and starts typing on it. In an object-oriented design, you could have a new objects for each character. So every time they typed an A, you would create a new A object. That would be a lot of objects. If they opened up a document that was hundreds of pages long, there might be a million separate letter objects. This situation might lead to the dreaded java.lang.OutOfMemory error. That's all Java developers are familiar with. Also in Java, it doesn't take long to create objects. In fact, it's actually pretty quick, but there is definitely an opportunity here to maximize performance. This is where the Flyweight Pattern can help. Instead of having a…

Contents