Java best practice for outside access to inner private class fields -
i searched lot answer question , found several options, know best practice.
use case: have singleton class accontmanager, has inner class relevant it, user class.
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; } private class user{ private string id; private user (string id){ this.id = id; } } } now, situation user fields have access outside package class, user class unique singleton class, hence inner class has private.
optimally, creating public getter methods in inner class user fields best, since inner class private, that's not possible.
possible practice:
practice: create respective getter methods in outer
accountmanagerclassuserfield.drawback: user fields relate
user, hence outer class should not have getter methods 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() // <-- user id { return user.id; } private class user{ private string id; private user (string id){ this.id = id; } } }practice: change
usermodifierpublic, keep constructor private, couldn't instantiated.drawback: inner
userclass visible member ofaccountmanagersingleton class, 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() // <-- user id { return user.id; } private class user{ private string id; private user (string id){ this.id = id; } } }practice:
- create public interface, example,
iuser - make inner class
userimplement interface - add outer class, in case
accountmanagergetter methoduserinstance
drawback: interface needs used in order obtain data
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() { // <-- user id return id; } } }
think?
1 of listed options?
other way?- create public interface, example,
thank input
i user class needs private, , accountmanager should implement getters , setters it's attributes.
you won't have know user, because accountmanager tell can know. plus, can obey law of demeter, thing.
imo, inner classes should used way make code in outer class cleaner, , shouldn't able used elsewhere.
but guess making constructor in user private better nothing.
Comments
Post a Comment