Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8900b3e

Browse files
committedDec 7, 2024·
implemented the view helper design pattern issue iluwatar#1263
1 parent b375919 commit 8900b3e

File tree

15 files changed

+908
-0
lines changed

15 files changed

+908
-0
lines changed
 

‎.sonarlint/connectedMode.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"sonarCloudOrganization": "iluwatar",
3+
"projectKey": "iluwatar_java-design-patterns"
4+
}

‎view-helper/README.md

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
2+
---
3+
title: "View Helper Pattern in Java: Optimizing Data Rendering"
4+
shortTitle: View Helper
5+
description: "Explore the View Helper Design Pattern in Java to optimize the process of data rendering. Learn how this pattern separates the logic of data processing and presentation, making the application more maintainable and scalable."
6+
category: Structural
7+
language: en
8+
tag:
9+
- Separation of concerns
10+
- Data formatting
11+
- MVC pattern
12+
---
13+
14+
## Also known as
15+
16+
* Data Formatter
17+
* Data Presenter
18+
19+
## Intent of View Helper Design Pattern
20+
21+
The View Helper Design Pattern is a structural pattern used to manage the data rendering process by separating concerns. This pattern allows developers to delegate complex data processing tasks to specialized classes (view helpers), ensuring that the controller only handles user input and coordination between the view and the model. This leads to a cleaner, more maintainable application architecture.
22+
23+
## Detailed Explanation of View Helper Pattern with Real-World Examples
24+
25+
Real-world example
26+
27+
> Think of a large e-commerce website where the product data needs to be processed differently depending on the context (e.g., displayed on the homepage, in a product detail page, or in search results). Instead of embedding complex logic within the view, which may cause redundancy and maintenance issues, a view helper class can format the data appropriately based on where it’s displayed.
28+
>
29+
> In this analogy, the view helper acts as the intermediary between the model and the view, ensuring that data is presented in the correct format without cluttering the view code with business logic.
30+
31+
In plain words
32+
33+
> The view helper pattern separates the logic of data formatting and processing from the actual display or view layer. It ensures that the view focuses solely on rendering the data while delegating complex logic to a separate class.
34+
35+
Wikipedia says
36+
37+
> A view helper is a design pattern used in web applications that assists in formatting or processing data before it is displayed in the view.
38+
39+
## Programmatic Example of View Helper Pattern in Java
40+
41+
The View Helper design pattern helps manage the separation of concerns in Java applications by delegating data processing and formatting tasks to specialized helper classes.
42+
43+
Consider an online system that needs to display user and product information in different formats. A `UserViewHelper` is created to process the `User` object, and a `ProductViewHelper` is created to process the `Product` object.
44+
45+
Here is an example implementation:
46+
47+
### User.java
48+
49+
```java
50+
public class User {
51+
private final String name;
52+
private final int age;
53+
private final String email;
54+
55+
public User(String name, int age, String email) {
56+
this.name = name;
57+
this.age = age;
58+
this.email = email;
59+
}
60+
61+
public String getName() {
62+
return name;
63+
}
64+
65+
public int getAge() {
66+
return age;
67+
}
68+
69+
public String getEmail() {
70+
return email;
71+
}
72+
}
73+
```
74+
75+
### Product.java
76+
77+
```java
78+
public class Product {
79+
private final int id;
80+
private final String name;
81+
private final double price;
82+
83+
public Product(int id, String name, double price) {
84+
this.id = id;
85+
this.name = name;
86+
this.price = price;
87+
}
88+
89+
public int getId() {
90+
return id;
91+
}
92+
93+
public String getName() {
94+
return name;
95+
}
96+
97+
public double getPrice() {
98+
return price;
99+
}
100+
}
101+
```
102+
103+
### ViewHelper.java
104+
105+
```java
106+
public interface ViewHelper<T> {
107+
String handle(T object);
108+
}
109+
110+
```
111+
112+
### UserViewHelper.java
113+
114+
```java
115+
public class UserViewHelper implements ViewHelper<User> {
116+
@Override
117+
public String handle(User user) {
118+
return String.format("User: %s, Age: %d, Email: %s", user.getName(), user.getAge(), user.getEmail());
119+
}
120+
}
121+
```
122+
123+
### ProductViewHelper.java
124+
125+
```java
126+
public class ProductViewHelper implements ViewHelper<Product> {
127+
@Override
128+
public String handle(Product product) {
129+
return String.format("Product ID: %d, Name: %s, Price: %.2f", product.getId(), product.getName(), product.getPrice());
130+
}
131+
}
132+
```
133+
134+
### ViewHelperApp.java
135+
136+
```java
137+
import java.util.logging.Logger;
138+
139+
public final class ViewHelperApp {
140+
141+
private static final Logger LOGGER = Logger.getLogger(ViewHelperApp.class.getName());
142+
143+
// Define constants for magic numbers
144+
private static final int DEFAULT_USER_AGE = 30;
145+
private static final double DEFAULT_PRODUCT_PRICE = 999.99;
146+
private static final String DEFAULT_USER_NAME = "John Doe";
147+
private static final String DEFAULT_USER_EMAIL = "john.doe@example.com";
148+
private static final int DEFAULT_PRODUCT_ID = 1;
149+
private static final String DEFAULT_PRODUCT_NAME = "Laptop";
150+
151+
private ViewHelperApp() {
152+
// Prevent instantiation
153+
}
154+
155+
public static void main(String... args) {
156+
// Creating a User instance using constants
157+
User user = new User(DEFAULT_USER_NAME, DEFAULT_USER_AGE, DEFAULT_USER_EMAIL);
158+
159+
// Creating a Product instance using constants
160+
Product product = new Product(DEFAULT_PRODUCT_ID, DEFAULT_PRODUCT_NAME, DEFAULT_PRODUCT_PRICE);
161+
162+
// Creating ViewHelper instances for User and Product
163+
ViewHelper<User> userViewHelper = new UserViewHelper();
164+
ViewHelper<Product> productViewHelper = new ProductViewHelper();
165+
166+
// Displaying the formatted user and product information using the logger
167+
LOGGER.info(userViewHelper.handle(user));
168+
LOGGER.info(productViewHelper.handle(product));
169+
}
170+
}
171+
172+
```
173+
174+
### When to Use the View Helper Pattern in Java
175+
176+
Use the View Helper pattern when:
177+
178+
* There is a need to format or process data in a way that is independent of the view layer.
179+
* You want to keep the controller focused on managing user interactions rather than handling data formatting.
180+
* Your application requires different types of views with different data formats.
181+
182+
### View Helper Pattern Java Tutorials
183+
184+
* [The View Helper Pattern (Baeldung)](https://www.baeldung.com/java/view-helper-pattern)
185+
186+
### Real-World Applications of View Helper Pattern in Java
187+
188+
* Formatting user data before display (e.g., converting date formats, currency formatting).
189+
* Managing product data to display in various parts of an e-commerce website.
190+
* Separating the business logic from the view layer in MVC (Model-View-Controller) applications.
191+
192+
### Benefits and Trade-offs of View Helper Pattern
193+
194+
Benefits:
195+
196+
* Promotes separation of concerns, improving code maintainability.
197+
* Reduces duplication by centralizing data processing and formatting logic.
198+
199+
Trade-offs:
200+
201+
* Adds complexity by introducing additional classes.
202+
* May result in slight overhead for smaller applications where such separation is unnecessary.
203+
204+
### Related Java Design Patterns
205+
206+
* [MVC (Model-View-Controller)](https://java-design-patterns.com/patterns/mvc/)
207+
* [Strategy](https://java-design-patterns.com/patterns/strategy/)
208+
* [Decorator](https://java-design-patterns.com/patterns/decorator/)
209+
210+
### References and Credits
211+
212+
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
213+
* [Effective Java](https://amzn.to/4cGk2Jz)
214+
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)

‎view-helper/etc/view-helper.urm.png

60.7 KB
Loading

‎view-helper/etc/view-helper.urm.puml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
@startuml
2+
package com.iluwatar.viewhelper {
3+
class ViewHelper {
4+
<<interface>>
5+
+ handle(object : T) : String
6+
}
7+
8+
class User {
9+
- name : String
10+
- age : int
11+
- email : String
12+
+ User(name : String, age : int, email : String)
13+
+ getName() : String
14+
+ getAge() : int
15+
+ getEmail() : String
16+
}
17+
18+
class Product {
19+
- id : int
20+
- name : String
21+
- price : double
22+
+ Product(id : int, name : String, price : double)
23+
+ getId() : int
24+
+ getName() : String
25+
+ getPrice() : double
26+
}
27+
28+
class UserViewHelper {
29+
+ handle(user : User) : String
30+
}
31+
32+
class ProductViewHelper {
33+
+ handle(product : Product) : String
34+
}
35+
36+
class ViewHelperApp {
37+
- LOGGER : Logger {static}
38+
+ main(args : String[]) {static}
39+
}
40+
41+
ViewHelperApp ..> UserViewHelper : Uses
42+
ViewHelperApp ..> ProductViewHelper : Uses
43+
UserViewHelper ..|> ViewHelper : Implements
44+
ProductViewHelper ..|> ViewHelper : Implements
45+
}
46+
47+
@enduml

‎view-helper/pom.xml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
31+
<parent>
32+
<groupId>com.iluwatar</groupId>
33+
<artifactId>java-design-patterns</artifactId>
34+
<version>1.26.0-SNAPSHOT</version>
35+
</parent>
36+
37+
<artifactId>view-helper</artifactId>
38+
39+
<dependencies>
40+
<!-- SLF4J API for logging -->
41+
<dependency>
42+
<groupId>org.slf4j</groupId>
43+
<artifactId>slf4j-api</artifactId>
44+
<version>1.7.32</version> <!-- Use the latest version -->
45+
</dependency>
46+
47+
<!-- Logback Classic for SLF4J binding -->
48+
<dependency>
49+
<groupId>ch.qos.logback</groupId>
50+
<artifactId>logback-classic</artifactId>
51+
<version>1.4.12</version> <!-- Use the latest version -->
52+
</dependency>
53+
54+
<!-- JUnit 5 for unit testing -->
55+
<dependency>
56+
<groupId>org.junit.jupiter</groupId>
57+
<artifactId>junit-jupiter-engine</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
61+
<!-- Mockito for mocking in unit tests -->
62+
<dependency>
63+
<groupId>org.mockito</groupId>
64+
<artifactId>mockito-core</artifactId>
65+
<scope>test</scope>
66+
</dependency>
67+
</dependencies>
68+
69+
<build>
70+
<plugins>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-assembly-plugin</artifactId>
74+
<executions>
75+
<execution>
76+
<configuration>
77+
<archive>
78+
<manifest>
79+
<mainClass>com.iluwatar.viewhelper.ViewHelper</mainClass>
80+
</manifest>
81+
</archive>
82+
</configuration>
83+
</execution>
84+
</executions>
85+
</plugin>
86+
</plugins>
87+
</build>
88+
89+
</project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
/**
28+
* Represents a product with an ID, name, and price.
29+
*/
30+
public class Product {
31+
// Fields should have proper indentation and be marked private
32+
private final int id;
33+
private final String name;
34+
private final double price;
35+
36+
/**
37+
* Constructs a Product with the specified ID, name, and price.
38+
*
39+
* @param id The product's ID
40+
* @param name The product's name
41+
* @param price The product's price
42+
*/
43+
public Product(int id, String name, double price) {
44+
this.id = id;
45+
this.name = name;
46+
this.price = price;
47+
}
48+
49+
50+
public int getId() {
51+
return id;
52+
}
53+
54+
55+
public String getName() {
56+
return name;
57+
}
58+
59+
60+
public double getPrice() {
61+
return price;
62+
}
63+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
/**
28+
* View Helper for formatting product information.
29+
*
30+
* @param <T> the type of the object
31+
*/
32+
public class ProductViewHelper implements ViewHelper<Product> {
33+
34+
@Override
35+
public String handle(Product product) {
36+
return String.format(
37+
"Product ID: %d, Name: %s, Price: %.2f",
38+
product.getId(), product.getName(), product.getPrice());
39+
}
40+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
/**
28+
* Represents a User with a name, age, and email address.
29+
*/
30+
public class User {
31+
32+
private final String name;
33+
private final int age;
34+
private final String email;
35+
36+
/**
37+
* Constructs a User with the specified name, age, and email.
38+
*
39+
* @param name the user's name
40+
* @param age the user's age
41+
* @param email the user's email address
42+
*/
43+
public User(String name, int age, String email) {
44+
this.name = name;
45+
this.age = age;
46+
this.email = email;
47+
}
48+
49+
/**
50+
* Gets the user's name.
51+
*
52+
* @return the user's name
53+
*/
54+
public String getName() {
55+
return name;
56+
}
57+
58+
/**
59+
* Gets the user's age.
60+
*
61+
* @return the user's age
62+
*/
63+
public int getAge() {
64+
return age;
65+
}
66+
67+
/**
68+
* Gets the user's email address.
69+
*
70+
* @return the user's email address
71+
*/
72+
public String getEmail() {
73+
return email;
74+
}
75+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
/**
28+
* View Helper for formatting user information.
29+
*
30+
* @param <T> the type of the object
31+
*/
32+
public class UserViewHelper implements ViewHelper<User> {
33+
34+
@Override
35+
public String handle(User user) {
36+
return String.format(
37+
"User: %s, Age: %d, Email: %s",
38+
user.getName(), user.getAge(), user.getEmail());
39+
}
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
/**
28+
* Interface for defining a View Helper in the application.
29+
*
30+
* @param <T> the type of object this View Helper processes
31+
*/
32+
public interface ViewHelper<T> {
33+
34+
/**
35+
* Handles the processing of the given object and returns a formatted string representation.
36+
*
37+
* @param object the object to be processed
38+
* @return a formatted string representation of the object
39+
*/
40+
String handle(T object);
41+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.util.logging.Logger;
28+
29+
/**
30+
* The main application class for demonstrating the View Helper pattern.
31+
*/
32+
public final class ViewHelperApp {
33+
34+
private static final Logger LOGGER = Logger.getLogger(ViewHelperApp.class.getName());
35+
private static final int AGE = 30;
36+
private static final double PRICE = 999.99;
37+
38+
private ViewHelperApp() {
39+
// Prevent instantiation
40+
}
41+
42+
/**
43+
* The entry point of the application.
44+
*
45+
* @param args command line arguments (not used)
46+
*/
47+
public static void main(String... args) {
48+
User user = new User("John Doe", AGE, "john.doe@example.com");
49+
Product product = new Product(1, "Laptop", PRICE);
50+
51+
// Conditionally log the user details with string formatting
52+
if (LOGGER.isLoggable(java.util.logging.Level.INFO)) {
53+
LOGGER.info(String.format("User details: Name = %s, Age = %d, Email = %s",
54+
user.getName(), user.getAge(), user.getEmail()));
55+
}
56+
57+
// Conditionally log the product details with string formatting
58+
if (LOGGER.isLoggable(java.util.logging.Level.INFO)) {
59+
LOGGER.info(String.format("Product details: ID = %d, Name = %s, Price = %.2f",
60+
product.getId(), product.getName(), product.getPrice()));
61+
}
62+
}
63+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 org.junit.jupiter.api.Test;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
31+
/**
32+
* Tests for {@link ProductViewHelper}
33+
*/
34+
class ProductViewHelperTest {
35+
36+
@Test
37+
void testHandle() {
38+
Product product = new Product(1, "Laptop", 999.99);
39+
ViewHelper<Product> productViewHelper = new ProductViewHelper();
40+
41+
String result = productViewHelper.handle(product);
42+
43+
// Assert that the output matches the expected format
44+
assertEquals("Product ID: 1, Name: Laptop, Price: 999.99", result);
45+
}
46+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
// Add package declaration here
27+
28+
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
31+
32+
import static org.junit.jupiter.api.Assertions.*;
33+
34+
class UserTest {
35+
36+
private User user;
37+
38+
@BeforeEach
39+
void setUp() {
40+
// Initializing the User object before each test
41+
user = new User("John Doe", 30, "john.doe@example.com");
42+
}
43+
44+
@Test
45+
void testGetName() {
46+
assertEquals("John Doe", user.getName(), "User's name should be 'John Doe'");
47+
}
48+
49+
@Test
50+
void testGetAge() {
51+
assertEquals(30, user.getAge(), "User's age should be 30");
52+
}
53+
54+
@Test
55+
void testGetEmail() {
56+
assertEquals("john.doe@example.com", user.getEmail(), "User's email should be 'john.doe@example.com'");
57+
}
58+
59+
@Test
60+
void testConstructorWithValidValues() {
61+
User newUser = new User("Jane Doe", 25, "jane.doe@example.com");
62+
assertNotNull(newUser);
63+
assertEquals("Jane Doe", newUser.getName());
64+
assertEquals(25, newUser.getAge());
65+
assertEquals("jane.doe@example.com", newUser.getEmail());
66+
}
67+
68+
@Test
69+
void testConstructorWithInvalidEmail() {
70+
// Test with invalid email (edge case)
71+
User invalidUser = new User("Invalid User", 40, "invalidemail");
72+
assertEquals("invalidemail", invalidUser.getEmail(), "Email should be 'invalidemail'");
73+
}
74+
75+
@Test
76+
void testGetNameNotNull() {
77+
assertNotNull(user.getName(), "User's name should not be null");
78+
}
79+
80+
@Test
81+
void testAgePositive() {
82+
assertTrue(user.getAge() > 0, "User's age should be a positive number");
83+
}
84+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 org.junit.jupiter.api.Test;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
31+
/**
32+
* Tests for {@link UserViewHelper}
33+
*/
34+
class UserViewHelperTest {
35+
36+
@Test
37+
void testHandle() {
38+
User user = new User("John Doe", 30, "john.doe@example.com");
39+
ViewHelper<User> userViewHelper = new UserViewHelper();
40+
41+
String result = userViewHelper.handle(user);
42+
43+
// Assert that the output matches the expected format
44+
assertEquals("User: John Doe, Age: 30, Email: john.doe@example.com", result);
45+
}
46+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.function.Executable;
29+
30+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
31+
32+
/**
33+
* Application test for ViewHelperApp
34+
*/
35+
class ViewHelperAppTest {
36+
37+
/**
38+
* Issue: Add at least one assertion to this test case.
39+
*
40+
* Solution: Inserted assertion to check whether the execution of the main method in {@link ViewHelperApp#main(String[])}
41+
* throws an exception.
42+
*
43+
* This test case ensures that the main method of the ViewHelperApp class executes without throwing any exceptions.
44+
* Since the main method does not return any result, we use assertDoesNotThrow to confirm it runs successfully.
45+
*/
46+
@Test
47+
void shouldExecuteApplicationWithoutException() {
48+
// Check that executing the main method does not throw any exceptions
49+
assertDoesNotThrow(new Executable() {
50+
@Override
51+
public void execute() throws Throwable {
52+
ViewHelperApp.main();
53+
}
54+
});
55+
}
56+
}

0 commit comments

Comments
 (0)
Please sign in to comment.