First, about the yuan model

The share mode is similar to the singleton mode in that only one object is generated for shared use. The main purpose of the share mode is to let multiple objects realize sharing, reduce unnecessary memory consumption, will be more than the same object access together, do not have to create a separate object for each visitor, in order to reduce the consumption of memory.

Ii. Enjoy yuan mode structure diagram

Because of the complex structure of the share mode, it is generally used together with the factory mode, which contains a share factory class in its structure diagram.



There are several roles in the schema structure diagram:

Flyweight (abstract meta-class) : Usually an interface or abstract class in which the abstract meta-class declares methods common to the concrete meta-class. These methods can provide external data (internal state) of a meta-object and also set external data (external state) through these methods.

ConcreteFlyweight: Implements an abstract meta-class, called a meta-object; Storage space for internal state is provided in the concrete meta-class. Typically, we can design concrete meta-classes in conjunction with the singleton pattern, providing a unique meta-object for each concrete meta-class.

UnsharedConcreteFlyweight (non-shared specific flyweight category) : not all of the abstract metaclass subclasses needs to be Shared, cannot be Shared subclasses can be designed as a Shared specific flyweight classes; An object that does not share a concrete meta-class can be created by instantiation.

FlyweightFactory: The free meta-factory class is used to create and manage the free meta-objects. It is designed for abstract free meta-classes, and stores the concrete free meta-objects of various types in a free meta-pool. The free meta-pool is generally designed as a collection of “key value pairs” (it can also be other types of collections), which can be combined with the factory mode. When a user requests a concrete share object, the share factory provides an instance that is already created and stored in the share pool or creates a new instance (if one does not exist), returns the newly created instance and stores it in the share pool.

Three, enjoy the realization of yuan model

In the flyweight pattern introduces the flyweight factory class, the role of the flyweight factory class is to provide a used to store the flyweight objects the flyweight pool, when users need to object, first from the flyweight pool, if the flyweight pool does not exist, then create a new flyweight objects returned to the user, and save the new object in the flyweight pool. Next, implement a log-in share pattern. Four,

As you can see from the above code and results, the same logon is “enjoying” the same logon object. There are only two objects in the share object pool.

The advantage of the share mode is that the external state is relatively independent, so that objects can be reused in different environments (shared objects can adapt to different external environments). In addition, the metadata sharing mode can share the same or similar fine-grained objects, thus reducing the memory consumption and object creation and garbage collection overhead.

The share mode has the disadvantage that the external status is saved by the client and the shared object may have a large overhead in reading the external status. The share pattern requires the separation of internal state from external state, which complicates the logic of the program and increases the cost of state maintenance.