Skip to content

Commit 9904085

Browse files
committed
iluwatar#1263 - creating README and diagrams
1 parent 3dbc8ef commit 9904085

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

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

0 commit comments

Comments
 (0)