I searched a lot for an answer to my question and found several options, would like to know what you this is best practice.
Use case:
So I have a singleton class AccontManager
, that has an inner class which is relevant to only it, which is a User
class.
public class AccontManager {
private static final AccountManger ourInstance = new AccountManger();
private User user;
public static AccountManger getInstance()
{
return ourInstance;
}
private AccountManger(){}
public User getUser(){
return this.user;
}
private class User{
private String id;
private User (String id){
this.id = id;
}
}
}
Now, the situation is that User
fields have to access to outside package class, but the user class is unique only to this singleton class, hence the inner class has to be private.
Optimally, creating public getter methods in the inner class to get the user fields would be best, but since the inner class is private, that's not possible.
Possible practice:
Practice: create respective getter methods in the outer
AccountManager
class for theUser
field.Drawback: the user fields relate to the
User
, hence the outer class should not have getter methods to its fields.Code example:
public class AccountManger { private static final AccountManger ourInstance = new AccountManger(); private User user; public static AccountManger getInstance() { return ourInstance; } private AccountManger(){} public User getUser(){ return this.user; } public String getUserId() // <-- get User id { return user.id; } private class User{ private String id; private User (String id){ this.id = id; } } }
Practice: Change the
User
modifier topublic
, but keep the constructor private, so it couldn't be instantiated.Drawback: the inner
User
class will be visible as a member of theAccountManager
singleton class, which it shouldn't be.Code example:
public class AccountManager { private static final AccountManger ourInstance = new AccountManager(); private User user; public static AccountManager getInstance() { return ourInstance; } private AccountManger(){} public User getUser(){ return this.user; } public String getUserId() // <-- get User id { return user.id; } private class User{ private String id; private User (String id){ this.id = id; } } }
Practice:
- Create a public interface, for example,
IUser
- Make inner class
User
implement that interface - Add to the outer class, in this case
AccountManager
a getter method to theUser
instance
Drawback: An interface needs to be used in order to obtain the data from User
.
Code example:
public class AccountManager {
private static final AccountManager ourInstance = new AccountManager();
private User user;
public interface IUser{
String getName();
}
private AccountManager(){}
public static AccountManager getInstance(){
return ourInstance;
}
public User getUser(){
return this.user;
}
private class User implements IUser{
private String id;
private User(String id){
this.id = id;
}
@Override
public String getName() { // <-- get user id
return id;
}
}
}
So what do you think? Any one of the listed options? Some other way?
Thank you for your input