mardi 7 novembre 2017

Singleton object per user in java web app

My Enterprise application requires an Singleton ShoppingCart object(per user),this cart object should not be technically static(which may lead to memory leak).

do i need to make changes to the code (or) apply any pattern to the web application, to achieve singleton per user?

public final class ShoppingCartSingleTon {

    private static ShoppingCartSingleTon instance = null;

    private ShoppingCartSingleTon() {}

    public static ShoppingCartSingleTon getInstance() {
        if (instance == null) {
            synchronized(ShoppingCartSingleTon.class) {
                if (instance == null) {
                    instance = new ShoppingCartSingleTon();
                }
            }
        }
        return instance;
    }
}

Aucun commentaire:

Enregistrer un commentaire