# Flyweight

In 
Published 2022-12-03

This tutorial explains to you the design pattern named Flyweight (which is a Structural Pattern).

# Flyweight Pattern - theory

The main idea behind Flyweight Pattern is that sometimes we need to keep in memory some objects to be faster and sometimes to use less memory.

Supposing, we have to draw many cars (of several types) on a huge map on different positions. We have 2 options:

  • for each car we add 2 attribute (x, y coordinates) and the "draw()" function will draw the car at the (x, y) position on the map

  • the x, y coordinates will be pass by the client, and we keep in memory only the specific information (we can share) for each car

The option 2) save memory and for some situation could be a good choice. This case is threaten by the Flyweight design pattern. This is a structural design pattern and in this article I will give you an example in Java.

Take a look at the following UML diagram representing the Flyweight design pattern (for my example):

In Flyweight Design Pattern, the main idea is to save RAM memory during the execution of a Java project in some special cases.

# Flyweight Pattern - example

Here is that example using the Flyweight Design Pattern in Java:

package flyweight.java.pattern.example;
 
public interface Car {
 
  // This method build a car.
  public void build();
}
package flyweight.java.pattern.example;
 
public class Chevrolet implements Car{
      
    public void build(){
        System.out.println("A Chevrolet car has been built.");
    }
     
}
package flyweight.java.pattern.example;
 
public class BMW implements Car{
      
    public void build(){
        System.out.println("A BMW car has been built.");
    }
     
}
package flyweight.java.pattern.example;
 
public class Renault implements Car{
      
    public void build(){
        System.out.println("A Renault car has been built.");
    }
}
package flyweight.java.pattern.example;
 
import java.util.HashMap;
 
public class CarFactory {
 
    private HashMap<String, Car> carsList = new HashMap<String, Car>();
     
    public Car getCar(String carType) {
         
        Car car = carsList.get(carType);
         
        if (car == null) {
            if (carType == "Chevrolet") {
                car = new Chevrolet();
                carsList.put("Chevrolet", car);
                System.out.println("A Chevrolet car was CREATED and saved in memory");
                 
            } else if (carType == "BMW") {
                car = new BMW();
                carsList.put("BMW", car);
                System.out.println("A BMW car was CREATED and saved in memory");
                 
            } else if (carType == "Renault") {
                car = new Renault();
                carsList.put("Renault", car);
                System.out.println("A Renault car was CREATED and saved in memory");
            }
 
        }
 
        return car;
    }   
}
package flyweight.java.pattern.example;
 
public class FlyweightJavaPatternExampleMain {
     
    static CarFactory carFactory = new CarFactory();
     
    public static void main(String[] args) {
     
       for(int x = 1; x < 5; x = x + 1) {
          Car car1 = carFactory.getCar("Chevrolet");
          System.out.println("A Chevrolet car was drawn in position (x, y)=("+x+",10)");
       }
        
       for(int x = 1; x < 5; x = x + 1) {
          Car car1 = carFactory.getCar("BMW");
          System.out.println("A BMW car was drawn in position (x, y)=("+x+",10)");
       }
    }
}

When you run this example you will receive the following result: