-
-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Resolved issue #1323 Two-Step View pattern #3106
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
microservices-idempotent-consumer/src/test/java/com/iluwatar/idempotentconsumer/AppTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...tent-consumer/src/test/java/com/iluwatar/idempotentconsumer/RequestStateMachineTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # Book Data Processing and Presentation | ||
|
|
||
| This example demonstrates a simple pipeline for processing book data and rendering it as an HTML view. It involves three main steps: **data preparation**, **business logic processing**, and **presentation**. The structure of the program aligns with the principles of the **Presentation-Abstraction-Control (PAC)** design pattern. | ||
|
|
||
| --- | ||
|
|
||
| ## Problem Addressed | ||
| This implementation solves the issue described in [Issue #1323](https://github.com/iluwatar/java-design-patterns/issues/1323) of the [java-design-patterns repository](https://github.com/iluwatar/java-design-patterns). The goal is to showcase how to split application logic into phases while keeping the layers loosely coupled. | ||
|
|
||
| --- | ||
|
|
||
| ## Overview of the Solution | ||
| The program is divided into the following parts: | ||
|
|
||
| 1. **Data Layer** | ||
| - The `Book` class represents the raw data for a book. | ||
| - The `BookStore` class holds the processed book data for display. | ||
|
|
||
| 2. **Logic Layer** | ||
| - The `DataPreparation` class handles the business logic. It computes whether a discount is applied and checks stock availability. | ||
|
|
||
| 3. **Presentation Layer** | ||
| - The `Presentation` class generates an HTML view of the book data. | ||
|
|
||
| --- | ||
|
|
||
| ## How to Run the Code | ||
|
|
||
| 1. Clone the repository: | ||
| ```bash | ||
| git clone https://github.com/iluwatar/java-design-patterns.git | ||
| cd java-design-patterns | ||
| ``` | ||
|
|
||
| 2. Compile and run the `App` class: | ||
| ```bash | ||
| javac com/iluwatar/*.java | ||
| java com.iluwatar.App | ||
| ``` | ||
|
|
||
| 3. Expected Output: | ||
| The program generates an HTML representation of the book data and prints it to the console: | ||
| ```html | ||
| <div class='book'> | ||
| <h1>Batman Vol. 1: The Court of Owls</h1> | ||
| <p>Price: $11.6</p> | ||
| <p>Discounted Price: $8.7</p> | ||
| <p>Status: In Stock</p> | ||
| </div> | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Code Structure | ||
|
|
||
| - `Book`: Represents raw book data with fields like `name`, `price`, `discount`, and `stock`. | ||
| - `BookStore`: Represents processed data including `discountPrice` and `inStock` status. | ||
| - `DataPreparation`: Contains logic to calculate the discount price and determine stock status. | ||
| - `Presentation`: Generates the HTML view of the book data. | ||
| - `App`: Entry point that orchestrates the flow. | ||
|
|
||
| --- | ||
|
|
||
| ## Key Features | ||
|
|
||
| - **Layered Architecture**: The application is divided into layers for better separation of concerns. | ||
| - **Scalable Design**: The modular approach makes it easier to extend functionality, such as adding new data preparation rules or presentation formats. | ||
| - **Reusability**: Each layer operates independently, promoting code reuse. | ||
|
|
||
| --- | ||
|
|
||
| ## Related Resources | ||
|
|
||
| - **GitHub Issue**: [Issue #1323](https://github.com/iluwatar/java-design-patterns/issues/1323) | ||
| - **Repository**: [java-design-patterns](https://github.com/iluwatar/java-design-patterns) | ||
| - **Design Pattern**: [Two-Step-View-Pattern](https://martinfowler.com/eaaCatalog/twoStepView.html) | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?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>two-step-view</artifactId> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>17</maven.compiler.source> | ||
| <maven.compiler.target>17</maven.compiler.target> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
sharara6 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
sharara6 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| </project> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import java.util.logging.Logger; | ||
|
|
||
| /** Main class. */ | ||
sharara6 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public class App { | ||
| private static final Logger logger = Logger.getLogger(App.class.getName()); | ||
|
|
||
| /** Main function. */ | ||
| public static void main(String[] args) { | ||
| // Create a Book instance with sample data | ||
| Book book = new Book("Batman Vol. 1: The Court of Owls", 11.60, true, 10); | ||
|
|
||
| // Convert raw book data into a structured format | ||
| BookStore preparedData = DataPreparation.prepareBook(book); | ||
|
|
||
| String htmlOutput = Presentation.presentBook(preparedData); | ||
sharara6 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Output the rendered HTML | ||
| logger.info(htmlOutput); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * 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; | ||
| import lombok.Data; | ||
|
|
||
| /** Represents a Book. */ | ||
| @Data | ||
| public class Book { | ||
| String name; | ||
| double price; | ||
| boolean discount; | ||
| int stock; | ||
| Book(String name, double price, boolean discount, int stock) { | ||
| if (!name.isEmpty()) { // Make sure is not empty | ||
| this.name = name; | ||
| } | ||
| if (price > 0) { // Make sure price is set with a realistic value | ||
| this.price = price; | ||
| } | ||
| if (stock >= 0) { // Make sure stock is not negative | ||
| this.stock = stock; | ||
| } | ||
| this.discount = discount; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Data; | ||
|
|
||
| /** Represents the processed book data used for display purposes. */ | ||
| @AllArgsConstructor | ||
| @Data | ||
| public class BookStore { | ||
| String name; | ||
| double price; | ||
| double discountPrice; // Calculates price if the book is discounted | ||
| boolean inStock; // Indicates if the book is in stock | ||
|
|
||
| } |
43 changes: 43 additions & 0 deletions
43
two-step-view/src/main/java/com/iluwatar/DataPreparation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
|
|
||
|
|
||
| /** class representing phase 1. */ | ||
| public class DataPreparation { | ||
|
|
||
| private DataPreparation() {} | ||
| /** | ||
| * Prepares book data for the store. | ||
| * Calculates the discount price if applicable and checks stock availability. | ||
| */ | ||
|
|
||
| public static BookStore prepareBook(Book book) { | ||
| double discountPrice = book.isDiscount() ? book.getPrice() * 0.75 : book.getPrice(); | ||
| boolean inStock = book.getStock() > 0; | ||
| return new BookStore(book.getName(), book.getPrice(), discountPrice, inStock); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.