Flyweight Design Pattern in Java

The Flyweight Design Pattern is a structural design pattern in Java that aims to minimize the memory usage of an application by sharing as much data as possible between multiple objects. The pattern achieves this by creating shared objects, called flyweights, that can be used by multiple objects instead of creating new objects each time.

The Flyweight Design Pattern in Java has the following properties:

  1. Memory optimization: The Flyweight Design Pattern aims to minimize the memory usage of an application by sharing as much data as possible between multiple objects.
  2. Shared objects: The pattern achieves this by creating shared objects, called flyweights, that can be used by multiple objects instead of creating new objects each time.
  3. Intrinsic and extrinsic state: The Flyweight Design Pattern distinguishes between intrinsic and extrinsic state of an object. Intrinsic state is the data that can be shared between multiple objects, while extrinsic state is the data that is specific to each object.
  4. Immutable flyweights: The Flyweight Design Pattern typically creates immutable flyweight objects that cannot be modified once they are created. This ensures that the shared data remains consistent across all objects.
  5. Flyweight factory: The Flyweight Design Pattern uses a flyweight factory to manage the creation and sharing of flyweight objects. The factory ensures that each flyweight object is created only once and is shared by multiple objects.
  6. Transparent to clients: The Flyweight Design Pattern is transparent to clients. The clients interact with the flyweight objects as if they were regular objects, without knowing that the data is shared between multiple objects.

In Java, the Flyweight Design Pattern can be implemented in the following way:

  1. Create an interface or abstract class that represents the flyweight object.
    public interface Flyweight {
        void operation();
    }
  2. Create a concrete flyweight class that implements the interface or abstract class.
    public class ConcreteFlyweight implements Flyweight {
        private String intrinsicState;
    
        public ConcreteFlyweight(String intrinsicState) {
            this.intrinsicState = intrinsicState;
        }
    
        @Override
        public void operation() {
            System.out.println("ConcreteFlyweight: " + intrinsicState);
        }
    }
  3. Create a flyweight factory that manages the creation and sharing of flyweight objects.
    public class FlyweightFactory {
        private Map<String, Flyweight> flyweights = new HashMap<>();
    
        public Flyweight getFlyweight(String intrinsicState) {
            if (!flyweights.containsKey(intrinsicState)) {
                flyweights.put(intrinsicState, new ConcreteFlyweight(intrinsicState));
            }
            return flyweights.get(intrinsicState);
        }
    }
  4. Use the flyweight factory to create and share flyweight objects.
    public class Main {
        public static void main(String[] args) {
            FlyweightFactory flyweightFactory = new FlyweightFactory();
    
            Flyweight flyweight1 = flyweightFactory.getFlyweight("state1");
            flyweight1.operation();
    
            Flyweight flyweight2 = flyweightFactory.getFlyweight("state2");
            flyweight2.operation();
    
            Flyweight flyweight3 = flyweightFactory.getFlyweight("state1");
            flyweight3.operation();
    
            System.out.println(flyweight1 == flyweight3); // true
        }
    }

In this example, the ConcreteFlyweight class represents the shared object that can be used by multiple objects. The FlyweightFactory class manages the creation and sharing of flyweight objects. The Main class uses the flyweight factory to create and share flyweight objects.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial