You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: composite-view/README.md
+18-23Lines changed: 18 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,9 @@ title: Composite View
3
3
category: Structural
4
4
language: en
5
5
tag:
6
+
- Abstraction
6
7
- Enterprise patterns
8
+
- Object composition
7
9
- Presentation
8
10
---
9
11
@@ -13,20 +15,22 @@ The primary goal of the Composite View design pattern is to compose objects into
13
15
14
16
## Explanation
15
17
16
-
Real World Example
18
+
Real-world example
17
19
18
-
> 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.
20
+
> 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.
19
21
20
-
In Plain Words
22
+
In plain words
21
23
22
24
> 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.
23
25
24
-
Wikipedia Says
26
+
Wikipedia says
25
27
26
28
> 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.
27
29
28
30
**Programmatic Example**
29
31
32
+
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.
33
+
30
34
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+.
31
35
32
36
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
285
289
</html>
286
290
```
287
291
288
-
The results are as such:
289
-
290
-
1) The user has put their name as `Tammy` in the request parameters and no preferences: 
291
-
2) The user has put their name as `Johnny` in the request parameters and has a preference for world, business, and science news: 
292
-
293
292
The different subviews such as `worldNews.jsp`, `businessNews.jsp`, etc. are included conditionally based on the request parameters.
294
293
295
294
**How To Use**
@@ -302,12 +301,6 @@ Under `Run` and `edit configurations` Make sure Tomcat server is one of the run
302
301
303
302
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.
304
303
305
-
## Class diagram
306
-
307
-

308
-
309
-
The class diagram here displays the Javabean which is the view manager. The views are JSP's held inside the web directory.
310
-
311
304
## Applicability:
312
305
313
306
Use the Composite View design pattern when:
@@ -316,6 +309,10 @@ Use the Composite View design pattern when:
316
309
* You expect that the composite structures might include any new components in the future.
317
310
* 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.
* 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:
335
332
336
333
## Related Patterns
337
334
338
-
*[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.
339
-
*[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.
340
-
*[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/): General structural pattern that is the foundation for Composite View, used for treating individual objects and compositions uniformly.
336
+
*[Decorator](https://java-design-patterns.com/patterns/decorator/): Enhances the behavior of individual views without modifying the underlying view.
337
+
*[Flyweight](https://java-design-patterns.com/patterns/flyweight/): Can be used to manage memory consumption of large numbers of similar view objects.
338
+
* View Helper: Separates the view logic from business logic, aiding in the clean organization and management of view components.
0 commit comments