This is the 17th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

An overview of the

Flyweight Pattern supports object reuse through sharing and reduces resource waste. Because the shared element pattern requires that the shared object must be fine grained object, it is also called lightweight pattern, which is an object structure pattern.

An object in the share mode has two states: internal state and external state. For example, Zhang SAN runs a kettle factory. One machine for producing one kettle would be too luxurious, so the machines used to make the kettle would be shared. The production machine is the internal state, and the kettle produced is the external state.

  • Internal state: State stored inside the share object that does not change with the environment and can be shared.

  • External state: A tag on which an object can depend, which changes with the environment and is not shareable.

structure

  • FlyweightFactory (Privilege factory class) : Used to create and manage privilege objects, it programs against abstract privilege meta-classes and stores various concrete privilege objects in a pool of privilege objects. When a user requests a specific share object, the share factory checks whether there is a share object that meets the requirements in the system. If there is one, it provides it to the client. If not, it creates a new share object.

  • Flyweight (Abstract meta-class) : Usually an interface or abstract class that declares the public methods of the concrete meta-class. These methods provide the internal state of the share object to the outside world and set the external state.

  • ConcreteFlyweight: This implements a method declared by an abstract meta-class, called a meta-object, and provides storage for internal state.

  • UnsharedConcreteFlyweight (non-shared specific flyweight category) : not all abstract metaclass subclasses needs to be Shared, don’t need to be Shared the external state of concrete metaclass, can be designed as a Shared it in the form of parameters into specific flyweight relevant methods, can be instantiated directly.

advantages

  1. By sharing objects, the number of objects in memory is reduced, the memory occupation is reduced, and the system performance is improved.
  2. The external state of the share mode is relatively independent and does not affect its internal state, allowing share objects to be shared in different environments.

disadvantages

  1. The need to separate the internal state from the external state adds complexity to the system.
  2. In order for objects to be shareable, part of the state needs to be externalized, and reading the external state makes the run time longer.

Application scenarios

  1. A large number of similar objects exist in the system.
  2. Fine-grained objects have close external state, and the internal state is independent of the environment.
  3. Scenarios where buffer pools are required.

Applications in the JDK

The java.lang.String#intern() and java.lang.

public final class Integer extends Number implements Comparable<Integer> {

    private static class IntegerCache {
        public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return newInteger(i); }}}Copy the code