Detailed Explanation of Flyweight Pattern Examples of Java Design Patterns

The example of this article describes the Flyweight pattern of Java design patterns. Share for your reference, the details are as follows:

Explain the concept: that is to say, if there are multiple identical objects in a system, only one copy can be shared, and each object does not have to be instantiated. For example, in a text system, each letter defines an object, then there are 52 uppercase and lowercase letters in total, so 52 objects must be defined. If there is a 1M text, then how many letters are there, if each letter defines an object, then the memory would have burst. Then if each letter shares an object, then resources are greatly saved.

In the Flyweight mode, the Factory mode often appears in the Flyweight (flyweight) mode due to the generation of various objects. The internal state of Flyweight is used for sharing, and the Flyweight factory is responsible for maintaining an object storage pool (Flyweight Pool) to store the objects of the 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 applications. Here is an example:

First define an abstract Flyweight class:

  1. package Flyweight;
  2. public abstract class Flyweight{
  3. public abstract void operation();
  4. }
copy code

Implement a concrete class:

  1. package Flyweight;
  2. public class ConcreteFlyweight extends Flyweight{
  3. private String string;
  4. public ConcreteFlyweight(String str){
  5. string = str;
  6. }
  7. public void operation()
  8. {
  9. System.out.println("Concrete---Flyweight : " + string);
  10. }
  11. }
copy code

Implement a factory method class:

  1. package Flyweight;
  2. import java.util.Hashtable;
  3. public class FlyweightFactory{
  4. private Hashtable flyweights = new Hashtable();//----------------------------1
  5. public FlyweightFactory(){}
  6. public Flyweight getFlyWeight(Object obj){
  7. Flyweight flyweight = (Flyweight) flyweights.get(obj);//----------------2
  8. if(flyweight == null){//---------------------------------------------------3
  9. //Generate new ConcreteFlyweight
  10. flyweight = new ConcreteFlyweight((String)obj);
  11. flyweights.put(obj, flyweight);//--------------------------------------5
  12. }
  13. return flyweight;//---------------------------------------------------------6
  14. }
  15. public int getFlyweightSize(){
  16. return flyweights.size();
  17. }
  18. }
copy code

This factory method class is very critical, and it is explained in detail here:

A Hashtable is defined at 1 to store each object; the object to be instantiated is selected at 2, and the object is returned at 6. If there is no object to be selected in the Hashtable, the variable flyweight is null at this time, resulting in A new flyweight is stored in the Hashtable and the object is returned.

Finally look at the call to Flyweight:

  1. package Flyweight;
  2. import java.util.Hashtable;
  3. public class FlyweightPattern{
  4. FlyweightFactory factory = new FlyweightFactory();
  5. Flyweight fly1;
  6. Flyweight fly2;
  7. Flyweight fly3;
  8. Flyweight fly4;
  9. Flyweight fly5;
  10. Flyweight fly6;
  11. /** *//** Creates a new instance of FlyweightPattern */
  12. public FlyweightPattern(){
  13. fly1 = factory.getFlyWeight("Google");
  14. fly2 = factory.getFlyWeight("Qutr");
  15. fly3 = factory.getFlyWeight("Google");
  16. fly4 = factory.getFlyWeight("Google");
  17. fly5 = factory.getFlyWeight("Google");
  18. fly6 = factory.getFlyWeight("Google");
  19. }
  20. public void showFlyweight(){
  21. fly1.operation();
  22. fly2.operation();
  23. fly3.operation();
  24. fly4.operation();
  25. fly5.operation();
  26. fly6.operation();
  27. int objSize = factory.getFlyweightSize();
  28. System.out.println("objSize = " + objSize);
  29. }
  30. public static void main(String[] args){
  31. System.out.println("The FlyWeight Pattern!");
  32. FlyweightPattern fp = new FlyweightPattern();
  33. fp.showFlyweight();
  34. }
  35. }
copy code

The following is the running result:

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 actual objects.

Summarize:

Flyweight (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 flyweight pattern. String object is final type, object cannot be changed once created. String constants are stored in the constant pool in JAVA, and JAVA will ensure that there is only one copy of a string constant in the constant pool. String a="abc", where "abc" is a string constant.

Those familiar with java should know the following example:

  1. String a = "hello";
  2. String b = "hello";
  3. if(a == b)
  4. System.out.println("OK");
  5. else
  6. System.out.println("Error");
copy code

The output is: OK. It can be seen that the if condition compares the addresses of two a and b, which can also be said to be memory space.

The core summary is that 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 detect whether there is an instance of this object, and if it exists, it will directly return this object instance, If it doesn't exist, create one and save it, which is a bit of a singleton pattern. Usually the factory class will have a member variable of a collection type to store objects, such as hashtable, vector, etc. In java, database connection pools, thread pools, etc. are applications that use the flyweight pattern.

Readers who are interested in more java related content can check the special topics on this site: "Java Data Structure and Algorithm Tutorial", "Java Operation DOM Node Skills Summary", "Java File and Directory Operation Skills Summary" and "Java Cache Operation Skills Summary"

http://www.ljhseo.com/
http://www.xyrjkf.net/
http://www.xyrjkf.cn/
http://www.xyrjkf.com.cn/
http://www.zjdygsi.cn/
http://www.zjdaiyun.cn/
http://www.jsdygsi.cn/
http://www.xyrjkf.top/
http://www.xyrjkf.com/
http://www.daiyunzj.cn/
http://ljhseo.com/
http://xyrjkf.net/
http://xyrjkf.cn/
http://xyrjkf.com.cn/
http://zjdygsi.cn/
http://zjdaiyun.cn/
http://jsdygsi.cn/
http://xyrjkf.top/
http://xyrjkf.com/
http://daiyunzj.cn/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325369603&siteId=291194637