java-design-patterns icon indicating copy to clipboard operation
java-design-patterns copied to clipboard

Implement View Helper pattern

Open e5LA opened this issue 7 months ago • 2 comments

What does this PR do?

This PR implements the View Helper design pattern to improve the separation of concerns within the application. It introduces a View Helper class responsible for handling data formatting and processing tasks. The Controller is updated to delegate relevant responsibilities to the View Helper, and the View is refactored to consume the prepared data, resulting in cleaner, more maintainable code.

Fixes #1263

e5LA avatar May 13 '25 18:05 e5LA

PR Summary

This PR implements the View Helper design pattern to improve the separation of concerns. It introduces a ViewHelper class responsible for handling data formatting and processing tasks. The Controller is updated to delegate responsibilities to the ViewHelper, and the View is refactored to consume the prepared data, resulting in cleaner, more maintainable code. A new module view-helper was created, including interfaces for View and ViewHelper, implementations for Product, ProductViewModel, ProductViewHelper, ConsoleProductView, ProductController, and tests.

Changes

File Summary
pom.xml The view-helper module was added to the project's modules list. The order of modules was adjusted to maintain alphabetical order.
view-helper/README.md A comprehensive README file was added, detailing the View Helper pattern, its implementation, use cases, benefits, and related patterns. Includes diagrams.
view-helper/etc/view-helper-sequence-diagram.png New file.
view-helper/etc/view-helper-sequence-diagram.puml New file.
view-helper/etc/view-helper.png New file.
view-helper/etc/view-helper.puml New file.
view-helper/pom.xml A pom.xml file was added for the view-helper module, defining its dependencies (slf4j-api, logback-classic, junit-jupiter-engine).
view-helper/src/main/java/com/iluwatar/viewhelper/App.java This class demonstrates the View Helper pattern by creating a Product, using ProductViewHelper to format it, and rendering it with ConsoleProductView.
view-helper/src/main/java/com/iluwatar/viewhelper/ConsoleProductView.java Implements the View interface, rendering a ProductViewModel to the console using a logger.
view-helper/src/main/java/com/iluwatar/viewhelper/Product.java A simple record defining a Product with its attributes: name, price, release date, and a boolean indicating whether it's discounted.
view-helper/src/main/java/com/iluwatar/viewhelper/ProductController.java The controller takes a Product, uses a ViewHelper to prepare a ProductViewModel, and then uses a View to render it.
view-helper/src/main/java/com/iluwatar/viewhelper/ProductViewHelper.java Implements the ViewHelper interface, formatting a Product into a ProductViewModel for display. Handles currency formatting and date formatting.
view-helper/src/main/java/com/iluwatar/viewhelper/ProductViewModel.java A record representing the view model for a Product, containing formatted data for display.
view-helper/src/main/java/com/iluwatar/viewhelper/View.java Interface defining a View that can render data of type V.
view-helper/src/main/java/com/iluwatar/viewhelper/ViewHelper.java Interface defining a ViewHelper that prepares data of type M for display as type V.
view-helper/src/test/java/com/iluwatar/viewhelper/AppTest.java Tests that the main application runs without throwing exceptions.
view-helper/src/test/java/com/iluwatar/viewhelper/ProductViewHelperTest.java Unit tests for the ProductViewHelper, verifying the formatting of products with and without discounts.

autogenerated by presubmit.ai

github-actions[bot] avatar May 13 '25 18:05 github-actions[bot]

Quality Gate Failed Quality Gate failed

Failed conditions
7 Security Hotspots
E Reliability Rating on New Code (required ≥ A)
E Security Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

sonarqubecloud[bot] avatar May 13 '25 21:05 sonarqubecloud[bot]

This PR is stale because it has been open 60 days with no activity.

github-actions[bot] avatar Jul 13 '25 02:07 github-actions[bot]