|
1 | | -# Book Data Processing and Presentation |
| 1 | +# Two-Step View |
2 | 2 |
|
3 | | -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. |
4 | 3 |
|
5 | 4 | --- |
6 | 5 |
|
7 | | -## Problem Addressed |
8 | | -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. |
| 6 | +## Also known as |
9 | 7 |
|
| 8 | +- Template View. |
| 9 | +- Model-View Separation |
| 10 | +- Separated Presentation |
| 11 | +- View Helper Pattern |
10 | 12 | --- |
11 | 13 |
|
12 | | -## Overview of the Solution |
13 | | -The program is divided into the following parts: |
| 14 | +## Intent of Two-Step View Design Pattern |
| 15 | +- **Decouple Data and Presentation**: Separate data preparation from rendering for better modularity. |
| 16 | +- **Reusable Rendering**: Use templates to reduce duplication. |
| 17 | +- **Flexibility**: Render the same data differently with various templates. |
| 18 | +- **Maintainability**: Simplify testing and updates by isolating logic. |
| 19 | +- **Multi-Format Support**: Prepare data once and render in formats like HTML or JSON. |
| 20 | +--- |
| 21 | + |
| 22 | +## Detailed Explanation of the Two Step View Pattern with Real-World Examples |
| 23 | + |
| 24 | +**Real-World Example**: |
| 25 | +Imagine a restaurant kitchen. |
| 26 | + |
| 27 | +**Step 1**: Preparation – The chef cooks and plates the food, ensuring it is ready to be served. |
14 | 28 |
|
15 | | -1. **Data Layer** |
16 | | - - The `Book` class represents the raw data for a book. |
17 | | - - The `BookStore` class holds the processed book data for display. |
| 29 | +**Step 2**: Presentation – The waiter serves the plated food to the customer, adding any final touches, such as garnishes. |
18 | 30 |
|
19 | | -2. **Logic Layer** |
20 | | - - The `DataPreparation` class handles the business logic. It computes whether a discount is applied and checks stock availability. |
| 31 | +**In Plain Words**: The Two-Step View Pattern separates the process of preparing data from how it is presented. |
21 | 32 |
|
22 | | -3. **Presentation Layer** |
23 | | - - The `Presentation` class generates an HTML view of the book data. |
| 33 | +**Authoritative Source**: Martin Fowler describes the Two-Step View pattern as transforming domain data into HTML in two stages: first by creating a logical page, then rendering it into HTML.(https://martinfowler.com/eaaCatalog/twoStepView.html) |
24 | 34 |
|
25 | 35 | --- |
26 | 36 |
|
27 | | -## How to Run the Code |
28 | | - |
29 | | -1. Clone the repository: |
30 | | - ```bash |
31 | | - git clone https://github.com/iluwatar/java-design-patterns.git |
32 | | - cd java-design-patterns |
33 | | - ``` |
34 | | - |
35 | | -2. Compile and run the `App` class: |
36 | | - ```bash |
37 | | - javac com/iluwatar/*.java |
38 | | - java com.iluwatar.App |
39 | | - ``` |
40 | | - |
41 | | -3. Expected Output: |
42 | | - The program generates an HTML representation of the book data and prints it to the console: |
43 | | - ```html |
44 | | - <div class='book'> |
45 | | - <h1>Batman Vol. 1: The Court of Owls</h1> |
46 | | - <p>Price: $11.6</p> |
47 | | - <p>Discounted Price: $8.7</p> |
48 | | - <p>Status: In Stock</p> |
49 | | - </div> |
50 | | - ``` |
| 37 | +## Programmatic Example of Two-Step View Pattern in Java |
| 38 | + |
| 39 | + |
| 40 | +#### **Phase 1: Data Preparation (`DataPreparation`)** |
| 41 | +- **Responsibility:** Processes raw `Book` data by calculating the discount price and checking stock availability. |
| 42 | +- **Method:** `prepareBook(Book book)` |
| 43 | +- **Output:** Returns a `BookStore` object with processed details. |
| 44 | +- **Example:** |
| 45 | + ```java |
| 46 | + BookStore bookStore = DataPreparation.prepareBook(book); |
| 47 | + ``` |
| 48 | + |
| 49 | +#### **Phase 2: Presentation (`Presentation`)** |
| 50 | +- **Responsibility:** Converts the prepared `BookStore` data into an HTML representation. |
| 51 | +- **Method:** `presentBook(BookStore bookInStore)` |
| 52 | +- **Output:** HTML formatted book details. |
| 53 | +- **Example:** |
| 54 | + ```java |
| 55 | + String bookHtml = Presentation.presentBook(bookStore); |
| 56 | + ``` |
| 57 | + |
51 | 58 |
|
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +## When to Use the Two Step View Pattern in Java |
| 63 | + |
| 64 | +- **Separation of Logic:** When you need to separate data preparation from presentation for better modularity. |
| 65 | +- **Multiple Formats:** When the same data needs to be rendered in multiple formats (e.g., HTML, JSON, XML). |
| 66 | +- **Reusable Templates:** When you want to reuse rendering logic across different parts of the application. |
| 67 | +- **Maintainability:** When frequent changes in the presentation layer shouldn't impact the business logic or data preparation. |
| 68 | +- **Flexibility:** When different rendering methods are required for the same prepared data based on user preferences or contexts. |
| 69 | +- **Scalability:** When adding new presentation formats without affecting existing preparation logic is needed. |
| 70 | +- **MVC Architecture:** When implementing the Model-View-Controller (MVC) design pattern, where the model (data preparation) and view (presentation) are distinctly separate. |
| 71 | +- **Testability:** When isolating and unit testing data preparation and rendering logic independently is a priority. |
52 | 72 | --- |
| 73 | +## Two-Step View Pattern Java Tutorials |
| 74 | + |
| 75 | + |
| 76 | +- **Martin Fowler's Explanation**: Martin Fowler discusses the Two-Step View pattern, detailing its application in transforming domain data into HTML in two stages.(https://martinfowler.com/eaaCatalog/twoStepView) |
| 77 | + |
| 78 | +- **Stack Overflow Discussion**: A discussion on Stack Overflow explores the implementation of the Two Step View pattern in Spring MVC, highlighting its benefits and practical considerations.(https://stackoverflow.com/questions/58114790/spring-mvc-one-step-view-or-two-step-view) |
53 | 79 |
|
54 | | -## Code Structure |
| 80 | +- **Java Design Patterns Tutorial**: This comprehensive tutorial covers various design patterns in Java, including structural patterns that may relate to the Two Step View pattern.(https://www.digitalocean.com/community/tutorials/java-design-patterns-example-tutorial) |
55 | 81 |
|
56 | | -- `Book`: Represents raw book data with fields like `name`, `price`, `discount`, and `stock`. |
57 | | -- `BookStore`: Represents processed data including `discountPrice` and `inStock` status. |
58 | | -- `DataPreparation`: Contains logic to calculate the discount price and determine stock status. |
59 | | -- `Presentation`: Generates the HTML view of the book data. |
60 | | -- `App`: Entry point that orchestrates the flow. |
61 | 82 |
|
| 83 | + |
| 84 | +--- |
| 85 | +## Real-World Applications of Two-Step View Pattern in Java |
| 86 | + |
| 87 | +- **Spring MVC**: Separates the logic of data preparation from view rendering, using technologies like JSP, Thymeleaf, or FreeMarker. ([Stack Overflow discussion](https://stackoverflow.com/questions/58114790/spring-mvc-one-step-view-or-two-step-view)) |
| 88 | +- **Composite View Pattern**: Similar to Two Step View, this pattern involves composing view components into tree structures, promoting separation of concerns. ([Java Design Patterns - Composite View](https://java-design-patterns.com/patterns/composite-view/?utm_source=chatgpt.com)) |
| 89 | +- **Martin Fowler's Two Step View**: A foundational explanation of transforming domain data into HTML in two stages, applied in web applications. ([martinfowler.com](https://martinfowler.com/eaaCatalog/twoStepView.html?utm_source=chatgpt.com)) |
| 90 | +--- |
| 91 | +### **Benefits of the Two Step View Pattern** |
| 92 | + |
| 93 | +- **Separation of Concerns:** Separates data processing from presentation logic, making the system more modular and easier to maintain. |
| 94 | +- **Reusability:** Data preparation and presentation can be reused independently across different views or formats. |
| 95 | +- **Flexibility:** Easily switch or modify the presentation layer (e.g., HTML, XML, JSON) without changing the underlying data logic. |
| 96 | +- **Maintainability:** Changes in the data model or presentation logic are isolated, reducing the risk of side effects. |
| 97 | +- **Testability:** Each phase (data preparation and rendering) can be unit tested independently, improving code quality and reliability. |
| 98 | +- **Scalability:** Adding new views or presentation formats becomes easier without altering the core business logic. |
| 99 | +--- |
| 100 | + |
| 101 | +## **Trade-offs of the Two Step View Pattern** |
| 102 | + |
| 103 | +- **Increased Complexity:** Implementing two separate steps adds complexity, particularly in smaller applications where a simple approach may suffice. |
| 104 | +- **Performance Overhead:** Dividing the logic into two phases may introduce unnecessary overhead, especially in performance-critical applications. |
| 105 | +- **Over-engineering:** For simple applications, the pattern might be overkill, leading to unnecessary abstraction. |
| 106 | +- **Tighter Coupling of Phases:** If not designed properly, the two phases (data preparation and presentation) might still become tightly coupled, reducing the pattern's effectiveness. |
| 107 | +- **Development Time:** The initial implementation of two phases may require more time compared to simpler, more straightforward methods. |
62 | 108 | --- |
| 109 | +## **Related Java Design Patterns** |
63 | 110 |
|
64 | | -## Key Features |
| 111 | +- **Model-View-Controller (MVC):** Separates data (Model), presentation (View), and user interaction (Controller), which is conceptually similar to the Two Step View pattern. |
| 112 | + [Learn more](https://refactoring.guru/design-patterns/mvc) |
65 | 113 |
|
66 | | -- **Layered Architecture**: The application is divided into layers for better separation of concerns. |
67 | | -- **Scalable Design**: The modular approach makes it easier to extend functionality, such as adding new data preparation rules or presentation formats. |
68 | | -- **Reusability**: Each layer operates independently, promoting code reuse. |
| 114 | +- **Composite View:** Builds complex views by combining multiple components into a tree structure, promoting modularity in rendering views. |
| 115 | + [Learn more](https://java-design-patterns.com/patterns/composite-view/?utm_source=chatgpt.com) |
69 | 116 |
|
| 117 | +- **Template Method:** Defines the structure of an algorithm but allows certain steps to be implemented by subclasses, similar to separating data preparation from presentation. |
| 118 | + [Learn more](https://refactoring.guru/design-patterns/template-method) |
| 119 | + |
| 120 | +- **Strategy Pattern:** Allows the selection of algorithms (e.g., data preparation or presentation rendering) at runtime, offering flexibility in handling multiple views. |
| 121 | + [Learn more](https://refactoring.guru/design-patterns/strategy) |
| 122 | + |
| 123 | +- **Decorator Pattern:** Adds behavior or functionality to an object dynamically, which can be useful for enhancing view rendering capabilities. |
| 124 | + [Learn more](https://refactoring.guru/design-patterns/decorator) |
| 125 | + |
| 126 | +- **Abstract Factory Pattern:** Creates families of related objects without specifying their concrete classes, similar to abstracting view rendering across different formats. |
| 127 | + [Learn more](https://refactoring.guru/design-patterns/abstract-factory) |
70 | 128 | --- |
| 129 | +## **References and Credits for the Two Step View Pattern** |
| 130 | + |
| 131 | +- **"Patterns of Enterprise Application Architecture"** by Martin Fowler |
| 132 | + A key resource explaining the Two Step View pattern, among other enterprise application design patterns. |
| 133 | + [Link to book](https://martinfowler.com/books/eaa.html) |
| 134 | + |
| 135 | +- **"Design Patterns: Elements of Reusable Object-Oriented Software"** by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides |
| 136 | + This book outlines foundational design patterns that are conceptually related to the Two Step View pattern, like MVC and Composite View. |
| 137 | + [Link to book](https://www.pearson.com/store/p/design-patterns-elements-of-reusable-object-oriented-software/P100000825256) |
| 138 | + |
| 139 | +- **"Refactoring: Improving the Design of Existing Code"** by Martin Fowler |
| 140 | + Discusses how to refactor code in a way that aligns with the principles of modularity and separation of concerns, which are also key goals of the Two Step View pattern. |
| 141 | + [Link to book](https://martinfowler.com/books/refactoring.html) |
| 142 | + |
| 143 | +- **"Head First Design Patterns"** by Eric Freeman and Elisabeth Robson |
| 144 | + A beginner-friendly resource that explains various design patterns, including those related to view rendering, like MVC and Composite View. |
| 145 | + [Link to book](https://www.oreilly.com/library/view/head-first-design/9780596007126/) |
71 | 146 |
|
72 | | -## Related Resources |
| 147 | +- **"Java Design Patterns: A Hands-On Experience with Real-World Examples"** by Vaskaran Sarcar |
| 148 | + Provides practical examples of design patterns in Java, with potential applications for the Two Step View pattern. |
| 149 | + [Link to book](https://www.amazon.com/Java-Design-Patterns-Hands-Experience/dp/1788621754) |
73 | 150 |
|
74 | | -- **GitHub Issue**: [Issue #1323](https://github.com/iluwatar/java-design-patterns/issues/1323) |
75 | | -- **Repository**: [java-design-patterns](https://github.com/iluwatar/java-design-patterns) |
76 | | -- **Design Pattern**: [Two-Step-View-Pattern](https://martinfowler.com/eaaCatalog/twoStepView.html) |
| 151 | +- **"Design Patterns in Java"** by Steven John Metsker |
| 152 | + A book that covers Java-specific examples of design patterns and explores how they can be used effectively in enterprise applications. |
| 153 | + [Link to book](https://www.amazon.com/Design-Patterns-Java-Steven-Metsker/dp/0321192958) |
77 | 154 |
|
78 | 155 |
|
79 | 156 |
|
0 commit comments