Flyweight Design Pattern Tutorial

Flyweight Design Pattern TutorialWelcome to my Flyweight Design Pattern Tutorial! The flyweight design pattern is used to dramatically increase the speed of your code when you are using many similar objects.

To reduce memory usage the flyweight design pattern shares Objects that are the same rather than creating new ones. In this tutorial, I’ll create 100,000 rectangles and show you the difference in speed versus creating unique rectangle objects versus the flyweight design. The code follows to help you learn.

If you like videos like this it helps to tell Google [googleplusone]

Share if you know anyone who would be interested

Code from the Video


// The Flyweight design pattern is used when you need to
// create a large number of similar objects

// To reduce memory this pattern shares Objects that are 
// the same rather than creating new ones

import javax.swing.*;

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

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class FlyWeightTest extends JFrame{

	private static final long serialVersionUID = 1L;

	JButton startDrawing;
	int windowWidth = 1750;
	int windowHeight = 1000;
	// A new rectangle is created only if a new color is needed
	Color[] shapeColor = {,, Color.yellow,,, Color.cyan, Color.magenta,, Color.gray};
	public static void main(String[] args){
		new FlyWeightTest();
	public FlyWeightTest(){
		// Create the frame, position it and handle closing it
		this.setTitle("Flyweight Test");
		JPanel contentPane = new JPanel();
		contentPane.setLayout(new BorderLayout());
		final JPanel  drawingPanel  = new JPanel();
		startDrawing = new JButton("Button 1");
		contentPane.add(drawingPanel,  BorderLayout.CENTER);
		contentPane.add(startDrawing, BorderLayout.SOUTH);
		startDrawing.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
	            Graphics g = drawingPanel.getGraphics();
	            long startTime = System.currentTimeMillis();
	            for(int i=0; i < 100000; ++i) {
	            	// Uses rectangles stored in the HashMap to
	            	// speed up the program
	            	MyRect rect = RectFactory.getRect(getRandColor());
	                rect.draw(g, getRandX(), getRandY(), 
	                		getRandX(), getRandY());
	            	MyRect rect = new MyRect(getRandColor(), getRandX(), getRandY(), getRandX(), getRandY());

	            	g.fillRect(getRandX(), getRandY(), getRandX(), getRandY());
	            long endTime = System.currentTimeMillis();

	            System.out.println("That took " + (endTime - startTime) + " milliseconds");
	// Picks random x & y coordinates
	private int getRandX(){ return (int)(Math.random()*windowWidth); }
	private int getRandY(){ return (int)(Math.random()*windowHeight); }
	// Picks a random Color from the 9 available
	private Color getRandColor(){ 
		Random randomGenerator = new Random();
		int randInt = randomGenerator.nextInt(9);
		return shapeColor[randInt]; 


import java.awt.*;
public class MyRect {
   private Color color =;
   private int x, y, x2, y2;
   public MyRect(Color color) {
	   this.color = color;
   public void draw(Graphics g, int upperX, int upperY, int lowerX, int lowerY) {
	      g.fillRect(upperX, upperY, lowerX, lowerY);
   /* Original forces creation of a rectangle every time
   public MyRect(Color color, int upperX, int upperY, int lowerX, int lowerY) {
      this.color = color;
      this.x = upperX;   
      this.y = upperY;
      this.x2 = lowerX; 
      this.y2 = lowerY;
   public void draw(Graphics g) {
      g.fillRect(x, y, x2, y2);


// This factory only creates a new rectangle if it 
// uses a color not previously used

// Intrinsic State: Color
// Extrinsic State: X & Y Values

import java.util.HashMap;
import java.awt.Color;
public class RectFactory {
	// The HashMap uses the color as the key for every 
	// rectangle it will make up to 8 total
	private static final HashMap<Color, MyRect> rectsByColor = new HashMap<Color, MyRect>();
	public static MyRect getRect(Color color) {
		MyRect rect = (MyRect)rectsByColor.get(color);
		// Checks if a rectangle with a specific
		// color has been made. If not it makes a
		// new one, otherwise it returns one made already
		if(rect == null) {
			rect = new MyRect(color);
			// Add new rectangle to HashMap
			rectsByColor.put(color, rect);
		return rect;

8 Responses to “Flyweight Design Pattern Tutorial”

  1. Anonymous says:

    Very nice tutorials. Thanks you!

  2. boris says:

    thank you so much for taking the time doing this

  3. Emily Chen says:

    Great job! You are my on-line teacher now.

  4. Ani says:

    You’re awesome! Thanks to you I may have a chance to pass my final exam!

Leave a Reply

Your email address will not be published.