Detailed explanation of sharing element mode of design mode

Keywords: Java Design Pattern

Detailed explanation of sharing element mode of design mode

1, What is the meta model

Flyweight Pattern, also known as lightweight pattern, is an implementation of object pool. Similar to thread pool, thread pool can avoid performance consumption by constantly creating and destroying objects. The number of objects is reduced to improve the way of applying the required object structure. Its purpose is to share fine-grained objects and centralize multiple accesses to the same object without creating an independent object for each visitor, so as to reduce memory consumption. It belongs to structural mode. Shared meta mode divides the state of an object into internal state and external state. The internal state is unchanged and the external state is changed. Then, by sharing the unchanged part, the number of objects and memory are reduced. Its essence is to cache shared objects and reduce memory consumption.

2, Role composition of meta mode

  • Abstract Flyweight: it abstracts the base class or interface of the meta object, and defines the interface or implementation of the external state and internal state of the object;
  • Concrete Flyweight: implements the business defined by the abstract role. The internal state processing of the role should be independent of the environment. No operation can change the internal state and modify the external state at the same time;
  • Flyweight Factory: responsible for managing the shared object pool and creating shared objects

3, Application scenario of meta mode

When the same set of information is needed in multiple places in the system, these information can be encapsulated into an object, and then the object can be cached. In this way, an object can be provided to multiple places that need to be used, so as to avoid multiple creation of a large number of the same object and consume a large amount of memory space. The meta sharing mode is actually an improved mechanism of the factory mode. The meta sharing mode also requires the creation of one or a group of objects, and the objects are generated through the factory method, but the caching function is added to the factory method in the meta sharing mode. The application scenarios are summarized as follows:
     1. It is often applied to the development of the underlying system in order to solve the performance problems of the system
     2. The system has a large number of similar objects and needs a buffer pool
Such as the housing supply sharing of intermediaries in life and the national social security network

4, Meta mode example

Let's first create an abstract meta role IFlyweight:

 * Abstract meta role
public interface IFlyweight {

    void operation(String extrinsicState);


Create concrete flyweight:

 * Specific meta role
public class ConcreteFlyweight implements IFlyweight {
    private String intrinsicState;

    public ConcreteFlyweight(String intrinsicState) {
        this.intrinsicState = intrinsicState;

    public void operation(String extrinsicState) {
        System.out.println("Object address: " + System.identityHashCode(this));
        System.out.println("IntrinsicState: " + this.intrinsicState);
        System.out.println("ExtrinsicState: " + extrinsicState);

Create a FlyweightFactory:

 * Xiangyuan factory
public class FlyweightFactory {
    private static Map<String, IFlyweight> pool = new HashMap<String, IFlyweight>();

     * Because the internal state is invariant, it is used as the key of cache
    public static IFlyweight getFlyweight(String intrinsicState) {
        if (!pool.containsKey(intrinsicState)) {
            IFlyweight flyweight = new ConcreteFlyweight(intrinsicState);
            pool.put(intrinsicState, flyweight);
        return pool.get(intrinsicState);

Write test class:

public class Test {
    public static void main(String[] args) {
        IFlyweight flyweight1 = FlyweightFactory.getFlyweight("aa");
        IFlyweight flyweight2 = FlyweightFactory.getFlyweight("bb");

Code test, console output:

5, Advantages and disadvantages of sharing mode

  • advantage
    • Reduce the creation of objects, reduce the number of objects in memory, reduce the memory of the system and improve efficiency
    • Reduce the use of resources other than memory
  • shortcoming
    • Pay attention to internal and external states and thread safety issues
    • It complicates the logic of the system and program

Posted by Sphynx on Fri, 19 Nov 2021 11:29:00 -0800