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:

  1. practice: create respective getter methods in outer accountmanager class user field.

    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;        }   } } 
  2. practice: change user modifier public, keep constructor private, couldn't instantiated.

    drawback: inner user class visible member of accountmanager singleton 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;         }     }     }   
  3. practice:

    • create public interface, example, iuser
    • make inner class user implement interface
    • add outer class, in case accountmanager getter method user instance

    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?

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

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -