Skip to content

Commit a3fcc63

Browse files
e5LAiluwatar
andauthored
feat: Implement View Helper pattern (#3278)
* #1263 - creating new module for view-helper * #1263 - creating Product and ProductViewModel * #1263 - creating interfaces for View and ViewHelper * #1263 - creating ProductViewHelper and its test * #1263 - creating Console View for Product * #1263 - creating ProductController * #1263 - creating main App and its test * #1263 - adding license header * #1263 - keeping module in alphabetical order * #1263 - creating README and diagrams * #1263 apply spotless --------- Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
1 parent dc3425d commit a3fcc63

17 files changed

Lines changed: 628 additions & 2 deletions

pom.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
<sonar.projectName>Java Design Patterns</sonar.projectName>
7272
</properties>
7373
<modules>
74-
7574
<module>abstract-document</module>
7675
<module>abstract-factory</module>
7776
<module>active-object</module>
@@ -244,11 +243,11 @@
244243
<module>update-method</module>
245244
<module>value-object</module>
246245
<module>version-number</module>
246+
<module>view-helper</module>
247247
<module>virtual-proxy</module>
248248
<module>visitor</module>
249249
<module>backpressure</module>
250250
<module>actor-model</module>
251-
252251
</modules>
253252
<repositories>
254253
<repository>

view-helper/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: "View Helper Pattern in Java: Simplifying Presentation Logic in MVC Applications"
3+
shortTitle: View Helper
4+
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."
5+
category: Architectural
6+
language: en
7+
tag:
8+
- Architecture
9+
- Presentation
10+
- Decoupling
11+
- Code reuse
12+
---
13+
14+
## Intent of View Helper Design Pattern
15+
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.
16+
17+
## Detailed Explanation with Real‑World Analogy
18+
Real‑world example
19+
> 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.
20+
>
21+
> 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.
22+
23+
### In plain words
24+
> 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.
25+
26+
### Sequence diagram
27+
![Sequence diagram for View Helper](etc/view-helper-sequence-diagram.png)
28+
29+
## Programmatic Example of View Helper Pattern in Java
30+
Raw domain object
31+
```java
32+
public record Product(String name, BigDecimal price, LocalDate releaseDate, boolean discounted) {}
33+
```
34+
35+
View model object for display
36+
```java
37+
public record ProductViewModel(String name, String price, String releasedDate) {}
38+
```
39+
40+
View Helper formats data for display
41+
```java
42+
class ProductViewHelper implements ViewHelper<Product, ProductViewModel> {
43+
44+
private static final String DISCOUNT_TAG = " (ON SALE)";
45+
46+
public ProductViewModel prepare(Product product) {
47+
var displayName = product.name() + (product.discounted() ? DISCOUNT_TAG : "");
48+
var priceWithCurrency = NumberFormat.getCurrencyInstance(US).format(product.price());
49+
var formattedDate = product.releaseDate().format(ISO_DATE);
50+
51+
return new ProductViewModel(displayName, priceWithCurrency, formattedDate);
52+
}
53+
}
54+
```
55+
56+
View renders the formatted data
57+
```java
58+
public class ConsoleProductView implements View<ProductViewModel> {
59+
60+
@Override
61+
public void render(ProductViewModel productViewModel) {
62+
LOGGER.info(productViewModel.toString());
63+
}
64+
}
65+
```
66+
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.
67+
Then it:
68+
1. Initializes a helper (`ProductViewHelper`) to format the product data for display.
69+
1. Creates a view (`ConsoleProductView`) to render the formatted data.
70+
1. Uses a controller (`ProductController`) to coordinate the flow between raw data, helper logic, and view rendering.
71+
72+
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.
73+
74+
## When to Use the View Helper Pattern in Java
75+
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.
76+
77+
## Real‑World Uses of View Helper Pattern in Java
78+
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.
79+
80+
## Benefits and Trade‑offs
81+
Benefits:
82+
* Separation of concerns: Keeps view templates clean by moving logic into dedicated helpers.
83+
* Reusability: Common formatting and display logic can be reused across multiple views.
84+
* Improved maintainability: Easier to update presentation logic without touching the view.
85+
* Testability: Helpers can be unit tested independently from the UI layer.
86+
87+
Trade‑offs:
88+
* Added complexity: Introduces extra classes, which may feel unnecessary for very simple views.
89+
* Overuse risk: Excessive use of helpers can spread logic thinly across many files, making it harder to trace behavior.
90+
* Tight coupling risk: If not designed carefully, helpers can become tightly coupled to specific views or data formats.
91+
92+
## Related Java Design Patterns
93+
* [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.
94+
* [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.
95+
* [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.
96+
97+
## References & Credits
98+
* [Core J2EE Patterns: View Helper.](https://www.oracle.com/java/technologies/viewhelper.html)
27.3 KB
Loading
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@startuml
2+
actor Client
3+
participant Controller
4+
participant ViewHelper
5+
participant View
6+
participant Product
7+
participant ProductViewModel
8+
9+
Client -> Controller : handle(product)
10+
Controller -> ViewHelper : prepare(product)
11+
ViewHelper -> Product : access data
12+
ViewHelper -> ProductViewModel : return formatted view model
13+
Controller -> View : render(viewModel)
14+
View -> Console : display output
15+
@enduml

view-helper/etc/view-helper.png

54.8 KB
Loading

view-helper/etc/view-helper.puml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
@startuml
2+
package com.iluwatar.viewhelper {
3+
interface View<T> {
4+
+render(T model)
5+
}
6+
7+
interface ViewHelper<S, T> {
8+
+prepare(S source): T
9+
}
10+
11+
class Product {
12+
-name: String
13+
-price: BigDecimal
14+
-releaseDate: LocalDate
15+
-discounted: boolean
16+
}
17+
18+
class ProductViewModel {
19+
-name: String
20+
-price: String
21+
-releasedDate: String
22+
}
23+
24+
class ProductViewHelper {
25+
+prepare(Product): ProductViewModel
26+
}
27+
28+
class ConsoleProductView {
29+
+render(ProductViewModel)
30+
}
31+
32+
class ProductController {
33+
-helper: ViewHelper<Product, ProductViewModel>
34+
-view: View<ProductViewModel>
35+
+handle(Product)
36+
}
37+
}
38+
Product --> ProductViewHelper
39+
ProductViewHelper ..|> ViewHelper
40+
ConsoleProductView ..|> View
41+
ProductViewHelper --> ProductViewModel
42+
ProductController --> ProductViewHelper
43+
ProductController --> ConsoleProductView
44+
45+
@enduml

view-helper/pom.xml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
5+
6+
The MIT License
7+
Copyright © 2014-2022 Ilkka Seppälä
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
-->
28+
<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">
29+
<modelVersion>4.0.0</modelVersion>
30+
<parent>
31+
<groupId>com.iluwatar</groupId>
32+
<artifactId>java-design-patterns</artifactId>
33+
<version>1.26.0-SNAPSHOT</version>
34+
</parent>
35+
<artifactId>view-helper</artifactId>
36+
<dependencies>
37+
<dependency>
38+
<groupId>org.slf4j</groupId>
39+
<artifactId>slf4j-api</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>ch.qos.logback</groupId>
43+
<artifactId>logback-classic</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.junit.jupiter</groupId>
47+
<artifactId>junit-jupiter-engine</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
</dependencies>
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-assembly-plugin</artifactId>
56+
<executions>
57+
<execution>
58+
<configuration>
59+
<archive>
60+
<manifest>
61+
<mainClass>com.iluwatar.value.object.App</mainClass>
62+
</manifest>
63+
</archive>
64+
</configuration>
65+
</execution>
66+
</executions>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
</project>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
package com.iluwatar.viewhelper;
27+
28+
import java.math.BigDecimal;
29+
import java.time.LocalDate;
30+
31+
/** The main application class that sets up and runs the View Helper pattern demo. */
32+
public class App {
33+
/**
34+
* The entry point of the application.
35+
*
36+
* @param args the command line arguments
37+
*/
38+
public static void main(String[] args) {
39+
// Raw Product data (no formatting, no UI tags)
40+
var product =
41+
new Product(
42+
"Design patterns book", new BigDecimal("18.90"), LocalDate.of(2025, 4, 19), true);
43+
44+
// Create view, viewHelper and viewHelper
45+
var helper = new ProductViewHelper();
46+
var view = new ConsoleProductView();
47+
var controller = new ProductController(helper, view);
48+
49+
// Handle “request”
50+
controller.handle(product);
51+
}
52+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.viewhelper;
26+
27+
import lombok.extern.slf4j.Slf4j;
28+
29+
/** Renders {@link ProductViewModel} to the console. */
30+
@Slf4j
31+
public class ConsoleProductView implements View<ProductViewModel> {
32+
@Override
33+
public void render(ProductViewModel productViewModel) {
34+
LOGGER.info(productViewModel.toString());
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.viewhelper;
26+
27+
import java.math.BigDecimal;
28+
import java.time.LocalDate;
29+
30+
/** Definition of product. */
31+
public record Product(String name, BigDecimal price, LocalDate releaseDate, boolean discounted) {}

0 commit comments

Comments
 (0)