Good programmers succinctly java design patterns - Flyweight

I, on the Flyweight

  Flyweight embodiment somewhat similar to the single mode, are only used to generate a shared object. Flyweight main purpose is to allow multiple objects sharing, reduce unnecessary memory consumption, and more access to the same object together, without having to create a separate object for each visitor in order to reduce memory consumption.

Second, the structure of FIG Flyweight

Because Flyweight structure is complicated and the plant model used in conjunction with, a Flyweight contains in its factory class structure in FIG.

It includes the following several roles in the structure of FIG Flyweight:

Of flyweight (abstract Flyweight class): usually an interface or abstract class declares concrete metaclass common method of sharing abstract Flyweight class, these methods may provide internal data (internal state) Flyweight object to the outside world, while external data may also be provided (external states) by these methods.

ConcreteFlyweight (Flyweight particular class): Flyweight it implements the abstract class whose instances referred Flyweight objects; in particular enjoy metaclass provides storage space for the internal state. We can typically bind specifically to design single-mode embodiment Flyweight classes, provided only enjoy a particular object for each element Flyweight class.

UnsharedConcreteFlyweight (particularly non-shared class Flyweight): Not all subclasses of the abstract class Flyweight need to be shared, can not be shared or non-shared subclass may be designed specifically Flyweight class; when a non-shared specific needs Flyweight It can be created directly by instantiating an object class.

FlyweightFactory (Flyweight factory class): Flyweight factory class for creating and managing objects Flyweight, Flyweight abstract class for that program, various types of specific objects stored in a shared element Flyweight pool, pool general Flyweight It is designed to store a "key on" set (which may be a collection of other types) may be combined factory design pattern; when a user requests a specific shared element object, Flyweight a plant cell has been stored in the Flyweight create Create a new instance or instances (if not present), and returns the newly created instance stored in Flyweight pool.

Third, to achieve Flyweight pattern

Flyweight incorporated in the factory class Flyweight, Flyweight role is to provide a factory class for storing objects Flyweight Flyweight pool, when the user needs an object, acquired from the first Flyweight pool if the pool Flyweight does not exist, create a new object is returned to the user Flyweight and Flyweight pool to save the new object. Next, realize Flyweight a landing.
IV Summary

This can be seen from the above code and operating results, landing with a lander is "shared" with the same Lander object. Flyweight objects in the pool are only two objects.

  Flyweight advantage, that independent external state, so that the objects may be multiplexed (shared object can adapt to different external environment) in different environments. Flyweight and may share the same or similar fine-grained objects, thereby reducing the memory consumption, while reducing object creation and garbage collection overhead.

Flyweight shortcomings, external state saved by the client, the shared object read external state spending may be relatively large. Flyweight requires separate internal state with an external state, which makes complicated logic, but also the state where the maintenance costs.

Guess you like

Origin www.cnblogs.com/gcghcxy/p/10980043.html