I. overview
Sharing mode: "sharing" is the meaning of sharing, which means that one thing is shared by all, and this is the ultimate purpose of the mode.
Sharing element mode is similar to singleton mode in that only one object is generated to be shared. There is a problem here, that is, the modification of shared objects. In order to avoid this situation, we extract the public part of these objects, or the unchanged part, to form an object. This object can avoid the problem of modification.
The purpose of sharing element is to reduce the unnecessary memory consumption. It is not necessary to create a separate object for each visitor to reduce the memory consumption by centralizing multiple accesses to the same object.
Two, example
Let's take a simple example:
Building interface: JianZhu
public interface Jianzhu { void use(); }
Gymnasium implementation class: TiYuGuan
public class TiYuGuan implements Jianzhu { private String name; private String shape; private String yundong; public TiYuGuan(String yundong){ this.setYundong(yundong); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getShape() { return shape; } public void setShape(String shape) { this.shape = shape; } public String getYundong() { return yundong; } public void setYundong(String yundong) { this.yundong = yundong; } @Override public void use() { System.out.println("The stadium was used to hold the Olympic Games" + " The movement is:"+ yundong+" The shape is:"+shape+ " The name is:"+name); } }
Construction factory class: JianZhuFactory
import java.util.*; public class JianZhuFactory { private static final Map<String,TiYuGuan> tygs = new HashMap<String,TiYuGuan>(); public static TiYuGuan getTyg(String yundong){ TiYuGuan tyg = tygs.get(yundong); if(tyg == null){ tyg = new TiYuGuan(yundong); tygs.put(yundong,tyg); } return tyg; } public static int getSize(){ return tygs.size(); } }
Test class: client
public class Clienter { public static void main(String[] args) { String yundong ="Football"; for(int i = 1;i <= 5;i++){ TiYuGuan tyg = JianZhuFactory.getTyg(yundong); tyg.setName("China Gymnasium"); tyg.setShape("circular"); tyg.use(); System.out.println("The number of objects in the object pool is:"+JianZhuFactory.getSize()); } } }
Execution result:
The stadium is used to hold the Olympic Games: football shape: circle Name: China Stadium The number of objects in the object pool is: 1 The stadium is used to hold the Olympic Games: football shape: circle Name: China Stadium The number of objects in the object pool is: 1 The stadium is used to hold the Olympic Games: football shape: circle Name: China Stadium The number of objects in the object pool is: 1 The stadium is used to hold the Olympic Games: football shape: circle Name: China Stadium The number of objects in the object pool is: 1 The stadium is used to hold the Olympic Games: football shape: circle Name: China Stadium The number of objects in the object pool is: 1
III. mode analysis
In the example above, you can use factory mode to cooperate, create object pool, and test the loop in the class. You can imagine that there are five games to be held, and the venue of each game is the gymnasium
It can be seen from the execution results that there is only one object in this object pool (HashMap) all the time. When it is first used, the object is created, and every subsequent call uses that object, and it will not be recreated.
In fact, there are instances of this type in Java: String.
In Java, the String class is defined as final (immutable). Strings in the JVM are generally stored in the String constant pool, which was in the constant pool before jdk 6.0 and in the permanent generation. In JDK 7.0, the JVM takes them out of the permanent generation and places them in the heap.
We use the following code to define two strings that point to string values in the same string constant pool.
String s1 = "abc"; String s2 = "abc";
If we compare s1==s2, the result is: true, because s1 and s2 store the same string address in the string constant pool. This is similar to the sharing element pattern we talked about today. Once the strings are defined, they can be shared, because they are immutable, and there is no hidden danger when they are called in multiple places.
IV. use scenarios
When we create a lot of objects in our project, and there are many same modules in these objects, we can extract these same modules and generate a single object by using the sharing element mode, and then use this object with many previous objects, which will undoubtedly save a lot of space.