GOF: Use the sharing technology to effectively support a large number of fine-grained objects.
To explain the concept, if there are multiple identical objects in a system, you can share only one object without instantiating one object. For example, in a text system (Here we reference the example in the GOF book), each letter is an object, so there are 52 uppercase and lowercase letters in total, so 52 objects must be defined. If there is a 1 m text, then there are many letters. If each letter defines an object, the memory will have exploded. Therefore, if each letter shares an object, the resource is greatly reduced.
In the Flyweight mode, the Factory mode is often used in the Flyweight mode because various objects are generated. The internal state of Flyweight is used for sharing. Flyweight factory maintains an object storage Pool to store objects in the internal state. The Flyweight mode is a mode for improving program efficiency and performance, which greatly speeds up program running. There are many application scenarios. The following example shows how to use the Flyweight mode:
First define an abstract Flyweight class:
Package Flyweight;
Public abstract class Flyweight ... { Public abstract void operation (); } // End abstract class Flyweight |
When implementing a specific class:
Package Flyweight;
Public class ConcreteFlyweight extends Flyweight ... { Private String string; Public ConcreteFlyweight (String str) ... { String = str; } // End ConcreteFlyweight (...)
Public void operation () ... { System. out. println ("Concrete --- Flyweight:" + string ); } // End operation ()
} // End class ConcreteFlyweight |
Implement a factory method class:
Package Flyweight; Import java. util. Hashtable;
Public class FlyweightFactory ... { Private Hashtable flyweights = new Hashtable (); // ---------------------------- 1 Public FlyweightFactory ()...{}
Public Flyweight getFlyWeight (Object obj) ... { Flyweight flyweight = (Flyweight) flyweights. get (obj); // -------------- 2
If (flyweight = null)... {// --------------------------------------------------------- 3 // Generate a new ConcreteFlyweight Flyweight = new ConcreteFlyweight (String) obj ); Flyweights. put (obj, flyweight); // ---------------------------------------- 5 } Return flyweight; // ----------------------------------------------------------- 6 } // End GetFlyWeight (...)
Public int getFlyweightSize () ... { Return flyweights. size (); } } // End class FlyweightFactory |
This factory method class is very important. Here we will explain in detail:
Define a Hashtable at 1 to store each object; select the object to be instantiated at 2 and return the object at 6. If there is no object to be selected in Hashtable, in this case, if the variable flyweight is null, a new flyweight is generated and stored in Hashtable, and the object is returned.
Finally, let's look at Flyweight's call:
Package Flyweight; Import java. util. Hashtable;
Public class FlyweightPattern ...{ FlyweightFactory factory = new FlyweightFactory (); Flyweight fly1; Flyweight fly2; Flyweight fly3; Flyweight fly4; Flyweight fly5; Flyweight fly6;
/*** // *** Creates a new instance of FlyweightPattern */ Public FlyweightPattern ()...{ Fly1 = factory. getFlyWeight ("Google "); Fly2 = factory. getFlyWeight ("QTR "); Fly3 = factory. getFlyWeight ("Google "); Fly4 = factory. getFlyWeight ("Google "); Fly5 = factory. getFlyWeight ("Google "); Fly6 = factory. getFlyWeight ("Google "); } // End FlyweightPattern ()
Public void showFlyweight () ... { Fly1.operation (); Fly2.operation (); Fly3.operation (); Fly4.operation (); Fly5.operation (); Fly6.operation (); Int objSize = factory. getFlyweightSize (); System. out. println ("objSize =" + objSize ); } // End showFlyweight ()
Public static void main (String [] args) ... { System. out. println ("The FlyWeight Pattern! "); FlyweightPattern fp = new FlyweightPattern (); Fp. showFlyweight (); } // End main (...) } // End class FlyweightPattern |
The running result is as follows:
Concrete --- Flyweight: Google Concrete --- Flyweight: QTR Concrete --- Flyweight: Google Concrete --- Flyweight: Google Concrete --- Flyweight: Google Concrete --- Flyweight: Google ObjSize = 2 |
We have defined six objects, five of which are the same. Google should share an object according to the definition of the Flyweight mode, in the actual number of objects, we can see that there are only two actual objects.
A simple UML diagram is provided below:
Summary:
The Flyweight mode is so important because it can help you save a lot of memory space in a complex system. I think it is very appropriate to give an example of Text Processing in the GOF book. In Java, the String type is special. Why? Let's look at the following example:
String a = "hello "; String B = "hello "; If (a = B) System. out. println ("OK "); Else System. out. println ("Error "); |
The output result is OK. People with a little experience can see that the if condition compares the addresses of a and B, or the memory space. So does the Sting implementation use the Flyweight mode? It is unknown that it has not been studied yet.