vendredi 17 juillet 2020

How to restrict access to object for one specific class in Java? If possible, by implementing a design pattern

I am writing a code where I have some SharedData object(single object) between Admin object(single object) and User objects. Only one person(Admin or User) should be able to access SharedData at a time, I am using synchronised locks currently. For accessing SharedData, currently I am keeping the SharedData object in the Admin and User class. The problem is I want to restrict Users from changing some members of SharedData. But, the Admin object should still be able to change SharedData.

Note: The admin and user classes are very different, I can't extend user to create Admin. Admin is not just like user here. Some code for better understanding SharedData

public class SharedData {
    public ArrayList<Integer> numbersShown = new ArrayList<Integer>();
    public boolean completeFlag = false;    
}
public class Admin implements Runnable {
    
    private SharedData SharedData;
    //OTHER MEMBERS
    run()
    {
    //Change numberShown
    }

}
public class User implements Runnable {
    
    private SharedData SharedData;
    //OTHER MEMBERS
    run()
    {
    //not able to change numberShown
    }
}

Aucun commentaire:

Enregistrer un commentaire