Flyweight Pattern
Author: Deron Eriksson
Description: This Java tutorial describes the flyweight pattern, a structural design pattern.
Tutorial created using: Windows Vista || JDK 1.6.0_11 || Eclipse JEE Ganymede SR1 (Eclipse 3.4.1)


Page: < 1 2

(Continued from page 1)

The FlyweightFactory class is our flyweight factory. It utilizes the singleton pattern so that we only have once instance of the factory, which we obtain via its static getInstance() method. The FlyweightFactory creates a hashmap pool of flyweights. If a request is made for a flyweight object and that object doesn't exist, it is created and placed in the flyweight pool. The flyweight pool of the FlyweightFactory stores all the instances of the different types of flyweights (ie, FlyweightAdder object, FlyweightMultiplier object, etc). Thus, only one instance of each type is created, and this occurs on-demand.

FlyweightFactory.java

package com.cakes;

import java.util.HashMap;
import java.util.Map;

public class FlyweightFactory {

	private static FlyweightFactory flyweightFactory;

	private Map<String, Flyweight> flyweightPool;

	private FlyweightFactory() {
		flyweightPool = new HashMap<String, Flyweight>();
	}

	public static FlyweightFactory getInstance() {
		if (flyweightFactory == null) {
			flyweightFactory = new FlyweightFactory();
		}
		return flyweightFactory;
	}

	public Flyweight getFlyweight(String key) {
		if (flyweightPool.containsKey(key)) {
			return flyweightPool.get(key);
		} else {
			Flyweight flyweight;
			if ("add".equals(key)) {
				flyweight = new FlyweightAdder();
			} else {
				flyweight = new FlyweightMultiplier();
			}
			flyweightPool.put(key, flyweight);
			return flyweight;
		}
	}

}

The FlyweightDemo class demonstrates our flyweight pattern. It obtains a FlyweightFactory object via FlyweightFactory.getInstance(). After this, in a loop, it obtains a FlyweightAdder from the FlyweightFactory and calls its doMath() operation with the current loop index as the two parameter values. Next, it does the same thing with a FlyweightMultiplier.

FlyweightDemo.java

package com.cakes;

public class FlyweightDemo {

	public static void main(String[] args) {

		FlyweightFactory flyweightFactory = FlyweightFactory.getInstance();

		for (int i = 0; i < 5; i++) {
			Flyweight flyweightAdder = flyweightFactory.getFlyweight("add");
			flyweightAdder.doMath(i, i);

			Flyweight flyweightMultiplier = flyweightFactory.getFlyweight("multiply");
			flyweightMultiplier.doMath(i, i);
		}
	}
}

The console output of executing FlyweightDemo is shown here.

Console Output

adding 0 and 0: 0
multiplying 0 and 0: 0
adding 1 and 1: 2
multiplying 1 and 1: 1
adding 2 and 2: 4
multiplying 2 and 2: 4
adding 3 and 3: 6
multiplying 3 and 3: 9
adding 4 and 4: 8
multiplying 4 and 4: 16

When executing FlyweightDemo, we see that it takes 3 seconds to create the FlyweightAdder object and the same thing happens for the FlyweightMultiplier object. After this, no more delays occur since we keep reusing the flyweight objects rather than instantiating new objects. This highlights how the flyweight pattern can be used to minimize resource requirements by avoiding unnecessary object instantiations for similar objects.

Page: < 1 2


Related Tutorials: