Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

死磕设计模式—单例模式 #48

Open
luokangyuan opened this issue Aug 24, 2019 · 2 comments
Open

死磕设计模式—单例模式 #48

luokangyuan opened this issue Aug 24, 2019 · 2 comments

Comments

@luokangyuan
Copy link
Owner

http://luokangyuan.com/si-ke-she-ji-mo-shi-dan-li-mo-shi/

@ddxygq
Copy link

ddxygq commented Jan 8, 2020

所有懒加载里面,在初始化完成,需要将new的对象复制,最后返回这个变量,而不是直接返回。

public static Singleton getInstance() {

        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    return new Singleton();
                }
            }
        }
        return singleton;
    }

建议修改成这样

public static Singleton getInstance() {

        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }

@luokangyuan
Copy link
Owner Author

感谢指出,已修改

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants