Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Make your apps fly

Implement Flyweight to improve performance

Justified or not, Java is sometimes perceived as slow. Compared with other languages—such as C++—that are compiled into machine code, Java is interpreted at runtime; and all other things being equal, compiled machine code is faster than interpreted byte code. Of course, you might argue that Java can hold its own, mainly due to advanced technologies such as just-in-time (JIT) compilers and Java HotSpot, and you'd be mostly correct. Today's JVMs are modern marvels that wring amazing performance out of Java byte code.

Regardless of modern JVM efficiency, you should always look for programming techniques that boost performance. In this installment of Java Design Patterns, I discuss one of those techniques that, not surprisingly, takes the form of a design pattern: the Flyweight pattern. Let's see how it works.

Note: You can download this article's source code from Resources.

The Flyweight pattern

Object-oriented design is sometimes at odds with performance. From a design standpoint, it's best to encapsulate everything in an object so you can treat those objects uniformly, reuse them, and extend them. Unfortunately, modeling everything as an object can be expensive performance-wise. A case-in-point is Smalltalk, which literally models everything as an object, as opposed to Java, which provides a mix of objects and intrinsic types. Today, Java is much more prevalent than Smalltalk, due in no small part to Java's better performance.

Note: Java also provides static type checking, which Smalltalk lacks. Although static type checking is widely regarded as beneficial, most Smalltalk developers argue against it.

Even though Java provides a mix of objects and intrinsic types, Java applications can create a huge number of objects. For example, trees can have an unlimited number of rows, so if you model each row as a distinct Swing component, you're asking for trouble. For fine-grained objects such as tree nodes, you can implement the Flyweight pattern to create a small pool of shared objects, which significantly reduces the number of objects created. Soon enough, you'll learn how to implement flyweights and share them; in the meantime, it's sufficient to understand that flyweights are shared objects and that using them can result in substantial performance gains.

In Design Patterns, the Gang of Four (GOF) authors describe the Flyweight pattern like this:

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


Figure 1 shows a class diagram for the Flyweight design pattern.

Figure 1. Flyweight class diagram

Flyweights are typically instantiated by a flyweight factory that creates a limited number of flyweights and doles them out, one at a time, to clients. Those flyweights are instantiated according to some criteria. For example, you might have a pool of line objects that know how to draw lines. In that case, the flyweight factory could create one line object for each line color, such as one object for white lines and another for blue lines. Those lines, which are flyweights, get reused whenever you draw white or blue lines. If you have a drawing with 1,000 white lines and 6,000 blue lines, only two lines—instead of 7,000—are actually instantiated.

1 | 2 | 3 | 4 |  Next >

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. Good article...but...
By Anonymous
0 10/05/06 10:10 AM
by Anonymous
. speed is the same?
By ApeHanger
1 10/05/06 10:09 AM
by Anonymous
. Very good explanation of flyweight!
By Anonymous
1 10/05/06 10:09 AM
by Anonymous
. Good Examples for a Pattern
By vik
0 10/05/06 08:15 AM
by Anonymous
. Smalltalk better - Java was FREE
By Anonymous
2 10/05/06 05:57 AM
by Anonymous
. Static typing beneficial?
By Anonymous
7 10/05/06 05:56 AM
by Anonymous
. Not usually applicable
By Bret Hansen
4 10/05/06 05:10 AM
by Anonymous
. good job
By Shahzaib
1 10/05/06 02:01 AM
by Anonymous
. Not all Borders in BorderFactory are Flyweights
By me-kell
0 10/05/06 02:00 AM
by Anonymous
. Very good article
By Anonymous
3 10/04/06 09:17 AM
by Anonymous
. and about the components like JTextField
By fabio lanza
2 10/04/06 05:38 AM
by Anonymous
. greate
By Anonymous
1 10/03/06 12:53 PM
by Anonymous
. String was amazing
By Anonymous
1 10/03/06 11:14 AM
by Anonymous
. Excellent article, poor example
By Philippe Lhoste
0 06/26/05 05:47 AM
by Anonymous
. very good article
By Anonymous
0 07/29/03 09:23 AM
by Anonymous
. Comments about Smalltalk misinformed
By Anonymous
0 07/28/03 07:48 AM
by Anonymous


Resources