This article describes the Enjoyment Mode of Java Design Pattern. Share it for your reference, as follows:
Explain the concept: that is, if there are multiple identical objects in a system, then just share one copy, and there is no need to instantiate an object for each. For example, in a text system, each letter is defined as an object, so there are 52 upper and lower case letters, so 52 objects need to be defined. If there is a 1M text, then there are so many letters. If each letter defines an object, the memory would have exploded long ago. Then if each letter shares an object, then it will greatly save resources.
In Flyweight mode, since various objects are to be generated, Factory mode often appears in Flyweight mode. The internal state of Flyweight is used to share. Flyweight factory is responsible for maintaining an object storage pool (Flyweight Pool) to store objects with internal state. Flyweight mode is a mode that improves program efficiency and performance, which will greatly speed up the running speed of the program. There are many application occasions, as follows:
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();//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- null){//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------This factory method class is very critical, here is a detailed explanation:
A Hashtable is defined at 1 to store each object; an object to be instantiated is selected at 2, and the object to be returned at 6. If there is no object to be selected in the Hashtable, the variable flyweight is null, and a new flyweight is generated to be stored in the Hashtable, and the object is returned.
Finally, let’s take a look at the call of Flyweight:
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();}}Here are the running results:
Concrete---Flyweight: Google
Concrete---Flyweight: Qutr
Concrete---Flyweight: Google
Concrete---Flyweight: Google
Concrete---Flyweight: Google
Concrete---Flyweight: Google
objSize = 2
We have defined 6 objects, 5 of which are the same. According to the definition of Flyweight mode, "Google" should share an object. In the actual number of objects, we can see that there are only 2 objects.
Summarize:
Flyweight mode is so important because it can help you save a lot of memory space in a complex system. In the JAVA language, the String type uses the Encyclopedia mode. The String object is of final type and cannot be changed once it is created. In JAVA, string constants are present in the constant pool. JAVA will ensure that a string constant has only one copy in the constant pool. String a="abc", where "abc" is a string constant.
Those who are familiar with Java should know the following example:
String a = "hello";String b = "hello";if(a == b)System.out.println("OK");elseSystem.out.println("Error");The output result is: OK. It can be seen that if condition compares the addresses of two a and b, or it can be said to be memory space
Core summary: objects that can be shared, that is, objects of the same type returned are actually the same instance. When the client requires an object to be generated, the factory will detect whether an instance of this object exists. If it exists, then directly return this object instance. If it does not exist, create and save it. This has some meaning of singleton pattern. Usually, the factory class will have a set type member variable to save objects, such as hashtable, vector, etc. In Java, database connection pools, thread pools, etc. are applications that use the Enyuan mode.
For more Java-related content, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.