Skip to content

Implement View Helper pattern #3278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
<sonar.projectName>Java Design Patterns</sonar.projectName>
</properties>
<modules>

<module>abstract-document</module>
<module>abstract-factory</module>
<module>active-object</module>
Expand Down Expand Up @@ -241,11 +240,11 @@
<module>update-method</module>
<module>value-object</module>
<module>version-number</module>
<module>view-helper</module>
<module>virtual-proxy</module>
<module>visitor</module>
<module>backpressure</module>
<module>actor-model</module>

<module>backpressure</module>
<module>actor-model</module>
</modules>
<repositories>
<repository>
Expand Down
98 changes: 98 additions & 0 deletions view-helper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
title: "View Helper Pattern in Java: Simplifying Presentation Logic in MVC Applications"
shortTitle: View Helper
description: "Discover the View Helper Design Pattern in Java, a powerful technique for separating view-related logic from business logic in MVC-based web applications. This pattern enhances maintainability, reusability, and testability by delegating complex UI operations to reusable helper components. Ideal for developers aiming to keep views clean and focused on presentation."
category: Architectural
language: en
tag:
- Architecture
- Presentation
- Decoupling
- Code reuse
---

## Intent of View Helper Design Pattern
The View Helper Design Pattern separates presentation logic from the view by delegating complex UI tasks — like formatting or conditional display — to reusable helper components. This keeps views clean, promotes reuse, and aligns with the MVC principle of separating concerns between the view and the business logic.

## Detailed Explanation with Real‑World Analogy
Real‑world example
> Imagine you're putting together a slideshow for a business presentation. You focus on arranging the slides, choosing the layout, and telling the story. But for tasks like resizing images, formatting charts, or converting data into visual form, you use tools or templates that automate those parts.
>
> In this analogy, you are the view, and the tools/templates are the helpers. They handle the heavy lifting behind the scenes so you can concentrate on the presentation. Similarly, in the View Helper pattern, the view delegates logic-heavy tasks—such as formatting or conditionally displaying data—to helper classes, keeping the view layer clean and presentation-focused.

### In plain words
> The View Helper pattern is about keeping your UI code clean by moving any logic—like formatting, calculations, or decision-making—into separate helper classes. Instead of stuffing all the logic into the HTML or template files, you delegate it to helpers, so the view just focuses on showing the final result.

### Sequence diagram
![Sequence diagram for View Helper](etc/view-helper-sequence-diagram.png)

## Programmatic Example of View Helper Pattern in Java
Raw domain object
```java
public record Product(String name, BigDecimal price, LocalDate releaseDate, boolean discounted) {}
```

View model object for display
```java
public record ProductViewModel(String name, String price, String releasedDate) {}
```

View Helper formats data for display
```java
class ProductViewHelper implements ViewHelper<Product, ProductViewModel> {

private static final String DISCOUNT_TAG = " (ON SALE)";

public ProductViewModel prepare(Product product) {
var displayName = product.name() + (product.discounted() ? DISCOUNT_TAG : "");
var priceWithCurrency = NumberFormat.getCurrencyInstance(US).format(product.price());
var formattedDate = product.releaseDate().format(ISO_DATE);

return new ProductViewModel(displayName, priceWithCurrency, formattedDate);
}
}
```

View renders the formatted data
```java
public class ConsoleProductView implements View<ProductViewModel> {

@Override
public void render(ProductViewModel productViewModel) {
LOGGER.info(productViewModel.toString());
}
}
```
The `App.java` class simulates how the View Helper pattern works in a real application. It starts with a raw `Product` object containing unformatted data.
Then it:
1. Initializes a helper (`ProductViewHelper`) to format the product data for display.
1. Creates a view (`ConsoleProductView`) to render the formatted data.
1. Uses a controller (`ProductController`) to coordinate the flow between raw data, helper logic, and view rendering.

Finally, it simulates a user request by passing the product to the controller, which prepares the view model using the helper and displays it using the view. This demonstrates a clean separation between data, presentation logic, and rendering.

## When to Use the View Helper Pattern in Java
Use the View Helper pattern when your view layer starts containing logic such as formatting data, applying conditional styles, or transforming domain objects for display. It's especially useful in MVC architectures where you want to keep views clean and focused on rendering, while delegating non-trivial presentation logic to reusable helper classes. This pattern helps improve maintainability, testability, and separation of concerns in your application's UI layer.

## Real‑World Uses of View Helper Pattern in Java
The View Helper pattern is widely used in web frameworks that follow the MVC architecture. In Java-based web applications (e.g., JSP, Spring MVC), it's common to use helper classes or utility methods to format dates, currencies, or apply conditional logic before rendering views. Technologies like Thymeleaf or JSF often rely on custom tags or expression helpers to achieve the same effect.

