Flyweight Definition: Avoid a large number of small categories of small categories (such as consumption), so that everyone share a category (yuan category).
Why is it used? The principle for the object language is that everything is an object, but if you really use, sometimes the number of objects may be very large, such as text processing software, if each text is used as an object, thousands of words, The number of items is thousands, undoubtedly consumes memory, then we still want to "seek to deposit", find the common point of these object groups, design a category, package can be shared, and some features depend on the application (context) is uncomfortable, which is also in Flyweight, two important concepts Internal status Intrinsic and external status Extrinsic.
To put it in a white point, it is first pinching a raw model, and then with the specific model of each characteristic, it is clear that different new objects are required here, so Flyweight mode often appears in the Factory mode. The internal state of Flyweight is used to be shared, and Flyweight Factory is responsible for maintaining a Flyweight Pool to store objects of internal status.
The Flyweight mode is a mode in which the program efficiency and performance will be greatly accelerated. There are many applications: For example, you have to read a series of strings from a database, and there are many repetitions in these strings, then we can store these strings in the Flyweight pool (Pool).
how to use?
Let's start from the Flyweight abstraction:
Public interface flyweight {public void Operation (EXTRINSICSTATE STATE);} // Working with this mode (self-design) public interface extrinsicState {}
The following is a concreteFlyWeight, which increases the memory space for the internal state. The ConcreteflyWeight must be shared. It must be internal (Intrinsic), that is, ConcreteflyWeight must be with its application environment. Not related to the occasion.
Public Class ConcreteflyWeight Implements Flyweight {Private IntrinsicsTate State; Public Void Operation (ExtrinsicsTate State) {// Specific Operation}}
Of course, not all Flyweight specific implementation subcategories need to be shared, so there is another non-shared Concreteflyweight:
Public Class UnsharedConcreteflyweight Implements Flyweight {Public Void Operation (ExtrinsicState State) {}}
Flyweight Factory is responsible for maintaining a FlyWeight pool (stores internal state), when the user requests a shared flyweight, this Factory first searches if there is already available in the pool. If there is, Factory just simply returns this object, otherwise, create a New objects, add to the pool, return to send this object pool
public class FlyweightFactory {// Flyweight pool private Hashtable flyweights = new Hashtable (); public Flyweight getFlyweight (Object key) {Flyweight flyweight = (Flyweight) flyweights.get (key); if (flyweight == null) {// create new Concreteflyweight flyweight = new contReteflyWeight (); flyweights.put (key, flyweight);}}}
Thus, the basic framework of the Flyweight pattern already in place, we look at how the call: FlyweightFactory factory = new FlyweightFactory (); Flyweight fly1 = factory.getFlyweight ( "Fred"); Flyweight fly2 = factory.getFlyweight ( "Wilma"); .. ....
From the call, it seems to be a pure Factory, but the mystery is in the internal design of Factory.
Flyweight mode Application in XML and other data sources have been mentioned above, when a large number of read strings from the data source, there is definitely, then we can use the Flyweight mode to improve efficiency, with a record CD as an example, in an XML In the file, multiple CDs are stored.
Each CD has three fields: 1. Date (YEAR) 2. Singer name and other information (Artist) 3. Recording track (title)
Among them, the name of the singer is likely to be repeated, that is, there may be different tracks of different tracks of the same singer. We will "singers' name" as a CONCRETEFLYWEIGHT that can be shared. The other two fields are as unsharedConcreteflyweight.
First look at the contents of the data source XML file:
XML Version = "1.0"?>
Although only 3 exceptions are only 3, the CD can be seen as a small amount of repetition, because the ingredients are only three fields, and there are repetitive (singers name).
The CD is similar to the above interface Flyweight:
public class CD {private String title; private int year; private Artist artist; public String getTitle () {return title;} public int getYear () {return year;} public Artist getArtist () {return artist;} public void setTitle ( String t) {title = t;} public void setYear (int y) {year = y;} public void setartist (artist a) {artist = a;}}
Put "Singer Name" as a shared Concreteflyweight:
Public class artist {// internal status private string name; // Note That artist is immutable. String getName () {return name;} artist (string n) {name = n;}}
Look Flyweight factory, designed to be shared by the above manufacturing ConcreteFlyweight: Artistpublic class ArtistFactory {Hashtable pool = new Hashtable (); Artist getArtist (String key) {Artist result; result = (Artist) pool.get (key) Generate new Artist if (Result == null) {result = new artist (key); pool.put (key, result);} return result;}}
When you have thousands of or even more CDs, the Flyweight mode saves more space, the more Flyweight, the more space saves.