Definition: It uses shared objects to minimize memory usage and share information to as many similar objects as possible; it is suitable for large objects that use unacceptable amounts of memory due to repetition.
Features: greatly reduces object creation, reduces system memory, and improves efficiency.
Applications in enterprise-level development and common frameworks: database connection pool, String constant cache pool
Specific code example:
import java.util.HashMap;import java.util.Map;import java.util.Random;public class Demo { public static void main(String[] args) { for(int i = 0 ; i < 10 ; i++){ Circle circle = new Circle(getColor()); circle.setRadius(getRadius()); circle.setX(getZ()); circle.setY(getZ()); circle.draw(); } } public static String getColor(){ String[] colors = {"red","orange","yellow","cyan","green"}; Random random = new Random(); int index = random.nextInt(4); return colors[index]; } public static double getRadius(){ Random random = new Random(); return random.nextDouble()*20; } public static int getZ(){ Random random = new Random(); return random.nextInt(100); }}/** * Abstract Encyclopedia Class* Here is an example of drawing a graph: for example, drawing a circle, adding color to fix it, the way of drawing a circle is the same, the difference is the position of the circle and the radius of the circle*/interface Shape{ public void draw();}/** * Specific Enjoyment Class* Here a specific Enjoyment Class is created, the class contains data that can be shared and data that cannot be shared* For example: shared colors and invisible circle drawing methods, unshared radius and coordinates*/class Circle implements Shape{ private int x; private int y; private double radius; private String color; public Circle(String color) { this.color = color; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public void draw() { System.out.println("Draw a circle with the center coordinates of: ("+this.x+","+this.y+"), and the radius is "+this.radius+","+this.color+"); } }/** * Factory class: The specific embodiment of the Xiangyuan mode is actually implemented in this section. In this section, we can clearly understand which attributes or data are shared* Here assuming that the color of the circle is fixed, we can only draw a few fixed colors of circles* In this example, the corresponding shared data should be the corresponding color attributes and invisible restoration method. As explained earlier, the way of drawing all circles is the same*/class CircleFactory{ private static Map<String, Circle> map = new HashMap<>(); public static Circle getCircle(String color){ Circle c = map.get(color); if(c == null){ c = new Circle(color); map.put(color, c); return c; } return c; }}The Xiangyuan mode is mainly used to solve the phenomenon that a large number of similar objects occupy a large amount of memory. Because memory is a precious resource, we classify these similar objects and extract the same parts for sharing. This can significantly save memory overhead. However, we must remember one premise. While saving memory, we increase the code running time as the premise, so sometimes we need to balance time and memory overhead.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.