Flyweight
Flyweight (Flyweight Pattern) is mainly used to reduce the number of objects created to reduce memory usage and improve performance. This type of design pattern belongs structural model, which reduces the number of objects is provided so as to improve the structure of the object in a manner required by the application.
Flyweight try to reuse existing objects of the same, if a matching object is not found, create new objects. We will draw 20 distributed in different locations of the circle by creating five objects to demonstrate this pattern. Because only five kinds of available colors, the color attribute is used to check existing Circle object.
Personal understanding:
java string is used Flyweight, if already exists, use it, otherwise it is re-created.
Introduction
Intent: Use sharing to support a large number of fine-grained objects.
The main solution: when there is a large number of objects, it may cause memory overflow, which we have a common part of the abstract, if the same service request directly back existing in-memory objects to avoid re-created.
When to use: 1, the system has a large number of objects. 2, these objects consume large amounts of memory. 3, the status of these objects can be external of the majority. 4, these objects can be divided into many groups according intrinsic state, when the object is removed from the outer Yun objects, each object group can be used instead of an object. 5, the system does not depend on the identity of these objects, these objects are indistinguishable.
How to solve: judge with a unique identification code, if any in memory, this object is identified by a unique identification code is returned.
The key code: use HashMap to store these objects.
Application examples: 1, the JAVA String, if it returns, if not then create a string stored in the string buffer pool inside. 2, the data pool database.
Advantages: significantly reduce the creation of objects, reducing the system's memory, so that improve efficiency.
Disadvantages: increase the complexity of the system, the need to separate the internal and external state status, but also outside of the state has an inherent nature, it should not change with the internal state changes, otherwise it will create chaos system.
Be used: 1, the system has a large number of similar objects. 2, the required buffer pool scene.
Note: 1, pay attention to the state division of external and internal state, otherwise it may cause thread-safety issues. 2, the class must have a factory object to be controlled.
achieve
We will create a Shape interface and implementation of the Shape entity class interface Circle . The next step is the definition of a factory class ShapeFactory .
ShapeFactory a Circle of the HashMap , wherein the key named Circle color of the object. Whenever a request is received, it will create a circle of a particular color. ShapeFactory check its HashMap Circle object, if found Circle object that is returned, otherwise it will create a stored in the hashmap to prepare for the subsequent use of the new object, and the object is returned to the client.
FlyWeightPatternDemo , our demo class uses ShapeFactory to get the Shape object. It to ShapeFactory transfer information ( Red / Green / Blue / Black / White ), so that it acquires the color desired object.
step 1
Create an interface.
Shape.java
void draw();
}
Step 2
Creating an entity class that implements the interface.
Circle.java
private String color;
private int x;
private int y;
private int radius;
public Circle(String color){
this.color = color;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setRadius(int radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Circle: Draw() [Color : " + color
+", x : " + x +", y :" + y +", radius :" + radius);
}
}
Step 3
Create a factory to produce the object entity classes based on the given information.
ShapeFactory.java
public class ShapeFactory {
private static final HashMap<String, Shape> circleMap = new HashMap<>();
public static Shape getCircle(String color) {
Circle circle = (Circle)circleMap.get(color);
if(circle == null) {
circle = new Circle(color);
circleMap.put(color, circle);
System.out.println("Creating circle of color : " + color);
}
return circle;
}
}
Step 4
Using this facility, the object acquired entity classes by passing the color information.
FlyweightPatternDemo.java
private static final String colors[] =
{ "Red", "Green", "Blue", "White", "Black" };
public static void main(String[] args) {
for(int i=0; i < 20; ++i) {
Circle circle =
(Circle)ShapeFactory.getCircle(getRandomColor());
circle.setX(getRandomX());
circle.setY(getRandomY());
circle.setRadius(100);
circle.draw();
}
}
private static String getRandomColor() {
return colors[(int)(Math.random()*colors.length)];
}
private static int getRandomX() {
return (int)(Math.random()*100 );
}
private static int getRandomY() {
return (int)(Math.random()*100);
}
}
Step 5
The implementation of the program, output:
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 :