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 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. <<../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. <<../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. [[advanced.architecture.mvp]]
  37. == Model-View-Presenter Pattern
  38. The Model-View-Presenter (MVP) pattern is one of the most common patterns in
  39. developing large applications with Vaadin. It is similar to the older
  40. Model-View-Controller (MVC) pattern, which is not as meaningful in Vaadin
  41. development. Instead of an implementation-aware controller, there is an
  42. implementation-agnostic presenter that operates the view through an interface.
  43. The view does not interact directly with the model. This isolates the view
  44. implementation better than in MVC and allows easier unit testing of the
  45. presenter and model.
  46. [[figure.advanced.architecture.mvp]]
  47. .Model-View-Presenter pattern
  48. image::img/mvp-pattern-hi.png[width=60%]
  49. <<figure.advanced.architecture.mvp>> illustrates the MVP pattern with a simple
  50. calculator. The domain model is realized in the [classname]#Calculator# class,
  51. which includes a data model and some model logic operations. The
  52. [classname]#CalculatorViewImpl# is a Vaadin implementation of the view, defined
  53. in the [interfacename]#CalculatorView# interface. The
  54. [classname]#CalculatorPresenter# handles the user interface logic. User
  55. interaction events received in the view are translated into
  56. implementation-independent events for the presenter to handle (the view
  57. implementation could also just call the presenter).
  58. Let us first look how the model and view are bound together by the presenter in
  59. the following example:
  60. [source, java]
  61. ----
  62. // Create the model and the Vaadin view implementation
  63. CalculatorModel model = new CalculatorModel();
  64. CalculatorViewImpl view = new CalculatorViewImpl();
  65. // The presenter binds the model and view together
  66. new CalculatorPresenter(model, view);
  67. // The view implementation is a Vaadin component
  68. layout.addComponent(view);
  69. ----
  70. You could add the view anywhere in a Vaadin application, as it is a composite
  71. component.
  72. [[advanced.architecture.mvp.model]]
  73. === The Model
  74. Our business model is quite simple, with one value and a number of operations
  75. for manipulating it.
  76. [source, java]
  77. ----
  78. /** The model **/
  79. class CalculatorModel {
  80. private double value = 0.0;
  81. public void clear() {
  82. value = 0.0;
  83. }
  84. public void add(double arg) {
  85. value += arg;
  86. }
  87. public void multiply(double arg) {
  88. value *= arg;
  89. }
  90. public void divide(double arg) {
  91. if (arg != 0.0)
  92. value /= arg;
  93. }
  94. public double getValue() {
  95. return value;
  96. }
  97. public void setValue(double value) {
  98. this.value = value;
  99. }
  100. }
  101. ----
  102. [[advanced.architecture.mvp.view]]
  103. === The View
  104. The purpose of the view in MVP is to display data and receive user interaction.
  105. It relays the user interaction to the presenter in an fashion that is
  106. independent of the view implementation, that is, no Vaadin events. It is defined
  107. as a UI framework interface that can have multiple implementations.
  108. [source, java]
  109. ----
  110. interface CalculatorView {
  111. public void setDisplay(double value);
  112. interface CalculatorViewListener {
  113. void buttonClick(char operation);
  114. }
  115. public void addListener(CalculatorViewListener listener);
  116. }
  117. ----
  118. The are design alternatives for the view. It could receive the listener in its
  119. constructor, or it could just know the presenter. Here, we forward button clicks
  120. as an implementation-independent event.
  121. As we are using Vaadin, we make a Vaadin implementation of the interface as
  122. follows:
  123. [source, java]
  124. ----
  125. class CalculatorViewImpl extends CustomComponent
  126. implements CalculatorView, ClickListener {
  127. private Label display = new Label("0.0");
  128. public CalculatorViewImpl() {
  129. GridLayout layout = new GridLayout(4, 5);
  130. // Create a result label that spans over all
  131. // the 4 columns in the first row
  132. layout.addComponent(display, 0, 0, 3, 0);
  133. // The operations for the calculator in the order
  134. // they appear on the screen (left to right, top
  135. // to bottom)
  136. String[] operations = new String[] {
  137. "7", "8", "9", "/", "4", "5", "6",
  138. "*", "1", "2", "3", "-", "0", "=", "C", "+" };
  139. // Add buttons and have them send click events
  140. // to this class
  141. for (String caption: operations)
  142. layout.addComponent(new Button(caption, this));
  143. setCompositionRoot(layout);
  144. }
  145. public void setDisplay(double value) {
  146. display.setValue(Double.toString(value));
  147. }
  148. /* Only the presenter registers one listener... */
  149. List<CalculatorViewListener> listeners =
  150. new ArrayList<CalculatorViewListener>();
  151. public void addListener(CalculatorViewListener listener) {
  152. listeners.add(listener);
  153. }
  154. /** Relay button clicks to the presenter with an
  155. * implementation-independent event */
  156. @Override
  157. public void buttonClick(ClickEvent event) {
  158. for (CalculatorViewListener listener: listeners)
  159. listener.buttonClick(event.getButton()
  160. .getCaption().charAt(0));
  161. }
  162. }
  163. ----
  164. [[advanced.architecture.mvp.presenter]]
  165. === The Presenter
  166. The presenter in MVP is a middle-man that handles all user interaction logic,
  167. but in an implementation-independent way, so that it doesn't actually know
  168. anything about Vaadin. It shows data in the view and receives user interaction
  169. back from it.
  170. [source, java]
  171. ----
  172. class CalculatorPresenter
  173. implements CalculatorView.CalculatorViewListener {
  174. CalculatorModel model;
  175. CalculatorView view;
  176. private double current = 0.0;
  177. private char lastOperationRequested = 'C';
  178. public CalculatorPresenter(CalculatorModel model,
  179. CalculatorView view) {
  180. this.model = model;
  181. this.view = view;
  182. view.setDisplay(current);
  183. view.addListener(this);
  184. }
  185. @Override
  186. public void buttonClick(char operation) {
  187. // Handle digit input
  188. if ('0' <= operation && operation <= '9') {
  189. current = current * 10
  190. + Double.parseDouble("" + operation);
  191. view.setDisplay(current);
  192. return;
  193. }
  194. // Execute the previously input operation
  195. switch (lastOperationRequested) {
  196. case '+':
  197. model.add(current);
  198. break;
  199. case '-':
  200. model.add(-current);
  201. break;
  202. case '/':
  203. model.divide(current);
  204. break;
  205. case '*':
  206. model.multiply(current);
  207. break;
  208. case 'C':
  209. model.setValue(current);
  210. break;
  211. } // '=' is implicit
  212. lastOperationRequested = operation;
  213. current = 0.0;
  214. if (operation == 'C')
  215. model.clear();
  216. view.setDisplay(model.getValue());
  217. }
  218. }
  219. ----
  220. In the above example, we held some state information in the presenter.
  221. Alternatively, we could have had an intermediate controller between the
  222. presenter and the model to handle the low-level button logic.