## Benefits and Trade‑offs
Benefits:
* Separation of concerns: Keeps view templates clean by moving logic into dedicated helpers.
* Reusability: Common formatting and display logic can be reused across multiple views.
* Improved maintainability: Easier to update presentation logic without touching the view.
* Testability: Helpers can be unit tested independently from the UI layer.

Trade‑offs:
* Added complexity: Introduces extra classes, which may feel unnecessary for very simple views.
* Overuse risk: Excessive use of helpers can spread logic thinly across many files, making it harder to trace behavior.
* Tight coupling risk: If not designed carefully, helpers can become tightly coupled to specific views or data formats.

## Related Java Design Patterns
* [Model-View-Controller (MVC)](https://java-design-patterns.com/patterns/model-view-controller/): View Helper supports the View layer in MVC by offloading logic from the view to helper classes.
* [Template Method](https://java-design-patterns.com/patterns/template-method/): Can structure the steps of rendering or data transformation, with helpers handling specific formatting tasks.
* [Data Transfer Object (DTO)](https://java-design-patterns.com/patterns/data-transfer-object/): Often used alongside View Helper when transferring raw data that needs formatting before being displayed.

## References & Credits
* [Core J2EE Patterns: View Helper.](https://www.oracle.com/java/technologies/viewhelper.html)
Binary file added view-helper/etc/view-helper-sequence-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions view-helper/etc/view-helper-sequence-diagram.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@startuml
actor Client
participant Controller
participant ViewHelper
participant View
participant Product
participant ProductViewModel

Client -> Controller : handle(product)
Controller -> ViewHelper : prepare(product)
ViewHelper -> Product : access data
ViewHelper -> ProductViewModel : return formatted view model
Controller -> View : render(viewModel)
View -> Console : display output
@enduml
Binary file added view-helper/etc/view-helper.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions view-helper/etc/view-helper.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@startuml
package com.iluwatar.viewhelper {
interface View<T> {
+render(T model)
}

interface ViewHelper<S, T> {
+prepare(S source): T
}

class Product {
-name: String
-price: BigDecimal
-releaseDate: LocalDate
-discounted: boolean
}

class ProductViewModel {
-name: String
-price: String
-releasedDate: String
}

class ProductViewHelper {
+prepare(Product): ProductViewModel
}

class ConsoleProductView {
+render(ProductViewModel)
}

class ProductController {
-helper: ViewHelper<Product, ProductViewModel>
-view: View<ProductViewModel>
+handle(Product)
}
}
Product --> ProductViewHelper
ProductViewHelper ..|> ViewHelper
ConsoleProductView ..|> View
ProductViewHelper --> ProductViewModel
ProductController --> ProductViewHelper
ProductController --> ConsoleProductView

@enduml
70 changes: 70 additions & 0 deletions view-helper/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).

The MIT License
Copyright © 2014-2022 Ilkka Seppälä

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.26.0-SNAPSHOT</version>
</parent>
<artifactId>view-helper</artifactId>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<configuration>
<archive>
<manifest>
<mainClass>com.iluwatar.value.object.App</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
52 changes: 52 additions & 0 deletions view-helper/src/main/java/com/iluwatar/viewhelper/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package com.iluwatar.viewhelper;

import java.math.BigDecimal;
import java.time.LocalDate;

/** The main application class that sets up and runs the View Helper pattern demo. */
public class App {
/**
* The entry point of the application.
*
* @param args the command line arguments
*/
public static void main(String[] args) {
// Raw Product data (no formatting, no UI tags)
var product =
new Product(
"Design patterns book", new BigDecimal("18.90"), LocalDate.of(2025, 4, 19), true);

// Create view, viewHelper and viewHelper
var helper = new ProductViewHelper();
var view = new ConsoleProductView();
var controller = new ProductController(helper, view);

// Handle “request”
controller.handle(product);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.viewhelper;

import lombok.extern.slf4j.Slf4j;

/** Renders {@link ProductViewModel} to the console. */
@Slf4j
public class ConsoleProductView implements View<ProductViewModel> {
@Override
public void render(ProductViewModel productViewModel) {
LOGGER.info(productViewModel.toString());
}
}
31 changes: 31 additions & 0 deletions view-helper/src/main/java/com/iluwatar/viewhelper/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.viewhelper;

import java.math.BigDecimal;
import java.time.LocalDate;

/** Definition of product. */
public record Product(String name, BigDecimal price, LocalDate releaseDate, boolean discounted) {}
Loading
Loading