我对dagger很陌生——我甚至还不知道它是否适用于我的应用程序
我有一个搜索页面,返回关于给定名人的最新消息。
我写了一个测试来验证当我们搜索一个受欢迎的名人时,结果是否出现在页面上。
page
有一个searchField
,它需要page
在其构造函数中,所以我用于我的测试的web驱动程序可以选择它。
名人搜索页面测试
public class CelebritySearchPageTest {
@Test
public void testSearchResultsForKevinBaconVerifyHisPopularity() {
CelebritySearchPage searchPage = new CelebritySearchPage();
searchPage.searchFor("Kevin Bacon");
Assert.assertTrue(searchPage.getNumberOfResults() > 9999999, "Verify that Kevin Bacon is still relevant");
}
}
名人搜索页面
public class CelebritySearchPage extends Page {
@Inject
@Named("search field")
TextField searchField;
public void searchFor(String text) {
searchField.setText(text);
// ...
}
public int getNumberOfResults() {
// ...
}
}
名人搜索页面模块
@Module(injects = CelebritySearchPage.class)
public class CelebritySearchPageModule {
@Provides
@Named("search field")
public TextField provideSearchField() {
return new TextField(/* How do I get the page? */, "#searchField");
}
}
public abstract class Page {
// ...
}
文本字段public class TextField {
protected Page page;
protected String selector;
public TextField(Page page, String selector) {
this.page = page;
this.selector = selector;
}
public void setText(String text) {
// ...
}
}
问题是page
需要searchField
,而searchField
需要page
。我如何克服这种循环依赖?
我不能在CelebritySearchPage内部初始化searchField
考虑一下:
CelebritySearchPage
public class CelebritySearchPage extends Page {
private final Lazy<TextField> searchField;
// always prefer constructor injection
// avoid @Named if possible, since the compiler cannot check the string
@Inject
CelebritySearchPage(@Named("search field") Lazy<TextField> searchField) {
this.searchField = searchField;
}
}
文本字段public class TextField {
protected final Lazy<Page> page;
protected final String selector;
@Inject TextField(Lazy<Page> page, String selector) {
this.page = page;
this.selector = selector;
}
/*
Lazy::get()
Return the underlying value, computing the value if necessary. All calls to the same Lazy instance will return the same result.
*/
}
我想一个Lazy也应该足够了。