Java Design Patterns .

Java Design Patterns Flyweight

Flyweight Overview

The reusable and variable parts of a class are broken into two classes to save resources.


TeaOrder.java - the Flyweight

public abstract class TeaOrder {  
    public abstract void serveTea(TeaOrderContext teaOrderContext);
}
To download source right-click here and "Save As...".


TeaFlavor.java - the Concrete Flyweight

public class TeaFlavor extends TeaOrder {  
    String teaFlavor; 
   
    TeaFlavor(String teaFlavor) {
        this.teaFlavor = teaFlavor;
    }
   
    public String getFlavor() {
        return this.teaFlavor;
    }
   
    public void serveTea(TeaOrderContext teaOrderContext) {
        System.out.println("Serving tea flavor " + 
                             teaFlavor + 
                           " to table number " + 
                             teaOrderContext.getTable());
    }
}
To download source right-click here and "Save As...".


TeaOrderContext.java - the Context

public class TeaOrderContext {  
   int tableNumber; 
   
   TeaOrderContext(int tableNumber) {
       this.tableNumber = tableNumber;
   }
   
   public int getTable() {
       return this.tableNumber;
   }
}
To download source right-click here and "Save As...".


TeaFlavorFactory.java - the Factory

public class TeaFlavorFactory {  
   TeaFlavor[] flavors = new TeaFlavor[10];
     //no more than 10 flavors can be made
   int teasMade = 0;
   
   public TeaFlavor getTeaFlavor(String flavorToGet) {
       if (teasMade > 0) {
           for (int i = 0; i < teasMade; i++) {
               if (flavorToGet.equals((flavors[i]).getFlavor())) {
                   return flavors[i];
               }
           }
       }
       flavors[teasMade] = new TeaFlavor(flavorToGet);
       return flavors[teasMade++];
   }
   
   public int getTotalTeaFlavorsMade() {return teasMade;}
}
To download source right-click here and "Save As...".


TestFlyweight.java - the Client, tests the Flyweight

class TestFlyweight {  
   static TeaFlavor[] flavors = 
     new TeaFlavor[100];
     //the flavors ordered
   static TeaOrderContext[] tables = 
     new TeaOrderContext[100];
     //the tables for the orders
   static int ordersMade = 0;    
   static TeaFlavorFactory teaFlavorFactory;
    
   static void takeOrders(String flavorIn, int table) {
       flavors[ordersMade] = 
         teaFlavorFactory.getTeaFlavor(flavorIn);
       tables[ordersMade++] = 
         new TeaOrderContext(table);
   }
    
   public static void main(String[] args) {
       teaFlavorFactory = new TeaFlavorFactory();
       
       takeOrders("chai", 2);    
       takeOrders("chai", 2);
       takeOrders("camomile", 1);
       takeOrders("camomile", 1);
       takeOrders("earl grey", 1);
       takeOrders("camomile", 897);
       takeOrders("chai", 97);
       takeOrders("chai", 97);
       takeOrders("camomile", 3);
       takeOrders("earl grey", 3);
       takeOrders("chai", 3);
       takeOrders("earl grey", 96);
       takeOrders("camomile", 552);
       takeOrders("chai", 121);
       takeOrders("earl grey", 121);
      
       for (int i = 0; i < ordersMade; i++) {
           flavors[i].serveTea(tables[i]);
       }  
       System.out.println(" ");       
       System.out.println("total teaFlavor objects made: " + 
                           teaFlavorFactory.getTotalTeaFlavorsMade());
   }
} 
To download source right-click here and "Save As...".


Test Results

Serving tea flavor chai to table number 2
Serving tea flavor chai to table number 2
Serving tea flavor camomile to table number 1
Serving tea flavor camomile to table number 1
Serving tea flavor earl grey to table number 1
Serving tea flavor camomile to table number 897
Serving tea flavor chai to table number 97
Serving tea flavor chai to table number 97
Serving tea flavor camomile to table number 3
Serving tea flavor earl grey to table number 3
Serving tea flavor chai to table number 3
Serving tea flavor earl grey to table number 96
Serving tea flavor camomile to table number 552
Serving tea flavor chai to table number 121
Serving tea flavor earl grey to table number 121
 
total teaFlavor objects made: 3

Notes

In this example a single class could have held both the tea flavor and table number.

However, we will have a limited number of instances of tea flavors, and so that is broken into a it's own flyweight class.

Table number is less limited, so it goes into the context.

The factory is responsible for only making one instance of each flyweight.

The client is responsible for keeping both halves matched up, and passing the context into the flyweight.

UML

UML for Flyweight

References


online
Portland Pattern Repository
Books
Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
Java Design Patterns - A Tutorial by James W. Cooper
Comments
Sign in to be the first to comment on Java Design Patterns Flyweight.