层间松耦合
在开放源码界已经出现了大量的基于MVC的Web容器,但是这些容器都仅限于Web的范围 ,不涉及Web层次后端的连接,Spring作为一个整体性的框架,定义了一种Web层和后端业务层的连接方式, 这个思路仍然疏运图MVC的范畴,但耦合更松散,不依赖于具体的集成层次。
1. public class GoogleSearchController
2. implements Controller {
3.
4. private IGoogleSearchPort google;
5.
6. private String googleKey;
7.
8. public void setGoogle(IGoogleSearchPort google) {
9. this.google = google;
10. }
11.
12. public void setGoogleKey(String googleKey) {
13. this.googleKey = googleKey;
14. }
15.
16. public ModelAndView handleRequest(
17. HttpServletRequest request, HttpServletResponse response)
18. throws ServletException, IOException {
19. String query = request.getParameter("query");
20. GoogleSearchResult result =
21. // Google property definitions omitted...
22.
23. // Use google business object
24. google.doGoogleSearch(this.googleKey, query,start, maxResults, filter, r
25. estrict, safeSearch, lr, ie, oe);
26.
27. return new ModelAndView("googleResults", "result", result);
28. }
29. }
2. implements Controller {
3.
4. private IGoogleSearchPort google;
5.
6. private String googleKey;
7.
8. public void setGoogle(IGoogleSearchPort google) {
9. this.google = google;
10. }
11.
12. public void setGoogleKey(String googleKey) {
13. this.googleKey = googleKey;
14. }
15.
16. public ModelAndView handleRequest(
17. HttpServletRequest request, HttpServletResponse response)
18. throws ServletException, IOException {
19. String query = request.getParameter("query");
20. GoogleSearchResult result =
21. // Google property definitions omitted...
22.
23. // Use google business object
24. google.doGoogleSearch(this.googleKey, query,start, maxResults, filter, r
25. estrict, safeSearch, lr, ie, oe);
26.
27. return new ModelAndView("googleResults", "result", result);
28. }
29. }
回调函数是一个匿名类,其中也使用了模板方法,异常的处理都在父类中完成了。