gpt4 book ai didi

android - ViewModelProvider.Factory 和 ViewModelProvider.NewInstanceFactory 有什么区别?

转载 作者:行者123 更新时间:2023-12-02 17:13:18 37 4
gpt4 key购买 nike

我目前正在将我的项目架构从 MVP 转换为 MVVM。当我在研究它时,我发现有些事情让我感到困惑:

在项目ioschedScheduleViewModelFactory.kt中,工厂实现ViewModelProvider.Factory:

class ScheduleViewModelFactory(
private val userEventRepository:DefaultSessionAndUserEventRepository
) : ViewModelProvider.Factory {

@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(ScheduleViewModel::class.java)) {
return ScheduleViewModel(LoadUserSessionsByDayUseCase(userEventRepository)) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}

codelab 的 Sunshine 项目的 DetailViewModelFactory.java 中,工厂扩展了ViewModelProvider.NewInstanceFactory:

public class DetailViewModelFactory extends ViewModelProvider.NewInstanceFactory {

private final SunshineRepository mRepository;
private final Date mDate;

public DetailViewModelFactory(SunshineRepository repository, Date date) {
this.mRepository = repository;
this.mDate = date;
}

@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
//noinspection unchecked
return (T) new DetailActivityViewModel(mRepository, mDate);
}
}

我想知道:

  1. ViewModelProvider.FactoryViewModelProvider.NewInstanceFactory 之间有什么区别?
  2. 为什么要像上面提到的代码一样使用它们?
  3. 使用它们的最佳实践/场景是什么?

最佳答案

What are the differences between ViewModelProvider.Factory and ViewModelProvider.NewInstanceFactory?

Why they are being used like the codes mentioned above?

基于ViewModelProvider文档:

public class ViewModelProvider {
/**
* Implementations of {@code Factory} interface are responsible to instantiate ViewModels.
*/
public interface Factory {
@NonNull
<T extends ViewModel> T create(@NonNull Class<T> modelClass);
(...)


/**
* Simple factory, which calls empty constructor on the given class.
*/
public static class NewInstanceFactory implements Factory {

@SuppressWarnings("ClassNewInstance")
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
//noinspection TryWithIdenticalCatches
try {
return modelClass.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("Cannot create an instance of " + modelClass, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Cannot create an instance of " + modelClass, e);
}
}
}
(...)
}

并采取geeksforgeeks考虑 newInstance() 的描述:

In general, new operator is used to create objects, but if we want to decide type of object to be created at runtime, there is no way we can use new operator. In this case, we have to use newInstance() method.

我假设NewInstanceFactoryFactory的实现,当我们想要创建不同类型的ViewModel时可以使用它。

<小时/>

另一方面,在google的android-architecture/todoapp中有:

public class ViewModelFactory extends ViewModelProvider.NewInstanceFactory {
(...)
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
if (modelClass.isAssignableFrom(StatisticsViewModel.class)) {
//noinspection unchecked
return (T) new StatisticsViewModel(mApplication, mTasksRepository);
} else if (modelClass.isAssignableFrom(TaskDetailViewModel.class)) {
//noinspection unchecked
return (T) new TaskDetailViewModel(mApplication, mTasksRepository);
} else if (modelClass.isAssignableFrom(AddEditTaskViewModel.class)) {
//noinspection unchecked
return (T) new AddEditTaskViewModel(mApplication, mTasksRepository);
} else if (modelClass.isAssignableFrom(TasksViewModel.class)) {
//noinspection unchecked
return (T) new TasksViewModel(mApplication, mTasksRepository);
}
throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
}
}

他们正在使用NewInstanceFactory,但是重写了create方法!据我了解,如果我们重写它,那么与使用常规的工厂

关于android - ViewModelProvider.Factory 和 ViewModelProvider.NewInstanceFactory 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52467801/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com