mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 08:58:26 +00:00
docs: update composite view
This commit is contained in:
+18
-23
@@ -3,7 +3,9 @@ title: Composite View
|
||||
category: Structural
|
||||
language: en
|
||||
tag:
|
||||
- Abstraction
|
||||
- Enterprise patterns
|
||||
- Object composition
|
||||
- Presentation
|
||||
---
|
||||
|
||||
@@ -13,20 +15,22 @@ The primary goal of the Composite View design pattern is to compose objects into
|
||||
|
||||
## Explanation
|
||||
|
||||
Real World Example
|
||||
Real-world example
|
||||
|
||||
> A news site wants to display the current date and news to different users based on that user's preferences. The news site will substitute in different news feed components depending on the user's interest, defaulting to local news.
|
||||
> A real-world example of the Composite View design pattern is the layout of a dashboard in a web application. Consider a financial dashboard that displays various widgets such as stock charts, recent transactions, account balances, and news feeds. Each of these widgets is a separate view component that can be independently updated and managed. By using the Composite View pattern, these individual widgets are composed into a single unified dashboard view. This approach allows for easy reorganization of the dashboard, the addition of new widgets without disrupting existing ones, and consistent management of the overall layout. This hierarchical composition of views mirrors how different sections of the dashboard are treated both as individual entities and as part of a larger whole.
|
||||
|
||||
In Plain Words
|
||||
In plain words
|
||||
|
||||
> Composite View Pattern is having a main view being composed of smaller subviews. The layout of this composite view is based on a template. A View-manager then decides which subviews to include in this template.
|
||||
|
||||
Wikipedia Says
|
||||
Wikipedia says
|
||||
|
||||
> Composite views that are composed of multiple atomic subviews. Each component of the template may be included dynamically into the whole and the layout of the page may be managed independently of the content. This solution provides for the creation of a composite view based on the inclusion and substitution of modular dynamic and static template fragments. It promotes the reuse of atomic portions of the view by encouraging modular design.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
A news site wants to display the current date and news to different users based on that user's preferences. The news site will substitute in different news feed components depending on the user's interest, defaulting to local news.
|
||||
|
||||
Since this is a web development pattern, a server is required to demonstrate it. This example uses Tomcat 10.0.13 to run the servlet, and this programmatic example will only work with Tomcat 10+.
|
||||
|
||||
Firstly, there is `AppServlet` which is an `HttpServlet` that runs on Tomcat 10+.
|
||||
@@ -285,11 +289,6 @@ Here are two examples of the mock atomic subviews used in the composite: `busine
|
||||
</html>
|
||||
```
|
||||
|
||||
The results are as such:
|
||||
|
||||
1) The user has put their name as `Tammy` in the request parameters and no preferences: 
|
||||
2) The user has put their name as `Johnny` in the request parameters and has a preference for world, business, and science news: 
|
||||
|
||||
The different subviews such as `worldNews.jsp`, `businessNews.jsp`, etc. are included conditionally based on the request parameters.
|
||||
|
||||
**How To Use**
|
||||
@@ -302,12 +301,6 @@ Under `Run` and `edit configurations` Make sure Tomcat server is one of the run
|
||||
|
||||
Ensure that the artifact is being built from the content of the `web` directory and the compilation results of the module. Point the output of the artifact to a convenient place. Run the configuration and view the landing page, follow instructions on that page to continue.
|
||||
|
||||
## Class diagram
|
||||
|
||||

|
||||
|
||||
The class diagram here displays the Javabean which is the view manager. The views are JSP's held inside the web directory.
|
||||
|
||||
## Applicability:
|
||||
|
||||
Use the Composite View design pattern when:
|
||||
@@ -316,6 +309,10 @@ Use the Composite View design pattern when:
|
||||
* You expect that the composite structures might include any new components in the future.
|
||||
* You want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.
|
||||
|
||||
## Tutorials
|
||||
|
||||
* [Composite View Design Pattern – Core J2EE Patterns (Dinesh on Java)](https://www.dineshonjava.com/composite-view-design-pattern/)
|
||||
|
||||
## Known Uses
|
||||
|
||||
* Graphical User Interfaces (GUIs) where widgets can contain other widgets (e.g., a window containing panels, buttons, and text fields).
|
||||
@@ -335,15 +332,13 @@ Trade-offs:
|
||||
|
||||
## Related Patterns
|
||||
|
||||
* [Decorator](https://java-design-patterns.com/patterns/decorator/): While Decorator is used to add responsibilities to objects, Composite is meant for building structures of objects.
|
||||
* [Flyweight](https://java-design-patterns.com/patterns/flyweight/): Composite can often be combined with Flyweight to implement shared leaf nodes in a composite structure, reducing the memory footprint.
|
||||
* [Chain of Responsibility](https://java-design-patterns.com/patterns/chain-of-responsibility/): Can be used with Composite to let components pass requests through the hierarchy.
|
||||
* [Composite](https://java-design-patterns.com/patterns/composite/)
|
||||
* [View Helper](https://www.oracle.com/java/technologies/viewhelper.html)
|
||||
* [Composite](https://java-design-patterns.com/patterns/composite/): General structural pattern that is the foundation for Composite View, used for treating individual objects and compositions uniformly.
|
||||
* [Decorator](https://java-design-patterns.com/patterns/decorator/): Enhances the behavior of individual views without modifying the underlying view.
|
||||
* [Flyweight](https://java-design-patterns.com/patterns/flyweight/): Can be used to manage memory consumption of large numbers of similar view objects.
|
||||
* View Helper: Separates the view logic from business logic, aiding in the clean organization and management of view components.
|
||||
|
||||
## Credits
|
||||
|
||||
* [Core J2EE Patterns - Composite View](https://www.oracle.com/java/technologies/composite-view.html)
|
||||
* [Composite View Design Pattern – Core J2EE Patterns](https://www.dineshonjava.com/composite-view-design-pattern/)
|
||||
* [Patterns of Enterprise Application Architecture](https://amzn.to/49jpQG3)
|
||||
* [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/3xfntGJ)
|
||||
* [Patterns of Enterprise Application Architecture](https://amzn.to/49jpQG3)
|
||||
* [Core J2EE Patterns - Composite View (Oracle)](https://www.oracle.com/java/technologies/composite-view.html)
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 71 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 77 KiB |
Reference in New Issue
Block a user