Java Design Pattern Series (11) Flyweight Pattern

Java Design Pattern Series (11) Flyweight Pattern

Flyweight is a structural pattern for objects. The Flyweight pattern efficiently supports a large number of fine-grained objects in a shared manner.

1. The structure of the flyweight pattern

The Flyweight pattern uses a share to avoid the overhead of having a lot of the same content objects. The most common and intuitive example of this overhead is the loss of memory. The key to the sharing of flyweight objects is to distinguish between internal state and external state.

An internal state is stored inside the flyweight object and is invariant to changes in the environment. Therefore, a flyweight can have intrinsic state and can be shared.

An external state changes with the environment and cannot be shared. The extrinsic state of the flyweight object must be saved by the client, and after the flyweight object is created, it is passed into the flyweight object when it needs to be used. The extrinsic state cannot affect the internal state of the Flyweight object, they are independent of each other.

Figure 11-1 The structure of the flyweight pattern

  • Flyweight: Flyweight interface, through which flyweight can accept and act on external state. The external state is passed in through this interface, and these external data may be used in the method processing of the flyweight object.

  • ConcreteFlyweight: The specific flyweight implementation object must be sharable and needs to encapsulate the internal state of flyweight.

  • FlyweightFactory: Flyweight Factory, which is mainly used to create and manage shared Flyweight objects and provide external access to shared Flyweight objects.

source code

(1) Flyweight

/***
 * 享元接口,通过这个接口享元可以接受并作用于外部状态
 */
public interface Flyweight {
    /** 示例操作,传入外部状态 */
    public void operation(String extrinsicState);
}

public class ConcreteFlyweight implements Flyweight{
    /** 示例,描述内部状态 */
    private String intrinsicState;

    /** 构造方法,传入享元对象的内部状态的数据 */
    public ConcreteFlyweight(String state){
        this.intrinsicState = state;
    }

    public void operation(String extrinsicState) {
        //具体的功能处理,可能会用到享元内部、外部的状态
    }
}

(2) FlyweightFactory

/**
 * 享元工厂,说白了就是一个缓存
 */
public class FlyweightFactory {
    /** 缓存多个flyweight对象,这里只是示意一下 */
    private Map<String, Flyweight> fsMap = new HashMap<String,Flyweight>();

    /** 获取key对应的享元对象 */
    public Flyweight getFlyweight(String key) {
        //这个方法里面基本的实现步骤如下:
        //1:先从缓存里面查找,是否存在key对应的Flyweight对象
        Flyweight f = fsMap.get(key);

        //2:如果存在,就返回相对应的Flyweight对象
        if(f == null){
            //3:如果不存在
            //3.1:创建一个新的Flyweight对象
            f = new ConcreteFlyweight(key);
            //3.2:把这个新的Flyweight对象添加到缓存里面
            fsMap.put(key, f);
            //3.3:然后返回这个新的Flyweight对象
        }
        return f;
    }
}

2. Summary

(1) Advantages and disadvantages of flyweight mode

The advantage of the Flyweight pattern is that it drastically reduces the number of objects in memory. However, it does so at a high price:

  • The Flyweight pattern makes the system more complex. To make objects sharable, some state needs to be externalized, which complicates the logic of the program.

  • The Flyweight pattern externalizes the state of the Flyweight object, and reading the external state makes the runtime slightly longer.


Record a little bit every day. Content may not be important, but habits are!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325941697&siteId=291194637