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, each letter is an object, so there are 52 uppercase and lowercase letters, 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();}
Implement a specific class:
package Flyweight;public class ConcreteFlyweight extends Flyweight{ private String string; public ConcreteFlyweight(String str){ string = str; } public void operation() { System.out.println("Concrete---Flyweight : " + string); }}
Implement a factory method class:
Package Flyweight; import java. util. hashtable; public class FlyweightFactory {private Hashtable flyweights = new Hashtable (); // Example 1 public FlyweightFactory () {}public Flyweight getFlyWeight (Object obj) {Flyweight flyweight = (Flyweight) flyweights. get (obj); // ---------------- 2 if (flyweight = null) {// consumed 3 // generate new ConcreteFlyweight flyweight = new ConcreteFlyweight (String) obj); flyweights. put (obj, flyweight); // ---------------------------------------- 5} return flyweight; // limit 6} public int getFlyweightSize () {return flyweights. size ();}}
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("Qutr"); fly3 = factory.getFlyWeight("Google"); fly4 = factory.getFlyWeight("Google"); fly5 = factory.getFlyWeight("Google"); fly6 = factory.getFlyWeight("Google"); } public void showFlyweight(){ fly1.operation(); fly2.operation(); fly3.operation(); fly4.operation(); fly5.operation(); fly6.operation(); int objSize = factory.getFlyweightSize(); System.out.println("objSize = " + objSize); } public static void main(String[] args){ System.out.println("The FlyWeight Pattern!"); FlyweightPattern fp = new FlyweightPattern(); fp.showFlyweight(); }}
The running result is as follows:
Concrete---Flyweight : GoogleConcrete---Flyweight : QutrConcrete---Flyweight : GoogleConcrete---Flyweight : GoogleConcrete---Flyweight : GoogleConcrete---Flyweight : GoogleobjSize = 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.
Summary:
The Flyweight mode is so important because it can help you save a lot of memory space in a complex system. In JAVA, the String type uses the metadata mode. The String object is of the final type and cannot be changed once it is created. In JAVA, string constants exist in the constant pool. JAVA ensures that a String constant has only one copy in the constant pool. String a = "abc", where "abc" is a String constant.
If you are familiar with java, you should know 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. We can see that the if condition compares the addresses of a and B, or the memory space.
Core Summary: objects that can be shared. That is to say, the returned objects of the same type are actually the same instance. When the client requests to generate an object, the factory will check whether an instance of this object exists, if this object exists, an instance is directly returned. If it does not exist, a instance is created and saved. This means some Singleton mode. Generally, the factory class has a set type member variable to save objects, such as hashtable and vector. In java, database connection pools and thread pools are applications that use the metadata mode.
Author: jason0539
Weibo: http://weibo.com/2553717707
Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)