msgbartop
Code that explodes conventions
msgbarbottom

16 Apr 11 Let your application fly

Yes, today I’m with an interesting design pattern that is flyweight pattern. Here we are going to make our application fly and this is the motivation. We will make your application ready for fly, so it’s needed to make sure that our application has very little weight.

Let us think for an application which is going to draw 10000 lines for different colors. So we need 1000 line objects right? Let us assume, creating a line object takes 100 milliseconds. So for 10000 lines, we need 1000000 milliseconds that mean we need 1000 seconds and it’s kind of huge. So first let us make a demo application.

Let us create a class named Line.


import java.awt.Color;
import java.awt.Graphics;

public class Line {
private Color color;

public Line(Color color) {
this.color = color;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void draw(Graphics g, int x1, int y1, int x2, int y2) {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
}

Here I used Thread.sleep(100) as we have assumed that every line will take 100 milliseconds to be instantiated.

And now let us create a Demo:

 

Flyweig pattern Demo

Flyweig pattern Demo

So this is our application and it takes approximately 16 minutes that is very huge time. Now I will make this application in a way so that it can fly. So it’s needed to be created so fast like within a seconds. Here I’m going to use a design pattern that is called flyweight pattern. So first let us make an interface named Line.

import java.awt.Graphics;
public interface Line {

public void draw(Graphics g, int x1, int y1, int x2, int y2);
}

Now let us make a concrete class implementing this interface.


import java.awt.Color;
import java.awt.Graphics;

public class MyLine implements Line {
private Color color;

public MyLine(Color color) {
this.color = color;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Override
public void draw(Graphics g, int x1, int y1, int x2, int y2) {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
}

Now let us make Factory class that is named MyLineFactory.

import java.awt.Color;
import java.util.HashMap;

public class MyLineFactory {
private static MyLineFactory myLineFactory;
private static HashMap lineByColor;

private MyLineFactory() {
lineByColor = new HashMap();
}

public static MyLineFactory getMyLineFactory() {
if (myLineFactory == null) {
myLineFactory = new MyLineFactory();
}
return myLineFactory;
}

public Line getLine(Color color) {
if (lineByColor.containsKey(color)) {
return (Line) lineByColor.get(color);
} else {
Line line = new MyLine(color);
lineByColor.put(color, line);
return line;
}
}
}

This MyLineFactory itself implements two design pattern, they are Singleton pattern and Factory Pattern.

Now if we make a demo, I have calculated that it takes approximately 1200 milliseconds only. So it’s better approach right?

Now let me define what actually Flyweight pattern. According to Gang of Four Design pattern book, it

Facilitates the reuse of many fine grained objects, making the utilization of large numbers of objects more efficient.

Now let me tell you my understanding. I have already told you that, we need to make your application very light weighted. So there should be a way. And that is reusing an object again and again. In this application, we have drawn 10000 line objects with 5 colors. So the probability of the same color in the 10000 line is 2000. For example, there will be 2000 red colored line. So if we create an object of red color, and reuse it 2000 times, then for 5 colors, we can make our application with only 5 line objects instead of 10000 line objects.

So to implement flyweight pattern we need 4 participants. They are –

Flyweight: It’s an interface that defines the main behavior. In our application we created an interface named Line and it contains a method named draw.

Concrete Flyweight: it’s a class that is going to implement the flyweight interface. In your application, it is MyLine.

Flyweight Factory: This is very important part. Here we use two major design patterns, singleton and Factory Pattern. In this class we will have an object pool. The object pool holds flyweight objects with a key. Whenever we need an object, we search it in the object pool. If the object is available here in the object pool, we will take it; otherwise we will create one and push into the object pool so that whenever we need this object again, we can get it from it.

Client: And here we are going to do our rest of work.

Source Code: Here you will get two version of source code, one using flyweight and another non flyweight version.

Click This Link

Reference :

[http://java.dzone.com/articles/design-patterns-flyweight]

[http://www.javaworld.com/javaworld/jw-07-2003/jw-0725-designpatterns.html?page=1]

[http://cnx.org/content/m26103/latest/]

Loading...
Join thousands and get updates for free...No-Spam Guarantee