官术网_书友最值得收藏!

Time for action – separating business classes from UI classes

Vaadin is mostly about UI. Vaadin's motto thinking of U and I is not in vain. I said mostly because you can additionally find a bunch of useful add-ons to deal with server-side technologies. Even Vaadin itself ships with some data components.

Note

There are lots of integration add-ons for Vaadin. For instance, you can integrate with technologies such as Groovy, Spring Framework, SQL, JPA, Hibernate, Lucene, Google App Engine Datastore, Quartz, JasperReports, JFreeChart, OpenLayers, Google Maps, OpenID, and even Twitter! All this with one hand tied behind your back. Visit https://vaadin.com/directory.

Most, if not all, applications have some kind of business logic. That is, something to do with the application's data. If you are an experienced developer you will agree with me that separating business logic from UI logic makes a lot of sense. For the "Time It" application, we will use two Java packages to separate business logic from UI logic:

  1. Create a new Vaadin project named time-it using your IDE.
  2. Delete the all the generated classes. We will add our own classes in a minute.
  3. Create the following package. Don't worry about the classes right now. Just create the Java packages timeit, timeit.biz, timeit.biz.test, and timeit.ui:
  4. Browse the book's source code and copy all the classes inside the biz package and paste them into your own biz package. Don't copy TimeItUI.java, we will work on that through the chapter.
  5. Create a new class with the TimeItUI name inside the ui package.
  6. Update your web.xml file to use TimeItUI as the starting application:
    <servlet>
      <servlet-name>Time It</servlet-name>
      <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
      <init-param>
        <description>
        Vaadin UI class to use</description>
        <param-name>UI</param-name>
          <param-value>timeit.ui.TimeItUI</param-value>
      </init-param>
    </servlet>
    <servlet-mapping>
      <servlet-name>Time It</servlet-name>
      <url-pattern>/*</url-pattern>
    </servlet-mapping>
  7. Let's start with an outline of our TimeItUI class. Modify your TimeItUI class to match something like this:
    public class TimeItUI extends UI {
      
      ... some variables ...
      
      @Override
      protected void init(VaadinRequest request) { ... }
    
      private void initCombo() { ... }
    
      private void initButton() { ... }
      
      private void initLayout() { ... }
    
      public boolean validate() { ... }
    
      public void runSelectedTest() { ... }
    
      private void showResults(Collection<String> results) { ... }
    
    }

What just happened?

Of course, the TimeItUI class won't compile. But now you have an outline with the structure of methods we will implement through the chapter. Just keep reading.

Most of the application is about business (the biz package) with only one class handling our UI logic. However, we will focus on the UI part. Business classes are not puzzling at all. We will use them as black boxes in our TimeItUI class (that's the reason we are just copying them).

Tip

I've faced several situations where I needed to modify UI components calling lots of business methods on classes that were completely unknown to me. Sometimes, I only had access to remote EJBs' interfaces code, there was no way to look at the actual implementation. When modifying UI layers, treat business logic as black boxes and use unit testing to prevent introducing regression bugs.

UI components as class members

In the example of the previous chapter, we created UI components as they were needed inside methods. This time, for the "Time It" application, we will declare most of the UI components as class members.

主站蜘蛛池模板: 彭州市| 隆安县| 遵义市| 武平县| 达州市| 寻甸| 尼勒克县| 延津县| 南部县| 柯坪县| 壶关县| 微山县| 平顶山市| 肃南| 宁津县| 滕州市| 龙山县| 宁明县| 陕西省| 江华| 会泽县| 惠水县| 东平县| 固安县| 通化市| 仁布县| 广宁县| 新昌县| 昆山市| 清新县| 高唐县| 丰镇市| 天门市| 方山县| 扶风县| 石嘴山市| 习水县| 仲巴县| 阜康市| 桦南县| 扶沟县|