Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
QZero_0
java-design-patterns
提交
374f4ae4
J
java-design-patterns
项目概览
QZero_0
/
java-design-patterns
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
java-design-patterns
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
374f4ae4
编写于
1月 18, 2022
作者:
I
Ilkka Seppälä
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
readme fixes
上级
58ce3a2a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
26 addition
and
9 deletion
+26
-9
composite-view/README.md
composite-view/README.md
+26
-9
未找到文件。
composite-view/README.md
浏览文件 @
374f4ae4
...
...
@@ -10,26 +10,28 @@ tags:
-
Presentation
---
## Name
**Composite View**
## Intent
The purpose of the Composite View Pattern is to increase re-usability and flexibility when creating views for websites/webapps.
This pattern seeks to decouple the content of the page from its layout, allowing changes to be made to either the content
or layout of the page without impacting the other. This pattern also allows content to be easily reused across different views easily.
## 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.
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
...
...
@@ -42,6 +44,7 @@ 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+.
```
java
public
class
AppServlet
extends
HttpServlet
{
private
String
msgPartOne
=
"<h1>This Server Doesn't Support"
;
...
...
@@ -91,12 +94,13 @@ public class AppServlet extends HttpServlet {
}
}
```
This servlet is not part of the pattern, and simply forwards GET requests to the correct JSP.
PUT, POST, and DELETE requests are not supported and will simply show an error message.
The view management in this example is done via a javabean class:
`ClientPropertiesBean`
, which stores user preferences.
```
java
public
class
ClientPropertiesBean
implements
Serializable
{
...
...
@@ -136,11 +140,13 @@ public class ClientPropertiesBean implements Serializable {
// getters and setters generated by Lombok
}
```
This javabean has a default constructor, and another that takes an
`HttpServletRequest`
.
This second constructor takes the request object, parses out the request parameters which contain the
user preferences for different types of news.
The template for the news page is in
`newsDisplay.jsp`
```
html
<html>
<head>
...
...
@@ -198,6 +204,7 @@ The template for the news page is in `newsDisplay.jsp`
</body>
</html>
```
This JSP page is the template. It declares a table with three rows, with one component in the first row,
two components in the second row, and one component in the third row.
...
...
@@ -205,7 +212,9 @@ The scriplets in the file are part of the
view management strategy that include different atomic subviews based on the user preferences in the Javabean.
Here are two examples of the mock atomic subviews used in the composite:
`businessNews.jsp`
```
html
<html>
<head>
...
...
@@ -233,7 +242,9 @@ Here are two examples of the mock atomic subviews used in the composite:
</body>
</html>
```
`localNews.jsp`
```
html
<html>
<body>
...
...
@@ -259,11 +270,15 @@ Here are two examples of the mock atomic subviews used in the composite:
</body>
</html>
```
The results are as such:
1) The user has put their name as
`Tammy`
in the request parameters and no preferences:
1) The user has put their name as
`Tammy`
in the request parameters and no preferences:
![
alt text
](
etc/images/noparam.PNG
)
2) The user has put their name as
`Johnny`
in the request parameters and has a preference for world, business, and science news:
![
alt text
](
etc/images/threeparams.PNG
)
The different subviews such as
`worldNews.jsp`
,
`businessNews.jsp`
, etc. are included conditionally
...
...
@@ -304,6 +319,7 @@ that are included in the page dynamically. Most modern Javascript libraries, lik
with components.
## Consequences
**Pros**
*
Easy to re-use components
*
Change layout/content without affecting the other
...
...
@@ -316,10 +332,11 @@ with components.
*
Increases potential for display errors
## Related patterns
*
[
Composite (GoF)
](
https://java-design-patterns.com/patterns/composite/
)
*
[
View Helper
](
https://www.oracle.com/java/technologies/viewhelper.html
)
## 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/
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录