Defining an API for an application's main screen

In order to explore and learn about API design in web development with Vaadin, let's assume we want the main screen to be a general purpose component not intended to be used only in this demo application. For this reason, we need to provide the component in a separate JAR file. Start by creating a new Maven module inside the chapter-02 project using the maven-archetype-simple archetype as follows:

cd chapter-02
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart

Use the following properties when prompted:

  • groupId: packt.vaadin.datacentric.chapter02
  • artifactId: api
  • version: 1.0-SNAPSHOT
  • package: packt.vaadin.datacentric.chapter02.api

Check that the new api module is listed in the chapter-02/pom.xml file:

<project ...>
...
<modules>
<module>api</module>
</modules>
</project>

Clean up as desired and add the Vaadin BOM and the vaadin-server dependency. You can also delete the generated App and AppTest classes. You will also need to configure Java 8 using properties, similar to how it was done in the previous chapter.

You can find the full pom.xml file in the Data-centric-Applications-with-Vaadin-8\chapter-02\api Maven project of the source code that accompanies this book.

The API should allow developers to create additional concrete main screen implementations with similar functionality. Abstracting this functionality can be done by defining a Java interface like the following:

public interface ApplicationLayout extends Component {

void addHeaderComponent(Component component);

void addWorkingAreaComponent(WorkingAreaComponent
component);

Collection<Component> getHeaderComponents();

Collection< WorkingAreaComponent> getWorkingAreaComponents();

void addMenuOption(MenuOption menuOption,
SerializableConsumer<MenuOption> clickListener);

}
The ApplicationLayout interface and related classes are located in the Data-centric-Applications-with-Vaadin-8/chapter-02/api Maven project of the source code that accompanies this book.

This interface extends Component, so any concrete implementation can be used as a regular UI component and added into any Vaadin component container, such as VerticalLayout, for instance. Concrete implementations will extend Composite, as will be shown later.