top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

When to use Flyweight pattern in Java?

+1 vote
224 views
When to use Flyweight pattern in Java?
posted Oct 24, 2016 by Farhan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

We need to consider following factors when choosing flyweight,

Need to create large number of objects.
Because of the large number when memory cost is a constraint.

When most of the object attributes can be made external and shared.

The application must not mandate unique objects, as after implementation same object will be used repeatedly.

Its better when extrinsic state can be computed rather than stored. (explained below)

Flyweight is all about memory and sharing. Nowadays an average desktop comes with 500 GB hard disk, 4GB ram and with this you can stuff your whole home inside and will still have remaining space to put an elephant in it. Do we really need to bother about memory and usage? Since the cost has come down there is no restriction to use it effectively. Think about mobile devices that are increasing everyday and they still have memory constraint.

Even if you have huge memory, in some cases the application may need efficient use of it. For example assume we are working with an application that maps stars from universe. In this application if we are going to create an object for every star then think of it how much memory we will need. Gang of Four (GoF) have given an example of text editors in their book. If we create an object for every character in a file, think of it how many objects we will create for a long document. What will be the application performance.

answer Oct 24, 2016 by Karthick.c
Similar Questions
0 votes

Given,
the total levels in a hill pattern (input1),
the weight of the head level (input2), and
the weight increments of each subsequent level (input3),
you are expected to find the total weight of the hill pattern.

"Total levels" represents the number of rows in the pattern.
"Head level" represents the first row.
Weight of a level represents the value of each star (asterisk) in that row.

The hill patterns will always be of the below format, starting with 1 star at head level and increasing 1 star at each level till level N.

*
**
***
****
*****
******

. . .and so on till level N

Example1 -
Given,
the total levels in the hill pattern = 5 (i.e. with 5 rows)
the weight of the head level (first row) = 10
the weight increments of each subsequent level = 2
Then, The total weight of the hill pattern will be calculated as = 10 + (12+12) + (14+14+14) + (16+16+16+16) + (18+18+18+18+18) = 10 + 24 + 42 + 64 + 90 = 230

Example2 -
Given,
the total levels in the hill pattern = 4
the weight of the head level = 1
the weight increments of each subsequent level = 5
Then, Total weight of the hill pattern will be = 1 + (6+6) + (11+11+11) + (16+16+16+16) = 1 + 12 + 33 + 64 = 110

+2 votes

I create a class named ErrorUtil that each Package has a cl1ass named Error that inherited from ErrorUtil. All Exception and Logical Error catch at these class.

Is my pattern suitable or Not? Is there any pattern be better?

...