Flyweight mode of design pattern 22

background

In our coding development process, we may encounter the problem of needing to create a large number of instances of the same or similar objects. If we create these objects, it will consume system resources. But we can extract these common parts, which will save a lot of system resources. So the Flyweight model came out.

What is Flyweight Model

Use sharing to support large numbers of fine-grained objects efficiently. (Use shared objects can effectively support a large number of fine-grained objects.)

The core idea of ​​the Flyweight model is sharing, and only one copy of the same object is kept. But in order to achieve sharing, different states will be extracted to the outside, thereby increasing the complexity of the program.

The Flyweight model mainly consists of the following elements:

  • Abstract Flyweight: It is the base class of all specific Flyweight classes. It is a public interface that needs to be implemented by the specific Flyweight specification. The external state of non-flyweights is passed in through methods in the form of parameters.

  • Concrete Flyweight role: implements the interface specified in the abstract Flyweight role.

  • Unsharable Flyweight (Unsharable Flyweight) role: It is an external state that cannot be shared. It is injected into the relevant methods of the specific Flyweight in the form of parameters.

  • Flyweight Factory (Flyweight Factory) role: responsible for creating and managing Flyweight Factory roles. When a client object requests a flyweight object, the flyweight factory checks whether there is a flyweight object that meets the requirements in the system, and if it exists, it will be provided to the customer; if it does not exist, a new flyweight object will be created.

The relationship diagram is as follows:

Flyweight model

Code

UnsharedConcreteFlyweight

Flyweight

ConcreteFlyweight

FlyweightFactory

Code test

Test Results:

具体享元a被创建!
具体享元a已经存在,被成功获取!
具体享元a已经存在,被成功获取!
具体享元b被创建!
具体享元b已经存在,被成功获取!
具体享元a被调用,非享元信息是:第1次调用a
具体享元a被调用,非享元信息是:第2次调用a
具体享元a被调用,非享元信息是:第3次调用a
具体享元b被调用,非享元信息是:第1次调用b
具体享元b被调用,非享元信息是:第2次调用b

In some ways, is it a bit like a singleton pattern?

Thoughts on Flyweight Model

If there are a lot of similar or identical objects in the project, using the flyweight mode can save a lot of memory overhead.

Everyone can think about whether there are specific scenarios where Flyweight mode can be used, such as the buffer pool scenario.

There is another problem. The core of the Flyweight model lies in data sharing, which may lead to thread insecurity. When using Flyweight mode, thread safety needs to be considered.

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

  1. 回复"java" 获取java电子书;

  2. 回复"python"获取python电子书;

  3. 回复"算法"获取算法电子书;

  4. 回复"大数据"获取大数据电子书;

  5. 回复"spring"获取SpringBoot的学习视频。

  6. 回复"面试"获取一线大厂面试资料

  7. 回复"进阶之路"获取Java进阶之路的思维导图

  8. 回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

  9. 回复"总结"获取Java后端面试经验总结PDF版

  10. 回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

  11. 回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

Guess you like

Origin blog.csdn.net/wujialv/article/details/110132642