Flyweight Definition: Avoid a large number of small costs (such as consumption) that have the same content, so that everyone share a class (category).
Why is it used?
The principle for object-oriented language is that everything is object, but if you really use, sometimes the number of objects may be very large, such as the word processing software, if each text is used as an object, thousands of words, the number of objects is a few Thousand, undoubtedly consuming memory, then we still want to "seek to save", find the common point of these object groups, design a class class, package can be shared, and some features depends on the application (Context), is Unswered, this is also in Flyweight, two important concepts Internal statin INTRINSIC and external status Extrinsic.
To put the white point, it is first pinching a raw model, and then the specific model of each character is produced, it is clear that different new objects are required here, so Factory mode often appears in the Flyweight mode .flyweight The internal status is used to share, Flyweight Factory is responsible for maintaining a Flyweight Pool to store objects in the internal state.
Flyweight mode is an improved program efficiency and performance mode, which will greatly speed up the running speed of the program. Applications a lot: such as reading a series of strings from a database, there are many repetitions in these strings, then we These strings can be stored in the Flyweight pool (Pool).
how to use?
Let's start with the Flyweight abstraction interface:
Public interface flyweight {public void Operation (EXTRINSICSTATE STATE);} // Appreciation data type (self-design) public interface extrinsicState {}
Below is a concreteflyweight, increasing memory space for internal status, and ConcreteflyWeight must be shared. Any state it saves must be internal (Intrinsic), that is, ConcreteFlyWeight must be in combination with it. Not related.
Public Class ConcreteflyWeight Implements Flyweight {Private IntrinsicsTate State; Public Void Operation (ExtrinsicsTate State) {// Specific Operation}}
Of course, not all Flyweight specific implementation subclasses 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 client 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 it to the pool, and then return this object.
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 (); flyweights.put (key, flyweight);}}} To this, the basic framework of Flyweight mode is ready, let's see how to 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 Apply in the data source such as XML, we have mentioned above, when reading a string 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. The name of the record is likely to repeat, that is, may have the same singing Multiple different tracks of different essays CD. We will "singers name" as a shared ConcreteflyWeight. The other two fields are UnsharedConcreteflyweight. First look at the contents of the data source XML file:
XML Version = "1.0"?>
Public class artist {// internal status private string name; // Note That Artist is immutable. String getName () {return name;} artist (string n) {name = n;}} then look at Flyweight Factory, specifically used Manufacture of Shared ConcreteflyWeight: Artist