Flyweight Design Pattern

May 16, 2012   

A problem I had while implementing the “flyweight” design pattern for a lab in my design patterns class. The flyweight design pattern basically has a factory to hold different attributes for example, the attributes of a font which are: 

– Font: String
– Size: Integer
– Color: String
– Italic: boolean
– Bold: Boolean
– Row: Integer
– Column: Integer

To implement the design pattern we must add different fonts to the factory, only if the font is not already in the factory.

The main class was not adding fonts to the factory correctly. It is supposed to add fonts that are not already in the factory, in this case it is not and the factory size remains the same when testing.
The test code:

FontFactory myFactory = new FontFactory();

Font a = new Font(“Arial”, 12, “Blue”, true, true);
MyCharacter characterA = new MyCharacter(myFactory.createFont(a));
System.out.println(“Factory size: “ + myFactory.getTotalFonts());

Font b = new Font(“Times new Roman”, 12, “Black”, true, true);
MyCharacter characterB = new MyCharacter(myFactory.createFont(b));
System.out.println(“Factory size: “ + myFactory.getTotalFonts());

Factory size kept returning the same value even though different fonts are being added.

Flyweight design pattern is implemented incorrectly, therefore the concept will not be clear and the lab will not be complete.

There was an error with the createFont method which creates the font by comparing the font being added through an arraylist of fonts that are already in the factory and then checking if the font is currently in the factory.

    Public Font createFont(Font a){
      For (int i = 0; I < fonts.size(); i++){ //loops through the factory list of fonts
        If (a.isEqual(a,fonts.get(i)) == true) { //if the font being added is there then it will not be //added
          Check = true;
        Else { //the problem with the method was that it was missing the else statement
          Check = false; //if the font is not there check is false

      If (check == false) { //if the font is not in the factory then it will be added
      Return a;

The else statement which determines that the font being added is not in the factory was not there so there was no way of adding the font to the factory.