※ design pattern ※ → ☆ ☆ ============ Flyweight structural model Mode (XI)

Overview mode

Flyweight Flyweight pattern, also known as the English called "Flyweight Pattern", also translated featherweight mode or flyweight mode.

Flyweight pattern is defined as: the use of a shared class to avoid "small class" has cost a lot of the same content. This overhead is the most common, is to increase the visual impact of memory loss. Flyweight pattern to share effective way to support a large number of fine-grained objects, reduce the overhead it brings.

The names and definitions all reflect on this core concept, then how to achieve shared it? Is different, but it exists between certain things in common, if only to share the exact same thing, then the Flyweight pattern can be said is not feasible; therefore we should try to share things in common, while preserving its personality . To do this, Flyweight mode distinguishes between a state and an outer Yun intrinsic state. Intrinsic state is common, Yun outside the state is a personality.

Intrinsic state is stored inside the Flyweight, with changes in the environment will not vary, can be shared; Yun outside is not shared state, which changes with changes in the environment, so that the outer Yun state by the client to maintain (because of changes in the environment caused by the client). In each specific environment, the client status to the Flyweight outer Yun, to create different objects out.

Flyweight pattern composed of the following roles

  • 1) Abstract Flyweight role: specifies the method must be implemented for specific Flyweight role, Yun state is out in the form of parameters passed by this method. In Java can be filled by abstract classes, interfaces.
  • 2) Specific Flyweight role: implementation of the provisions of abstract roles. If the intrinsic state, that it is responsible for providing storage for intrinsic state.
  • 3) Flyweight factory Role: responsible for creating and managing Flyweight role. To achieve the shared objective, to achieve this role is the key!

 

Mode structure

 

Mode discussion

He realized in the design of object-oriented system, create objects is the most common operation. There is there is a problem: If an application uses too many objects, it will cause a lot of memory overhead. Especially for a large number of lightweight (fine-grained) objects, such as document editor of the design process, we create an object as if there is no letter, the system might be because a large number of objects and wasted storage overhead.

Flyweight pattern advantage is that it can greatly reduce the number of objects in memory; and to do this step brings its disadvantages: it makes the system complicated logic, and to some extent the impact on the outer Yun state of the system speed.

In summary, under normal circumstances, it is difficult to use the Flyweight pattern. Unless there is a certain order of magnitude and above, and subject specific commonalities. When the content is modified or common variations, Flyweight mode need not completely traverse each corresponding modification. In addition, the returned object management is also a problem, recommend the use of smart pointers, otherwise it can control external release when the current object and generate confusion.

Flyweight pattern of use conditions:

  • 1) The system has a large number of objects, so that they reduce the efficiency of the system.
  • 2) the status of these objects can be isolated desired inner and outer portions.

Division and the corresponding relationship between the two foreign Yun intrinsic state and the state is also very worthy of attention. Only inside and outside the division in order to make appropriate intrinsic state to play its proper role; if divided mistakes, objects in the system under the worst case is not a decrease! Maintenance and find the corresponding relationship between the two is to spend a certain amount of space (compared to this of course does not use shared objects much smaller) and time, it can be said Flyweight pattern is the use of time in exchange for space. Corresponding algorithm can be used to speed up the search.

 

Pattern implementation

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
after Xiao Ting notes will be as detailed as possible explain some knowledge, I hope you continue to focus on my blog.
This section notes to the end here.

Xiao Ting will have time to put their learning experience, feel better knowledge to write and share with everyone.
Programming development of the road is very long, very much like to be able to share together, learn together, and common progress.
If you have any articles oversight place, please correct me. I hope you and I can be more comments to discuss issues related to programming.
Finally, thank you for your support ~~~ been

       Complete C ++ code example (the code under test can be run in VS2017)
codes and related information Download:
              https://gitee.com/arvinxt/DesignPattern

Published 170 original articles · won praise 207 · Views 4.59 million +

Guess you like

Origin blog.csdn.net/xiaoting451292510/article/details/103721643