The shared meta-mode (Flyweight Pattern) is mainly used to reduce the number of objects created to reduce memory footprint and improve performance. This type of design pattern is a structural pattern, which provides a way to reduce the number of objects and improve the object structure required by the application.
The shared meta-pattern attempts to reuse existing similar objects, and if no matching objects are found, new objects are created. We will demonstrate this pattern by creating 5 objects to draw 20 circles distributed in different locations. Since there are only five available colors, the color property is used to check the existing Circle Object. 意图: The use of sharing technology to effectively support a large number of fine-grained objects. 主要解决: When there are a large number of objects, it may cause memory overflow. We abstract the common part of them. If we have the same business request, we directly return the existing objects in memory to avoid re-creation. 何时使用: 1. There are a lot of objects in the system. 2. These objects consume a lot of memory. 3. Most of the states of these objects can be externalized. 4. These objects can be divided into many groups according to the intrinsic state. When the extrinsic objects are removed from the objects, each group of objects can be replaced by one object. 5. The system does not depend on the identity of these objects, and these objects are indistinguishable. 如何解决: Judged by the unique identification code, if there is one in memory, the object identified by the unique identification code is returned. 关键代码: Use HashMap to store these objects. 应用实例: 1. String in JAVA, if any, return, if not, create a string and save it in the string cache pool. 2. the data pool of the database. 优点: Greatly reduce the creation of objects, reduce the memory of the system, and improve the efficiency. 缺点: Improve the complexity of the system, need to separate the external state and the internal state, and the external state has the nature of inherent, should not change with the change of the internal state, otherwise it will cause confusion of the system. 使用场景: 1. The system has a large number of similar objects. 2. Scenarios where buffer pools are needed. 注意事项: 1. Pay attention to the division of external state and internal state, otherwise it may cause thread safety problems. 2. These classes must be controlled by a factory object. We will create a Shape Interface and implementation of the Shape The entity class of the interface Circle . The next step is to define the factory class ShapeFactory . ShapeFactory There is one Circle Of HashMap Where the key name is Circle The color of the object. Whenever a request is received, a circle of a specific color is created. ShapeFactory Check it. HashMap The circle object in the, if found Circle Object, otherwise a new object stored in hashmap for later use will be created and returned to the client. FlyWeightPatternDemo Class usage ShapeFactory To get Shape Object. It will be directed to ShapeFactory Pass the message ( red / green / blue/ black / white To get the color of the object it needs Create an interface. Create an entity class that implements the interface. Create a factory that generates objects of entity classes based on the given information. Using this factory, objects of the entity class are obtained by passing color information. Execute the program and output the result: 6.14.1. Introduction ¶
6.14.2. Realize ¶
6.14.3. Step 1 ¶
Shape.java ¶
publicinterfaceShape{voiddraw();}
6.14.4. Step 2 ¶
Circle.java ¶
publicclassCircleimplementsShape{privateStringcolor;privateintx;privateinty;privateintradius;publicCircle(Stringcolor){this.color=color;}publicvoidsetX(intx){this.x=x;}publicvoidsetY(inty){this.y=y;}publicvoidsetRadius(intradius){this.radius=radius;}@Overridepublicvoiddraw(){System.out.println("Circle:
Draw() [Color :"+color+", x :"+x+", y :"+y+", radius :"+radius);}}
6.14.5. Step 3 ¶
ShapeFactory.java ¶
importjava.util.HashMap;publicclassShapeFactory{privatestaticfinalHashMap<String,Shape>circleMap=newHashMap<>();publicstaticShapegetCircle(Stringcolor){Circlecircle=(Circle)circleMap.get(color);if(circle==null){circle=newCircle(color);circleMap.put(color,circle);System.out.println("Creating
circle of color :"+color);}returncircle;}}
6.14.6. Step 4 ¶
FlyweightPatternDemo.java ¶
publicclassFlyweightPatternDemo{privatestaticfinalStringcolors[]={"Red","Green","Blue","White","Black"};publicstaticvoidmain(String[]args){for(inti=0;i<20;
++i){Circlecircle=(Circle)ShapeFactory.getCircle(getRandomColor());circle.setX(getRandomX());circle.setY(getRandomY());circle.setRadius(100);circle.draw();}}privatestaticStringgetRandomColor(){returncolors[(int)(Math.random()\*colors.length)];}privatestaticintgetRandomX(){return(int)(Math.random()\*100);}privatestaticintgetRandomY(){return(int)(Math.random()\*100);}}
6.14.7. Step 5 ¶
Creating circle of color : Black
Circle: Draw() [Color : Black, x : 36, y :71, radius :100
Creating circle of color : Green
Circle: Draw() [Color : Green, x : 27, y :27, radius :100
Creating circle of color : White
Circle: Draw() [Color : White, x : 64, y :10, radius :100
Creating circle of color : Red
Circle: Draw() [Color : Red, x : 15, y :44, radius :100
Circle: Draw() [Color : Green, x : 19, y :10, radius :100
Circle: Draw() [Color : Green, x : 94, y :32, radius :100
Circle: Draw() [Color : White, x : 69, y :98, radius :100
Creating circle of color : Blue
Circle: Draw() [Color : Blue, x : 13, y :4, radius :100
Circle: Draw() [Color : Green, x : 21, y :21, radius :100
Circle: Draw() [Color : Blue, x : 55, y :86, radius :100
Circle: Draw() [Color : White, x : 90, y :70, radius :100
Circle: Draw() [Color : Green, x : 78, y :3, radius :100
Circle: Draw() [Color : Green, x : 64, y :89, radius :100
Circle: Draw() [Color : Blue, x : 3, y :91, radius :100
Circle: Draw() [Color : Blue, x : 62, y :82, radius :100
Circle: Draw() [Color : Green, x : 97, y :61, radius :100
Circle: Draw() [Color : Green, x : 86, y :12, radius :100
Circle: Draw() [Color : Green, x : 38, y :93, radius :100
Circle: Draw() [Color : Red, x : 76, y :82, radius :100
Circle: Draw() [Color : Blue, x : 95, y :82, radius :100