-
Structuring the application
As applications get larger and more complex it is beneficial to organise the code into packages that have different purposes.
This is called Separation of Concerns (SoC).
Separate out each concern (or responsibility) in such a way that each component addresses a specific concern (or responsibility) in a modular fashion.
-
Separated Presentation Design Pattern
- Develop separate layers for:
- presentation code and
- domain code.
- The domain code is unaware of the presentation code.
- See: 🔗 https://martinfowler.com/eaaDev/SeparatedPresentation.html
- How to think about it
- Divide the application code (data and behaviour) in a system into two logical modules for:
- presentation code and
- other (domain model and possibly other) code.
- Presentation code:
- Manipulates the GUI “widgets and structures” in a rich client application,
- the HTTP headers and HTML code in a web application, or
- the print statements (or command line arguments) in a command line application.
- Use logical packaging mechanisms (Java packages, .NET namespaces) to separate the layers.
-
Visibility
- The presentation is able to call the domain.
- But not vice-versa.
- The domain should be unaware of how it is presented.
- The domain should be unaware of how it is presented.
- Such an architecture supports using multiple presentations with the same domain code.
- The domain should not call the presentation but it can notify the presentation if any changes occur.
-
One Step Further: Model-View-Controller
- MVC is a popular and widely-used architecture for developing web applications. It is supported by many technologies (Java, Spring, ASP.NET, Php).
- The domain element is referred to as the model. (Model objects exist independently of the (G)UI.)
- Model is responsible for managing and maintaining the state of data.
- The data is often made persistent in a database.
- The presentation part of MVC is made of the two remaining elements: view and controller.
- The controller takes the user's input “and figure[s] out what to do with it”. This often means to update the view and model.
-
MVC with Servlet
- The domain objects (POJOs) are the model.
- The servlet class (extends HttpServlet) is the controller.
- The servlet passes relevant information from the user request to the model.
- The servlet takes the results and passes them on to the view.
- The “output”, i.e. the HTML page that is returned to the client is the view.
- User packages to structure code.
-
Advantages
- SoC, in particular:
- Re-usability of code
- Code is easier to maintain
- Disadvantages
- The MVC pattern does not mandate you to have one Controller per View.
- In a complex application MVC typically creates many Views and Controllers.
- It is challenging to design the application in a way that scales well.
- Flux is an alternative design pattern.
-
Components in Flux
1. View: the user interface. The user clicks a button, e.g. AddNewItem.
2. Action: raised by the View when the user clicks a button.
3. Dispatcher: Holds the data store and propagates the action from View to Store. Notifies Store of the Action.
4. Store: Contains the Data and is registered with Dispatcher. It receives the update event from Dispatcher and will respond to it. The response is another change event to View.
5. View: Makes changes.
-
Apache Maven
- Is a software development tool that can manage a project's build, reporting and documentation from a central piece of information.
- Started as an attempt to simplify the build processes
- A standard way to build the projects, a clear definition of what the project consisted of, an easy way to publish project information and a way to share JARs across several projects.
- A tool that can now be used for building and managing any Java-based project.
-
Benefits of Maven
- Can manage a project's build, reporting and documentation from a central piece of information.
- Started as an attempt to simplify the build processes
- A standard way to build the projects, a clear definition of what the project consisted of, an easy way to publish project information and a way to share JARs across several projects.
- A tool that can now be used for building and managing any Java-based project.
- Allows transparent migration to new features.
- Maven provides an infrastructure that makes it easy for Maven clients to update their installations because these are built from the information in the POM.
- Changes made to Maven itself are transparent to clients.
- Installation of new or updated plugins from third parties or Maven itself are transparent to clients (just load the new library).
- Provides quality project information.
- Gathers current principles for best practices development, and makes it easy to guide a project in that direction.
-
A Typical Maven Directory Layout
- If users are familiar with one standard layout they will find it easy to understand other projects structured in this way.
- Once a user or developer is familiar with the details of a Maven project structure these can be applied to other projects.
- The default (standard) settings can be re-configured via the project descriptor.
- A typical Maven project structure:
- The src directory is the root directory of source code and test code.
- The main directory is the root directory for source code related to the application itself, not test code.
- The test directory contains the test source code.
- The java directories under main and test contains the Java code for the application itself which is under main and the Java code for the tests which is under test.
- The resources directory contains the resources needed by your project.
- The target directory is created by Maven. It contains all the compiled classes, JAR files etc.
- When executing the mvn clean command, Maven would clean the target directory.
- The webapp directory contains Java web application, if the project is a web application.
- The webapp directory is the root directory of the web application.
- The webapp directory contains the WEB-INF directory.
- The WEB-INF directory typically contains configuration documents.
- The WEB-INF node is not part of the public document tree of the application.
- No file contained in the WEB-INF directory may be served directly to a client by the container.
- The contents of the WEB-INF directory are visible to servlet code (e.g. using getResource() , getResourceAsStreammethod())
- The root also contains:
- LICENSE.txt – the project’s license document
- NOTICE.txt -Notices and attributions required by libraries that the project depends on
- README.txt – The project’s readme file
- Note:
- Source code is located in the src folder.
- During build we compile source code into byte code that is copied into the target folder.
- Resources are also copied into the target folder (the structure provided in src is mirrored under target).
- The project runs out of the target directory!
-
Project Object Model (POM)
- A Project Object Model or POM is an XML file that contains information about the project and configuration details used by Maven to build the project.
- It contains default values that are useful for most projects.
- If you follow the directory structure, you do not need to specify the directories of your source code, test code, resource files, etc. in your POM file. Otherwise specify the directories of your source code, test code, resource files, etc. in your POM file.
- Typical configurations specified in the POM are:
- modelVersion - should be set to 4.0.0,
- groupId - the id of the project's group, similar to domain,
- artifactId - the id of the artifact (project),
- version - the version of the artifact under the specified group,
- the project dependencies,
- the plugin lists,
- the build profiles,
- project version,
- description,
- mailing lists.
-
POM Dependencies
- Typically a project depends on other components to build and run, e.g. third-party libraries or third party components.
- Maven can manage this task when configured appropriately.
- Maven downloads and links the dependencies for you on compilation (and other goals that require them).
- Maven brings in the dependencies of those dependencies (transitive dependencies), allowing your list to focus solely on the dependencies your project requires.
- Example
- POM Dependency for the Jetty Server
- The mark-up is usually provided by the supplier of the library and can be copied from the Maven Central Repository
- Often we add dependencies when we work with third-party libraries
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>9.4.0.v20161208</version> </dependency>