如何为User模型类创建单例类(Android,java)

时间:2016-03-28 09:40:27

标签: java android

我有一个带有get和setter方法的User模型类,它也实现了Parcelable

public class User implements Parcelable
{
    private String email;

    private String password;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

现在我应该在User模型类中创建一个单独的UserSingleton类或实现。

public class UserSingleton
{
    private static User instance = null;

    private UserSingleton()
    {
        // Exists only to defeat instantiation.
    }

    public static User getInstance()
    {
        if(instance == null)
        {
            instance = new User();
        }

        return instance;
    }
}

请建议我首选方法

0 个答案:

没有答案