Java Android MVP-股票优先权

Java Android MVP-股票优先权,java,android,sharedpreferences,mvp,Java,Android,Sharedpreferences,Mvp,我开始学习MVP,但我有一些关于SharedReference的问题,据我所知,如果我想在SharedReference中保存一个值,我需要将该值传递给演示者,演示者调用模型来保存该值,如果我想从SharedReference中获取或删除一个值,我将应用相同的逻辑,但是如果我不通过上下文,那么最好的方法是什么呢 我使用了一些代码,人们习惯于将构造函数方法中的上下文直接传递给模型,但我仍然认为这不是一个好主意 你们有什么想法吗 谢谢, 泰利斯如果您希望保持演示者的单元可测试性,则演示者中不应存在特

我开始学习MVP,但我有一些关于SharedReference的问题,据我所知,如果我想在SharedReference中保存一个值,我需要将该值传递给演示者,演示者调用模型来保存该值,如果我想从SharedReference中获取或删除一个值,我将应用相同的逻辑,但是如果我不通过上下文,那么最好的方法是什么呢

我使用了一些代码,人们习惯于将构造函数方法中的上下文直接传递给模型,但我仍然认为这不是一个好主意

你们有什么想法吗

谢谢,
泰利斯

如果您希望保持演示者的单元可测试性,则演示者中不应存在特定于Android的导入

您可以做的是,在
SharedReferences
上面创建一个抽象层,我们称之为
Cache
,它将是一个包含所有所需缓存方法的接口,然后您将使用
SharedReferences
提供它的具体实现

以下是该想法的快速说明:

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
}
然后将该实现的引用传递给Presenter的构造函数(最好使用DI将其注入Presenter构造函数):

然后在演示者中,您将在构造函数中接收该实例:

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

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

然后,您可以在不知道缓存变量的具体实现的情况下使用它,也不应该为它提供上下文。

在视图中创建一个存储类对象,并在存储类构造函数中传递上下文

然后从视图类在presenter(构造函数)中传递此存储类对象

然后,每当您需要保存或从演示者获取一些数据时,只需从您传递的对象调用存储类的方法即可

这样,您就不需要将上下文发送给演示者

查看类

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);
    }

}
public class MyStorage {

    private Context mContext;

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

    public void saveData(String data){

    }

    public String getData(){
        return "";
    }
}
public class MyPresenter {
    private final ViewClass mView;
    private final MyStorage mStorage;

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

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);
    }

}
public class MyStorage {

    private Context mContext;

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

    public void saveData(String data){

    }

    public String getData(){
        return "";
    }
}
public class MyPresenter {
    private final ViewClass mView;
    private final MyStorage mStorage;

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

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);
    }

}
public class MyStorage {

    private Context mContext;

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

    public void saveData(String data){

    }

    public String getData(){
        return "";
    }
}
public class MyPresenter {
    private final ViewClass mView;
    private final MyStorage mStorage;

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

您可以单独创建一个共享的pref类,并将该类实例设置为公共。之后,您可以通过将当前类上下文传递给pref类来访问任何类中的实例。关于MVP架构的详细信息,我建议您浏览Google引导的架构,它可以让您清楚了解MVP。这与艾哈迈德·阿什拉夫·贾马尔的回答基本相同。但仍然应该是可以接受的。