directory

  • define
  • Usage scenarios
  • Code implementation

define

The share pattern enables shared objects to efficiently support a large number of fine-grained objects. The share pattern abstracts out the common parts, and if there is the same business request, returns the existing object in memory, avoiding re-creation. In the share mode, some objects can be shared. The shared state is internal and the internal state does not change with the environment. Objects that are not shareable are called external states and do not change as the environment changes.

Usage scenarios

  1. A large number of similar objects need to be generated in the system.
  2. Fine-grained objects have a close external state, and the internal state is context-independent, that is, the object has no specific identity.
  3. Scenarios where buffer pools are required.

Code implementation

  1. Split the class member variable that needs to be overwritten as a member into two parts:
    • Intrinsic state: A member variable that contains unchanging data that can be reused in many objects.
    • External state: Member variables that contain different situational data for each object.
  2. Leave the member variables in the class that represent the internal state and set their properties to be unmodifiable. These variables are available only in the constructor for initial values.
  3. Find all methods that use external state member variables, create a new parameter for each member variable used in the method, and use that parameter instead of the member variable.
  4. Optionally create a factory class to manage the share cache pool, which checks for existing shares as they are created. If you choose to use a factory, the client can only request the privilege through the factory, and they need to pass the intrinsic state of the privilege as a parameter to the factory.
  5. The client must store and calculate the value of the external state (scene) because only then can the methods of the metadata object be called. For ease of use, the external state and member variables that reference the member can be moved into a separate situation class.
public class Circle {
    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;
    }

    public void draw(a) {
        System.out.println("Circle: Draw() [Color : " + color + ", x : " + x + ", y :" + y + ", radius :"+ radius); }}public class ShapeFactory {

    private static final HashMap<String, Circle> circleMap = new HashMap<>();

    public static Circle getCircle(String color) {
        Circle circle = circleMap.get(color);
        if (circle == null) {
            circle = new Circle(color);
            circleMap.put(color, circle);
            System.out.println("Creating circle of color : " + color);
        } else {
            System.out.println("Get local circle of color : " + color);
        }
        returncircle; }}public class DemoTest {
    public static final String colors[] = {"Red"."Green"."Blue"."White"."Black"};

    @Test
    public void test(a) {
        for (int i = 0; i < 10; ++i) {
            Circle circle = ShapeFactory.getCircle(getRandomColor());
            circle.setX(getRandomX());
            circle.setY(getRandomY());
            circle.setRadius(100); circle.draw(); }}private static String getRandomColor(a) {
        return colors[(int) (Math.random() * colors.length)];
    }

    private static int getRandomX(a) {
        return (int) (Math.random() * 100);
    }

    private static int getRandomY(a) {
        return (int) (Math.random() * 100);
    }
}

Creating circle of color : White
Circle: Draw() [Color : White, x : 20, y :92, radius :100
Creating circle of color : Blue
Circle: Draw() [Color : Blue, x : 11, y :10, radius :100
Creating circle of color : Green
Circle: Draw() [Color : Green, x : 13, y :8, radius :100
Get local circle of color : Green
Circle: Draw() [Color : Green, x : 54, y :41, radius :100
Creating circle of color : Black
Circle: Draw() [Color : Black, x : 11, y :1, radius :100
Get local circle of color : Blue
Circle: Draw() [Color : Blue, x : 83, y :27, radius :100
Get local circle of color : White
Circle: Draw() [Color : White, x : 82, y :71, radius :100
Get local circle of color : Blue
Circle: Draw() [Color : Blue, x : 68, y :72, radius :100
Get local circle of color : White
Circle: Draw() [Color : White, x : 3, y :52, radius :100
Get local circle of color : Black
Circle: Draw() [Color : Black, x : 28, y :54, radius :100
Copy the code