0

I want to know others opinion about this approach that I use in a lot of projects, for example, I can show the last time that I use it:

public class DataStorageService {
    private enum Keys {
        USER_NAME("userName"), USER_ID("userId"), TELEPHONE("telephone"), EMAIL("email");

        private final String key;

        Keys(String key){
            this.key = key;
        }

        public String getKey(){
            return key;
        }

    }

    private SharedPreferences sharedPreferences;

    public DataStorageService(Context context){
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    }

    public void clearUserData(){
        setTelephone("");
        setUserEmail("");
        setUserName("");
    }

    public String getUserId(){
        return getString(Keys.USER_ID, "");
    }

    public void setUserId(String userName){
        putString(Keys.USER_ID, userName);
    }

    public String getUserName(){
        return getString(Keys.USER_NAME, "");
    }

    public void setUserName(String userName){
        putString(Keys.USER_NAME, userName);
    }

    public String getUserEmail(){
        return getString(Keys.EMAIL, "");
    }

    public void setUserEmail(String email){
        putString(Keys.EMAIL, email);
    }

    public String getTelephone(){
        return getString(Keys.TELEPHONE, "");
    }

    public void setTelephone(String telephone){
        putString(Keys.TELEPHONE, telephone);
    }

    private String getString(Keys key, String defaultValue){
        return getSharedPreferences().getString(key.getKey(), defaultValue);
    }

    private void putString(Keys key, String value){
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString(key.getKey(), value);
        editor.apply();
    }

    private Boolean getBoolean(Keys key, Boolean defaultValue){
        return getSharedPreferences().getBoolean(key.getKey(), defaultValue);
    }

    private void putBoolean(Keys key, Boolean value){
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean(key.getKey(), value);
        editor.apply();
    }

    private SharedPreferences getSharedPreferences(){
        return  sharedPreferences;
    }

}

How you can see, I have a "generic method" (putBoolen, getBoolean, putString, getString) to get a data of SharedPreferences, these methods are private because I do not want to be able the possibility of access to SharedPreferences (with this class). Then I create a new class for get access to concretes cases how telephone, userId, etc...

Is this approach a good approach os is better to put only the putBolean, getBoolean etc...?

With another example:

public class ActivityOpenerService {

    private Context context;

    public ActivityOpenerService(Context context){
        setContext(context);
    }

    private Context getContext(){
        return context;
    }

    public void launchAboutActivity(){
        launch(AboutActivity.class);
    }

    public void launchAssistanceFinishedActivity(){
        launch(AssistanceFinishedActivity.class);
    }

    public void launchLoginActivity(){
        launch(LoginActivity.class);
    }

    public void launchRegisterActivity(){
        launch(RegisterActivity.class);
    }

    public void launchProfileActivity(){
        launch(ProfileActivity.class);
    }

    private void launch(Class<? extends Activity> activityToLaunch){
        Intent intent = new Intent(getContext(), activityToLaunch);
        getContext().startActivity(intent);
    }

    public void setContext(Context context){
        this.context = context;
    }

}

Here you can see the same approach, here my "generic function" is launch, and I created another function for concrete cases.

The question is, what do you think about that, is a good approach, or is better call the generic functions in other places instead of concrete functions.

4

Short answer: yes, it is a good approach in most cases. It has a well-known name, it is called creating abstractions, and it is one of the fundamental basics of program design.

In this case, a class like DataStorageService provides functions with meaningful, specific names for certain actions, hiding away the implementation in terms of functions with unspecific names like putString or getString. This leads to a clear, easy-to-grasp API for a user of that class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.