Flyweight, and your application will fly..


The flyweight design pattern is super simple, yet it slips out the avg’ developer mind..

It’s one of the GoF patterns, a structural one..

What is GoF?

GoF, Gang of Four, are four intelligent writers, who wrote the coolest, most popular design patterns book in the 90’s.

Most of them are now working in the IBM research department..

The book is all about OO design patterns, I personally admire them and their book, hopefully one day(soon!!) I will write my own book with a Gang of N 🙂 wanna join?!

What is a structural design pattern?

Those are design patterns who deals with the structure of classes and the relationship between them.

The patterns make it easier for us to plan the classes and a correct form, which increase performance and scalability.

The Flyweight pattern

When we plan a class, and we are sure that there will be many instances of it in an application, we must stop and think if some of the data (variables content) will be identical for many of the instances. In that case it’s obvious that such a case will waste a lot of heap space, right?

The solution is simple, we should re factor the identical variables to an external class (Flyweight) and add a reference to it from the original class.

The most common example is a word processor: Your are in a job interview and the interviewer ask you to design a word processor, you will probably talk about a Char class, the Char class holds font, size and color variables, is that so? NO! those variables are identical for many chars that resides next to each other…the solution is to use the Flyweight pattern, all Chars in the same group of style should reference to a Flyweight object,CharLayout.

In order to reference each char to the correct CharLayout, one would use the factory pattern..

Some other examples: duplicated UI controls (buttons, icons, panels etc..), data with externic variable, Distributed tree nodes etc..

Benefits Reduce the number of objects created, decrease memory footprint and increase performance.

Advertisements

3 thoughts on “Flyweight, and your application will fly..

  1. A not secondary property is that value object like the ones you describe here must be immutable.
    A Java String object is an example of immutable object: it can be shared by many business object and no one can edit it. If a change has to be made, a new String is created.

  2. Pingback: Speed-up your java code « Simple Notions

  3. asbestos cancer lawyer How-do-you-do, just required you to know I’ve extra your web site to my Google bookmarks as a consequence of your incredible website format. But severely, I think your web site has one of many freshest topic I’ve discovered. It actually assists make reading through your website a great deal simpler.asbestos lawyers Pennsylvania

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s