Flyweight definition: Avoid the overhead of a large number of small classes with the same content (such as memory consumption), so that you can share a class (meta Class).
Why use shared mode/enjoy meta mode
The principle of object-oriented language is that everything is an object, but if the real use, sometimes the number of objects may appear very large, such as word processing software, if each text as an object, thousands of words, the number of objects is thousands of, no doubt the memory consumption, then we still have to "seek common ground", Find common ground among these object groups, design a meta class, encapsulate the classes that can be shared, and, in addition, some features that depend on the application (context), are not shareable, and flyweight the internal state intrinsic and the external state extrinsic of the two important concepts.
The white point is to pinch a first original model, and then with different occasions and circumstances, and then produce the specific characteristics of the model, it is obvious that here need to produce different new objects, so the flyweight mode often appears Factory mode. The internal state of the Flyweight is shared, and Flyweight factory is responsible for maintaining a Flyweight pool (pattern pool) to hold objects in the internal state.
The flyweight mode is a model that improves program efficiency and performance, and can greatly speed up the program's running speed. There are many applications: for example, you want to read a series of strings from a database, many of which are duplicates, so we can store those strings in the flyweight pool.
How to use shared mode/enjoy meta mode
Let's start with the flyweight abstract interface:
Copy Code code as follows:
public interface flyweight{
public void operation (Extrinsicstate state);
}
Abstract data type for this pattern (self-designed)
Public interface Extrinsicstate {}
The following is the concrete implementation of the interface (Concreteflyweight) and increases the memory space for the internal state, concreteflyweight must be shareable, and any state it holds must be internal (intrinsic), that is to say, Concreteflyweight must have nothing to do with its application environment.
Copy Code code as follows:
public class Concreteflyweight implements Flyweight {
Private intrinsicstate State;
public void operation (Extrinsicstate state) {
Specific operations
}
}
Of course, not all flyweight need to be shared specifically to implement subclasses, so there is another concreteflyweight that is not shared:
Copy Code code as follows:
public class Unsharedconcreteflyweight implements Flyweight {
public void operation (Extrinsicstate state) {}
}
Flyweight Factory is responsible for maintaining a Flyweight pool (storing internal state), and when the client requests a shared Flyweight, the factory first searches the pool for any applicable, if any, factory simply returns the object to be sent , otherwise, create a new object, add it to the pool, and then return the object pool.
Copy Code code as follows:
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 a new Concreteflyweight
Flyweight = new Concreteflyweight ();
Flyweights.put (key, flyweight);
}
return flyweight;
}
}
Now that the basic framework for the flyweight model is in place, let's look at how to invoke:
Copy Code code as follows:
Flyweightfactory factory = new Flyweightfactory ();
Flyweight fly1 = factory.getflyweight ("Fred");
Flyweight fly2 = factory.getflyweight ("Wilma");
......
From the call, it seems to be a pure factory use, but the secret lies in the internal design of factory.
Application of Flyweight mode in XML and other data sources
as we mentioned above, when a large number of strings are read from the data source, there must be duplicates, then we can use the flyweight mode to improve efficiency, taking the CD as an example, in an XML file, storing more than one CD of data.
Each CD has three fields:
1. Date of the film (year)
2. Information (artist) of singers ' names
3. Record track (title)
Among them, the singer's name may be repeated, that is to say, there may be several different tracks of the same singer's CD. We use "singer's name" as a shareable concreteflyweight. Two other fields as unsharedconcreteflyweight.
First look at the contents of the data source XML file:
Copy Code code as follows:
<?xml version= "1.0"?>
<collection>
<cd>
<title>another Green world</title>
<year>1978</year>
<artist>eno, brian</artist>
</cd>
<cd>
<title>greatest hits</title>
<year>1950</year>
<artist>holiday, billie</artist>
</cd>
<cd>
<title>taking Tiger Mountain (by strategy) </title>
<year>1977</year>
<artist>eno, brian</artist>
</cd>
.......
</collection>
Although there are only 3 examples of CDs listed above, CDs can be viewed as a large number of repetitive classes, because there are only three fields in the composition, and there are duplicates (singers ' names).
CD is similar to the above interface Flyweight:
Copy Code code as follows:
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;}
}
To use "singer name" as a shareable concreteflyweight:
Copy Code code as follows:
public class Artist {
Internal state
private String name;
Note This Artist is immutable.
String GetName () {return name;}
Artist (String N) {
name = N;
}
}
And then look at flyweight factory, which is designed to be used to create the shareable concreteflyweight:artist above.
Copy Code code as follows:
public class Artistfactory {
Hashtable pool = new Hashtable ();
Artist getartist (String key) {
Artist result;
result = (Artist) pool.get (key);
Create a new artist
if (result = = null) {
result = new Artist (key);
Pool.put (Key,result);
}
return result;
}
}
When you have thousands of or more CDs, the flyweight mode saves more space, and the more flyweight you share, the greater the space savings.