singleton using enum

丶灬走出姿态 提交于 2019-12-17 23:03:11

问题


I read a lot on stackoverflow regarding the creation of singleton classes using enum. I must have missed something because i can't reach the INSTANCE anywhere.

this is my code:

public class UserActivity {

    private DataSource _dataSource;
    private JdbcTemplate _jdbcTemplate;

    static enum Singleton {
        INSTANCE;

        private static final UserActivity singleton = new UserActivity();

        public UserActivity getSingleton() {
            return singleton;
        }
    }

    public UserActivity() {
        this._dataSource = MysqlDb.getInstance().getDataSource();
        this._jdbcTemplate = new JdbcTemplate(this._dataSource);
    }

    public void dostuff() {
     ...
    }
}

and outside I'm trying to do

UserActivity.INSTANCE.getSingleton()

or

UserActivity.Singleton.

but eclipse's code completion doesn't find anything

thanks!


回答1:


The trick is to make the enum itself the singleton. Try this:

public enum UserActivity {
    INSTANCE;

    private DataSource _dataSource;
    private JdbcTemplate _jdbcTemplate;

    private UserActivity() {
        this._dataSource = MysqlDb.getInstance().getDataSource();
        this._jdbcTemplate = new JdbcTemplate(this._dataSource);
    }

    public void dostuff() {
     ...
    }
}

// use it as ...
UserActivity.INSTANCE.doStuff();



回答2:


INSTANCE is a member of Singleton, not of UserActivity - so you'd need:

UserActivity.Singleton.INSTANCE.getSingleton();

However, you haven't actually made UserActivity a singleton - normally you'd make the type itself an enum, not embed an enum within the type...




回答3:


public class UserActivity {

    private DataSource _dataSource;
    private JdbcTemplate _jdbcTemplate;

    private static enum Singleton { // private, why not
        INSTANCE;

        private static final UserActivity singleton = new UserActivity();

        public UserActivity getSingleton() {
            return singleton;
        }
    }

    private UserActivity() { // private !!(*)
        this._dataSource = MysqlDb.getInstance().getDataSource();
        this._jdbcTemplate = new JdbcTemplate(this._dataSource);
    }


    public static UserActivity getInstance() {
     return UserActivity.Singleton.INSTANCE.getSingleton();
    } 

    public void dostuff() {
     ...
    }
}

and call UserActivity.getInstance().doStuff();

You can't call the constructor (*) and you can only get an instance of your UserActivity() class via the INSTANCE in the private enum - which is guaranteed to be created once and only once



来源:https://stackoverflow.com/questions/8027701/singleton-using-enum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!