You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

advanced-architecture.asciidoc 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. ---
  2. title: Advanced Application Architectures
  3. order: 10
  4. layout: page
  5. ---
  6. [[advanced.architecture]]
  7. = Advanced Application Architectures
  8. In this section, we continue from the basic application architectures described
  9. in
  10. <<dummy/../../../framework/application/application-architecture#application.architecture,"Building
  11. the UI">> and discuss some of the more advanced patterns that are often used in
  12. Vaadin applications.
  13. [[advanced.architecture.layering]]
  14. == Layered Architectures
  15. Layered architectures, where each layer has a clearly distinct responsibility,
  16. are probably the most common architectures. Typically, applications follow at
  17. least a three-layer architecture:
  18. * User interface (or presentation) layer
  19. * Domain layer
  20. * Data store layer
  21. Such an architecture starts from a __domain model__, which defines the data
  22. model and the "business logic" of the application, typically as beans or POJOs.
  23. A user interface is built on top of the domain model, in our context with the
  24. Vaadin Framework. The Vaadin user interface could be bound directly to the data
  25. model through the Vaadin Data Model, described in
  26. <<dummy/../../../framework/datamodel/datamodel-overview.asciidoc#datamodel.overview,"Binding Components to Data">>.
  27. Beneath the domain model lies a data store, such as a relational database.
  28. The dependencies between the layers are restricted so that a higher layer may depend on a lower one, but never the other way around.
  29. [[figure.advanced.architecture.layering]]
  30. .Three-layer architecture
  31. image::img/three-layer-architecture-hi.png[width=80%]
  32. An __application layer__ (or __service layer__) is often distinguished from the
  33. domain layer, offering the domain logic as a service, which can be used by the
  34. user interface layer, as well as for other uses. In Java EE development,
  35. Enterprise JavaBeans (EJBs) are typically used for building this layer.
  36. An __infrastructure layer__ (or __data access layer__) is often distinguished
  37. from the data store layer, with a purpose to abstract the data store. For
  38. example, it could involve a persistence solution such as JPA and an EJB
  39. container. This layer becomes relevant with Vaadin when binding Vaadin
  40. components to data with the JPAContainer, as described in
  41. <<dummy/../../../framework/jpacontainer/jpacontainer-overview.asciidoc#jpacontainer.overview,"Vaadin
  42. JPAContainer">>.
  43. [[advanced.architecture.mvp]]
  44. == Model-View-Presenter Pattern
  45. The Model-View-Presenter (MVP) pattern is one of the most common patterns in
  46. developing large applications with Vaadin. It is similar to the older
  47. Model-View-Controller (MVC) pattern, which is not as meaningful in Vaadin
  48. development. Instead of an implementation-aware controller, there is an
  49. implementation-agnostic presenter that operates the view through an interface.
  50. The view does not interact directly with the model. This isolates the view
  51. implementation better than in MVC and allows easier unit testing of the
  52. presenter and model.
  53. [[figure.advanced.architecture.mvp]]
  54. .Model-View-Presenter pattern
  55. image::img/mvp-pattern-hi.png[width=60%]
  56. <<figure.advanced.architecture.mvp>> illustrates the MVP pattern with a simple
  57. calculator. The domain model is realized in the [classname]#Calculator# class,
  58. which includes a data model and some model logic operations. The
  59. [classname]#CalculatorViewImpl# is a Vaadin implementation of the view, defined
  60. in the [interfacename]#CalculatorView# interface. The
  61. [classname]#CalculatorPresenter# handles the user interface logic. User
  62. interaction events received in the view are translated into
  63. implementation-independent events for the presenter to handle (the view
  64. implementation could also just call the presenter).
  65. Let us first look how the model and view are bound together by the presenter in
  66. the following example:
  67. [source, java]
  68. ----
  69. // Create the model and the Vaadin view implementation
  70. CalculatorModel model = new CalculatorModel();
  71. CalculatorViewImpl view = new CalculatorViewImpl();
  72. // The presenter binds the model and view together
  73. new CalculatorPresenter(model, view);
  74. // The view implementation is a Vaadin component
  75. layout.addComponent(view);
  76. ----
  77. You could add the view anywhere in a Vaadin application, as it is a composite
  78. component.
  79. [[advanced.architecture.mvp.model]]
  80. === The Model
  81. Our business model is quite simple, with one value and a number of operations
  82. for manipulating it.
  83. [source, java]
  84. ----
  85. /** The model **/
  86. class CalculatorModel {
  87. private double value = 0.0;
  88. public void clear() {
  89. value = 0.0;
  90. }
  91. public void add(double arg) {
  92. value += arg;
  93. }
  94. public void multiply(double arg) {
  95. value *= arg;
  96. }
  97. public void divide(double arg) {
  98. if (arg != 0.0)
  99. value /= arg;
  100. }
  101. public double getValue() {
  102. return value;
  103. }
  104. public void setValue(double value) {
  105. this.value = value;
  106. }
  107. }
  108. ----
  109. [[advanced.architecture.mvp.view]]
  110. === The View
  111. The purpose of the view in MVP is to display data and receive user interaction.
  112. It relays the user interaction to the presenter in an fashion that is
  113. independent of the view implementation, that is, no Vaadin events. It is defined
  114. as a UI framework interface that can have multiple implementations.
  115. [source, java]
  116. ----
  117. interface CalculatorView {
  118. public void setDisplay(double value);
  119. interface CalculatorViewListener {
  120. void buttonClick(char operation);
  121. }
  122. public void addListener(CalculatorViewListener listener);
  123. }
  124. ----
  125. The are design alternatives for the view. It could receive the listener in its
  126. constructor, or it could just know the presenter. Here, we forward button clicks
  127. as an implementation-independent event.
  128. As we are using Vaadin, we make a Vaadin implementation of the interface as
  129. follows:
  130. [source, java]
  131. ----
  132. class CalculatorViewImpl extends CustomComponent
  133. implements CalculatorView, ClickListener {
  134. private Label display = new Label("0.0");
  135. public CalculatorViewImpl() {
  136. GridLayout layout = new GridLayout(4, 5);
  137. // Create a result label that spans over all
  138. // the 4 columns in the first row
  139. layout.addComponent(display, 0, 0, 3, 0);
  140. // The operations for the calculator in the order
  141. // they appear on the screen (left to right, top
  142. // to bottom)
  143. String[] operations = new String[] {
  144. "7", "8", "9", "/", "4", "5", "6",
  145. "*", "1", "2", "3", "-", "0", "=", "C", "+" };
  146. // Add buttons and have them send click events
  147. // to this class
  148. for (String caption: operations)
  149. layout.addComponent(new Button(caption, this));
  150. setCompositionRoot(layout);
  151. }
  152. public void setDisplay(double value) {
  153. display.setValue(Double.toString(value));
  154. }
  155. /* Only the presenter registers one listener... */
  156. List<CalculatorViewListener> listeners =
  157. new ArrayList<CalculatorViewListener>();
  158. public void addListener(CalculatorViewListener listener) {
  159. listeners.add(listener);
  160. }
  161. /** Relay button clicks to the presenter with an
  162. * implementation-independent event */
  163. @Override
  164. public void buttonClick(ClickEvent event) {
  165. for (CalculatorViewListener listener: listeners)
  166. listener.buttonClick(event.getButton()
  167. .getCaption().charAt(0));
  168. }
  169. }
  170. ----
  171. [[advanced.architecture.mvp.presenter]]
  172. === The Presenter
  173. The presenter in MVP is a middle-man that handles all user interaction logic,
  174. but in an implementation-independent way, so that it doesn't actually know
  175. anything about Vaadin. It shows data in the view and receives user interaction
  176. back from it.
  177. [source, java]
  178. ----
  179. class CalculatorPresenter
  180. implements CalculatorView.CalculatorViewListener {
  181. CalculatorModel model;
  182. CalculatorView view;
  183. private double current = 0.0;
  184. private char lastOperationRequested = 'C';
  185. public CalculatorPresenter(CalculatorModel model,
  186. CalculatorView view) {
  187. this.model = model;
  188. this.view = view;
  189. view.setDisplay(current);
  190. view.addListener(this);
  191. }
  192. @Override
  193. public void buttonClick(char operation) {
  194. // Handle digit input
  195. if ('0' <= operation && operation <= '9') {
  196. current = current * 10
  197. + Double.parseDouble("" + operation);
  198. view.setDisplay(current);
  199. return;
  200. }
  201. // Execute the previously input operation
  202. switch (lastOperationRequested) {
  203. case '+':
  204. model.add(current);
  205. break;
  206. case '-':
  207. model.add(-current);
  208. break;
  209. case '/':
  210. model.divide(current);
  211. break;
  212. case '*':
  213. model.multiply(current);
  214. break;
  215. case 'C':
  216. model.setValue(current);
  217. break;
  218. } // '=' is implicit
  219. lastOperationRequested = operation;
  220. current = 0.0;
  221. if (operation == 'C')
  222. model.clear();
  223. view.setDisplay(model.getValue());
  224. }
  225. }
  226. ----
  227. In the above example, we held some state information in the presenter.
  228. Alternatively, we could have had an intermediate controller between the
  229. presenter and the model to handle the low-level button logic.