"Design Mode Design Pattern" Reading Notes 12

xiaoxiao2021-03-06  17

"Design Mode Design Pattern" Reading Notes 12

Flyweight mode

structure

purpose

Reduce storage space consumption by sharing objects

discuss

The status of an object can be divided into an internal state and an external state (ExtrinSic State). The internal state is independent of context, and the external state is different due to Context. By separating the external state, only the object that only holds the internal state is a Flyweight, and Flyweight can share. The external state can be used as a member function (operation) of the parameter incoming object. In this way, the requirements of storage space can be reduced on a large scale.

The advantage of FlyWeight is to reduce the consumption of storage space, the disadvantage is to increase the consumption of Run-Time (due to the need to transfer, lookup, or calculate external state).

If the number of separate Extrinsic State is not distinguished before the separation, it will not help improve the storage space consumption of Flyweight. Ideally, ExtrinSic State can be calculated by a smaller object structure (Computed).

UnsharedConcreteflyweight can be created via FlyweightFactory and is created directly by Client. However, considering that there may be a need for unsharedconcreteflyweight to change the unsharedconcreteflyweight, which is a good choice to create a good future through FlyWeightFactory.

Contact with other modes

The FlyWeight mode can be combined with the Composite mode to generate a Directed-Acyclic Hierarchical structure. As the Flyweight's LEAF does not save pointers to the Parent, the Parent pointer is passed to the LEAF as an external state.

STATE and STRATEGY objects are generally best implemented as Flyweights.

Reference

"Design Pattern" Gang-Of-4 1997

转载请注明原文地址:https://www.9cbs.com/read-44067.html

New Post(0)