简体   繁体   English

Android MVP - 分享偏好

[英]Android MVP - Share Preference

I started to learn MVP but I have a few questions related the SharedPreferences, as far as I know if I want to save a value in the sharedPreferences I need to pass this value to the presenter and the presenter calls the model to save the value, the same logic I would apply if I want to get or remove a value from the sharedPreference, but how is the best way to do that if I shouldn't pass the Context? 我开始学习MVP,但是我有一些与SharedPreferences相关的问题,据我所知,如果我想在sharedPreferences中保存一个值,我需要将这个值传递给演示者,并且演示者调用模型来保存值,如果我想从sharedPreference获取或删除一个值,我将应用相同的逻辑,但如果我不应该传递Context,那么最好的方法是什么?

I sae a few code and the people used to pass the Context in the constructor method direct to the Model, but I still don't think that's a good idea. 我说了几个代码,人们习惯将构造函数方法中的Context直接传递给Model,但我仍然认为这不是一个好主意。

Do you guys have any ideas? 你们有什么想法吗?

Thanks, Thales 谢谢,泰勒斯

Android specific imports should never exist in the Presenter if you want to keep it unit testable. 如果要保持单元可测试,则Presenter中永远不应存在Android特定导入。

What you can do is, make an abstraction layer above SharedPreferences let's call it Cache , it would be an interface with all the needed caching methods, you would then provide a concrete implementation of it using SharedPreferences . 你可以做的是,在SharedPreferences之上创建一个抽象层让我们称之为Cache ,它将是一个包含所有需要的缓存方法的接口,然后你将使用SharedPreferences提供它的具体实现。

Here is a quick illustration of the idea: 以下是该想法的快速说明:

interface Cache {
// Your caching methods
}

class CacheImpl implements Cache {

    private SharedPreferences sharedPrefs;

    public CacheImpl(Context context) {
        // Takes a context to init sharedPrefs.
    }

    // implements all of Cache's methods
}

Then you would pass a reference for that implementation to the Presenter's constructor (better yet using DI to inject it to your presenters constructor): 然后,您将该实现的引用传递给Presenter的构造函数(更好的是使用DI将其注入到演示者构造函数中):

Cache cache = new CacheImpl(myContext); // Naturally that would be an activity context
MyPresenter presenter = new MyPresenter(cache);

Then in your presenter you would receive that instance in the constructor: 然后在您的演示者中,您将在构造函数中收到该实例:

private Cache cache;

public MyPresenter(Cache cache) {
    this.cache = cache;
}

You can then use the cache variable without knowing about it's concrete implementation nor should you provide it a context. 然后,您可以在不知道具体实现的情况下使用缓存变量,也不应该为其提供上下文。

Create a Storage class Object inside View and pass the context inside Storage Class constructor. 在View中创建一个Storage类Object,并在Storage Class构造函数中传递上下文。

Then pass this storage class object in presenter (constructor) from View class. 然后从View类在presenter(构造函数)中传递此存储类对象。

Then whenever you need to save or get some data from your presenter - Then simply call the method of storage class from the object you have passed. 然后,只要您需要保存或从演示者那里获取一些数据 - 然后只需从您传递的对象中调用存储类的方法即可。

This way you will not need to send the context to your presenter. 这样您就不需要将上下文发送给演示者。

View class 查看课程

public class ViewClass extends ActionBarActivity {

    private MyPresenter presenter;
    private MyStorage storage;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        storage = new MyStorage(this);
        presenter = new MyPresenter(this,storage);
    }

}

MyStorage Class MyStorage类

public class MyStorage {

    private Context mContext;

    public MyStorage(Context context) {
        this.mContext = context;
    }

    public void saveData(String data){

    }

    public String getData(){
        return "";
    }
}

MyPresenter class MyPresenter类

public class MyPresenter {
    private final ViewClass mView;
    private final MyStorage mStorage;

    public MyPresenter(ViewClass viewClass, MyStorage storage) {
        this.mView = viewClass;
        this.mStorage = storage;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM