1. 程式人生 > 其它 >深度學習推薦系統演化

深度學習推薦系統演化

介紹

主要用於減少建立物件得數量,以減少記憶體佔用和提高效能。

意圖

減少物件的建立,儘量共享同一元素。

解決

當jvm記憶體中物件太多時,可能會造成記憶體溢位情況,我們將其中共同的物件抽象出來(即不用一個個的都建立物件),之後有相同的業務請求過來時,返回一個記憶體中有得物件,避免重新建立。

優點

  1. 大大減少物件得建立,降低重複記憶體,提供使用效率。

缺點

  1. 提高了系統的複雜度。需要分離出內部狀態和外部狀態,而外部狀態具有固化特性,不應該隨著內部狀態的改變而改變

使用場景

  1. 記憶體中的string字串,如果有則返回該字串地址,沒有則在記憶體中分配,再返回地址;
  2. 資料庫連線池。

UML

示例

定義一個畫圖得介面 Shape.java

package cn.geoaryblog.design.cretedg.flyweight;

public interface Shape {
    void draw();
}

圓 這個物件,之後要畫得就是他, Circle.java

package cn.geoaryblog.design.cretedg.flyweight;

public class Circle implements Shape{
    private String color;
    private int x;
    private int y;
    private int radius;

    public Circle(String color) {
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(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 int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    @Override
    public void draw() {
        System.out.println("Circle{" +
                "color='" + color + '\'' +
                ", x=" + x +
                ", y=" + y +
                ", radius=" + radius +
                '}');
    }
}

畫圖工廠,用於動態生成圓物件 ShapeFactory.java

package cn.geoaryblog.design.cretedg.flyweight;

import java.util.HashMap;

public class ShapeFactory {
    
    private static final HashMap<String, Shape> circleMap = new HashMap<>();
    // 新增 synchronized 執行緒鎖,防止在多執行緒情況下生成多個物件
    public static synchronized Shape getCircle(String color){
        Circle circle = (Circle) circleMap.get(color);
        if(circle == null){
            circle = new Circle(color);
            circleMap.put(color, circle);
            System.out.println("建立成功"+color);
        }
        return circle;
    }
}

客戶端 Client.java

package cn.geoaryblog.design.cretedg.flyweight;

public class Client {

    private static final String colors[] = {"red", "green", "blue", "white", "black"};

    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);
    }

    public static void main(String[] args) {
        // 10個執行緒每個執行緒生成30個物件
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 30; j++) {
                    Circle circle = (Circle) ShapeFactory.getCircle(getRandomColor());
                    circle.setX(getRandomX());
                    circle.setY(getRandomY());
                    circle.draw();
                }
            }).start();
        }
        /*for (int i = 0; i < 30; i++) {
            Circle circle = (Circle) ShapeFactory.getCircle(getRandomColor());
            circle.setX(getRandomX());
            circle.setY(getRandomY());
            circle.draw();
        }*/
    }
}

執行結果:

建立成功white
建立成功red
Circle{color='white', x=33, y=98, radius=0}
Circle{color='white', x=29, y=9, radius=0}
建立成功blue
Circle{color='red', x=71, y=18, radius=0}
建立成功black
Circle{color='blue', x=51, y=44, radius=0}
建立成功green
Circle{color='red', x=6, y=35, radius=0}
Circle{color='green', x=87, y=18, radius=0}
Circle{color='green', x=48, y=68, radius=0}
Circle{color='black', x=63, y=63, radius=0}
Circle{color='black', x=89, y=79, radius=0}
Circle{color='red', x=66, y=11, radius=0}
Circle{color='blue', x=71, y=4, radius=0}
Circle{color='blue', x=22, y=61, radius=0}
Circle{color='black', x=69, y=4, radius=0}
... ...
Circle{color='green', x=3, y=10, radius=0}
Circle{color='black', x=1, y=14, radius=0}
Circle{color='black', x=65, y=58, radius=0}
Circle{color='red', x=22, y=31, radius=0}
Circle{color='white', x=65, y=49, radius=0}
Circle{color='green', x=17, y=57, radius=0}
Circle{color='red', x=62, y=49, radius=0}
Circle{color='green', x=83, y=37, radius=0}
Circle{color='black', x=18, y=50, radius=0}
Circle{color='black', x=52, y=76, radius=0}
Circle{color='black', x=44, y=10, radius=0}
Circle{color='white', x=48, y=72, radius=0}
Circle{color='black', x=85, y=47, radius=0}
Circle{color='red', x=90, y=21, radius=0}

Process finished with exit code 0