公共类容器{
private static Container instance;
private FavoriteAdder favoriteAdder;
private FavoriteRepo favoriteRepo;
private FavoritesGetter favoritesGetter;
private FavoriteRemover favoritesRemover;
private Container() {}
public static Container getInstance() {
if (instance == null) {
instance = new Container();
}
return instance;
}
public FavoriteAdder getFavoriteAdder() {
if (favoriteAdder == null) {
favoriteAdder = new FavoriteAdder(getFavoriteRepo());
}
return favoriteAdder;
}
public FavoritesGetter getFavoritesGetter() {
if (favoritesGetter == null) {
favoritesGetter = new FavoritesGetter(getFavoriteRepo());
}
return favoritesGetter;
}
public FavoriteRemover getFavoritesRemover() {
if (favoritesRemover == null) {
favoritesRemover = new FavoriteRemover(getFavoriteRepo());
}
return favoritesRemover;
}
public FavoriteRepo getFavoriteRepo() {
if (favoriteRepo == null) {
CachingFavoriteRepo inMemoryRepo = new InMemoryFavoriteRepo();
FavoriteRepo remoteRepo = new SlowFavoriteRepoWrapper(new MockRemoteFavoriteRepo(), 1000);
favoriteRepo = new FavoriteReadThroughCache(inMemoryRepo, remoteRepo);
}
return favoriteRepo;
}
}
然后在我们可以执行此操作的活动中使用它:
public class FavoriteActivity extends ActionBarActivity {
private FavoriteAdder favoriteAdder = Container.getInstance().getFavoriteAdder();
private FavoritesGetter favoritesGetter = Container.getInstance().getFavoritesGetter();
private FavoriteRemover favoritesRemover = Container.getInstance().getFavoritesRemover();
private FavoriteCrudPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
presenter = new FavoriteCrudPresenter(favoriteAdder, favoritesGetter, favoritesRemover, Schedulers.io(), AndroidSchedulers.mainThread(), backgroundExecutor, foregroundExecutor);
//...
}}
使用匕首似乎更容易,我想知道匕首在这方面有什么好处? 为什么我们甚至打扰匕首我想它更容易让匕首中的单身人士成为一种情景。