Skip to content

Commit 60cd40e

Browse files
authored
Merge branch 'master' into master
2 parents 9ea484e + 8938054 commit 60cd40e

File tree

78 files changed

+6271
-15
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+6271
-15
lines changed

.all-contributorsrc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3294,6 +3294,24 @@
32943294
"contributions": [
32953295
"code"
32963296
]
3297+
},
3298+
{
3299+
"login": "CodeMaverick-143",
3300+
"name": "Arpit Sarang",
3301+
"avatar_url": "https://avatars.githubusercontent.com/u/182847716?v=4",
3302+
"profile": "https://codemaverick-143.github.io/My-Portfolio/",
3303+
"contributions": [
3304+
"code"
3305+
]
3306+
},
3307+
{
3308+
"login": "mayatarek",
3309+
"name": "Maya",
3310+
"avatar_url": "https://avatars.githubusercontent.com/u/111644421?v=4",
3311+
"profile": "https://github.com/mayatarek",
3312+
"contributions": [
3313+
"translation"
3314+
]
32973315
}
32983316
],
32993317
"contributorsPerLine": 6,

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=coverage)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
77
[![Join the chat at https://gitter.im/iluwatar/java-design-patterns](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
88
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
9-
[![All Contributors](https://img.shields.io/badge/all_contributors-361-orange.svg?style=flat-square)](#contributors-)
9+
[![All Contributors](https://img.shields.io/badge/all_contributors-362-orange.svg?style=flat-square)](#contributors-)
1010
<!-- ALL-CONTRIBUTORS-BADGE:END -->
1111

1212
<br/>
@@ -542,6 +542,8 @@ This project is licensed under the terms of the MIT license.
542542
</tr>
543543
<tr>
544544
<td align="center" valign="top" width="16.66%"><a href="https://github.com/SalmaAzeem"><img src="https://avatars.githubusercontent.com/u/121863224?v=4?s=100" width="100px;" alt="Salma"/><br /><sub><b>Salma</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=SalmaAzeem" title="Code">💻</a></td>
545+
<td align="center" valign="top" width="16.66%"><a href="https://codemaverick-143.github.io/My-Portfolio/"><img src="https://avatars.githubusercontent.com/u/182847716?v=4?s=100" width="100px;" alt="Arpit Sarang"/><br /><sub><b>Arpit Sarang</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=CodeMaverick-143" title="Code">💻</a></td>
546+
<td align="center" valign="top" width="16.66%"><a href="https://github.com/mayatarek"><img src="https://avatars.githubusercontent.com/u/111644421?v=4?s=100" width="100px;" alt="Maya"/><br /><sub><b>Maya</b></sub></a><br /><a href="#translation-mayatarek" title="Translation">🌍</a></td>
545547
</tr>
546548
</tbody>
547549
</table>

composite-view/web/index.jsp

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,53 @@
2626
--%>
2727
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2828
<html>
29-
<head>
29+
<head>
30+
<title>Composite Patterns Mock News Site</title>
3031
<style>
31-
h1 { text-align: center;}
32-
h2 { text-align: center;}
33-
h3 { text-align: center;}
32+
body {
33+
font-family: Arial, sans-serif;
34+
background-color: #f4f4f4;
35+
margin: 0;
36+
padding: 20px;
37+
}
38+
h1, h2, h3 {
39+
text-align: center;
40+
color: #333;
41+
}
42+
h1 {
43+
font-size: 2.5em;
44+
}
45+
h2 {
46+
font-size: 2em;
47+
margin-top: 20px;
48+
}
49+
h3 {
50+
font-size: 1.5em;
51+
margin-top: 10px;
52+
}
53+
.parameters {
54+
max-width: 600px;
55+
margin: 0 auto;
56+
background: #fff;
57+
padding: 20px;
58+
border-radius: 8px;
59+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
60+
}
3461
</style>
35-
</head>
36-
<body>
62+
</head>
63+
<body>
3764
<h1>Welcome To The Composite Patterns Mock News Site</h1>
38-
<h2>Send a GET request to the "/news" path to see the composite view with mock news</h2>
39-
<h2>Use the following parameters:</h2>
40-
<h3>name: string name to be dynamically displayed</h3>
41-
<h3>bus: boolean for whether you want to see the mock business news</h3>
42-
<h3>world: boolean for whether you want to see the mock world news</h3>
43-
<h3>sci: boolean for whether you want to see the mock world news</h3>
44-
<h3>sport: boolean for whether you want to see the mock world news</h3>
45-
</body>
65+
<div class="parameters">
66+
<h2>Send a GET request to the "/news" path to see the composite view with mock news</h2>
67+
<h2>Use the following parameters:</h2>
68+
<h3>name: <em>string</em> - Your name to be dynamically displayed</h3>
69+
<h3>bus: <em>boolean</em> - Set to true to see mock business news</h3>
70+
<h3>world: <em>boolean</em> - Set to true to see mock world news</h3>
71+
<h3>sci: <em>boolean</em> - Set to true to see mock science news</h3>
72+
<h3>sport: <em>boolean</em> - Set to true to see mock sports news</h3>
73+
<h2>Example Request:</h2>
74+
<h3>/news?name=John&bus=true&world=false&sci=true&sport=false</h3>
75+
<h3>If the request fails, ensure you have the correct parameters and try again.</h3>
76+
</div>
77+
</body>
4678
</html>
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
---
2+
title: Abstract Document
3+
shortTitle: Abstract Document
4+
category: Structural
5+
language: ar
6+
tag:
7+
- Extensibility
8+
---
9+
10+
11+
## الهدف
12+
13+
استخدام الخصائص الديناميكية والحصول على مرونة اللغات غير المتغيرة مع الحفاظ على أمان الأنواع.
14+
15+
## التوضيح
16+
17+
يتيح استخدام نمط الوثيقة المجردة إدارة الخصائص غير الثابتة الإضافية. يستخدم هذا النمط مفهوم
18+
السمات لتمكين أمان الأنواع وخصائص مفصولة من فئات مختلفة في مجموعة من الواجهات.
19+
20+
مثال من العالم الحقيقي
21+
22+
> خذ على سبيل المثال سيارة مكونة من العديد من الأجزاء. ومع ذلك، لا نعرف إذا كانت السيارة تحتوي على جميع الأجزاء أو جزء منها فقط. سياراتنا ديناميكية ومرنة للغاية.
23+
24+
بصيغة أخرى
25+
26+
> يسمح نمط الوثيقة المجردة بإضافة خصائص إلى الكائنات دون أن تكون هذه الكائنات على دراية بذلك.
27+
28+
حسب ويكيبيديا
29+
30+
> نمط تصميم هيكلي موجه للكائنات لتنظيم الكائنات في حاويات من نوع مفتاح-قيمة بشكل فضفاض مع نوعية غير محددة، وكشف البيانات باستخدام طرق عرض مهيكلة. الهدف من هذا النمط هو تحقيق درجة عالية من المرونة بين المكونات في لغة قوية النوع حيث يمكن إضافة خصائص جديدة إلى شجرة الكائنات أثناء العمل دون فقدان دعم أمان الأنواع. يستخدم النمط السمات لفصل خصائص مختلفة للفئة إلى واجهات متعددة.
31+
32+
**مثال برمجي**
33+
34+
أولاً، دعونا نعرف الفئات الأساسية `Document` و `AbstractDocument`. في الأساس، تجعل الكائن يحتوي على خريطة من الخصائص وأي عدد من الكائنات الفرعية.
35+
36+
37+
```java
38+
public interface Document {
39+
40+
Void put(String key, Object value);
41+
42+
Object get(String key);
43+
44+
<T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor);
45+
}
46+
47+
public abstract class AbstractDocument implements Document {
48+
49+
private final Map<String, Object> properties;
50+
51+
protected AbstractDocument(Map<String, Object> properties) {
52+
Objects.requireNonNull(properties, "properties map is required");
53+
this.properties = properties;
54+
}
55+
56+
@Override
57+
public Void put(String key, Object value) {
58+
properties.put(key, value);
59+
return null;
60+
}
61+
62+
@Override
63+
public Object get(String key) {
64+
return properties.get(key);
65+
}
66+
67+
@Override
68+
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
69+
return Stream.ofNullable(get(key))
70+
.filter(Objects::nonNull)
71+
.map(el -> (List<Map<String, Object>>) el)
72+
.findAny()
73+
.stream()
74+
.flatMap(Collection::stream)
75+
.map(constructor);
76+
}
77+
...
78+
}
79+
```
80+
81+
بعد ذلك، نعرف `enum` لـ `Property` ومجموعة من الواجهات للنمط، السعر، النموذج، والأجزاء. هذا يتيح لنا إنشاء واجهات تظهر بشكل ثابت لفئة `Car`.
82+
83+
84+
```java
85+
public enum Property {
86+
87+
PARTS, TYPE, PRICE, MODEL
88+
}
89+
90+
public interface HasType extends Document {
91+
92+
default Optional<String> getType() {
93+
return Optional.ofNullable((String) get(Property.TYPE.toString()));
94+
}
95+
}
96+
97+
public interface HasPrice extends Document {
98+
99+
default Optional<Number> getPrice() {
100+
return Optional.ofNullable((Number) get(Property.PRICE.toString()));
101+
}
102+
}
103+
public interface HasModel extends Document {
104+
105+
default Optional<String> getModel() {
106+
return Optional.ofNullable((String) get(Property.MODEL.toString()));
107+
}
108+
}
109+
110+
public interface HasParts extends Document {
111+
112+
default Stream<Part> getParts() {
113+
return children(Property.PARTS.toString(), Part::new);
114+
}
115+
}
116+
```
117+
118+
Ahora estamos listos para introducir el Coche `Car`.
119+
120+
```java
121+
public class Car extends AbstractDocument implements HasModel, HasPrice, HasParts {
122+
123+
public Car(Map<String, Object> properties) {
124+
super(properties);
125+
}
126+
}
127+
```
128+
129+
وأخيرًا، هكذا نبني ونستخدم السيارة `Car` في مثال كامل.
130+
131+
132+
```java
133+
LOGGER.info("Constructing parts and car");
134+
135+
var wheelProperties = Map.of(
136+
Property.TYPE.toString(), "wheel",
137+
Property.MODEL.toString(), "15C",
138+
Property.PRICE.toString(), 100L);
139+
140+
var doorProperties = Map.of(
141+
Property.TYPE.toString(), "door",
142+
Property.MODEL.toString(), "Lambo",
143+
Property.PRICE.toString(), 300L);
144+
145+
var carProperties = Map.of(
146+
Property.MODEL.toString(), "300SL",
147+
Property.PRICE.toString(), 10000L,
148+
Property.PARTS.toString(), List.of(wheelProperties, doorProperties));
149+
150+
var car = new Car(carProperties);
151+
152+
LOGGER.info("Here is our car:");
153+
LOGGER.info("-> model: {}", car.getModel().orElseThrow());
154+
LOGGER.info("-> price: {}", car.getPrice().orElseThrow());
155+
LOGGER.info("-> parts: ");
156+
car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}",
157+
p.getType().orElse(null),
158+
p.getModel().orElse(null),
159+
p.getPrice().orElse(null))
160+
);
161+
162+
// Constructing parts and car
163+
// Here is our car:
164+
// model: 300SL
165+
// price: 10000
166+
// parts:
167+
// wheel/15C/100
168+
// door/Lambo/300
169+
```
170+
171+
## Diagrama de clases
172+
173+
![alt text](./etc/abstract-document.png "Abstract Document Traits and Domain")
174+
175+
## التطبيق
176+
177+
استخدم نمط الوثيقة المجردة عندما:
178+
179+
* يوجد حاجة لإضافة خصائص أثناء العمل.
180+
* ترغب في طريقة مرنة لتنظيم النطاق في هيكل مشابه لشجرة.
181+
* ترغب في نظام أقل ترابطًا.
182+
183+
## الحقوق
184+
185+
186+
* [Wikipedia: Abstract Document Pattern](https://en.wikipedia.org/wiki/Abstract_Document_Pattern)
187+
* [Martin Fowler: Dealing with properties](http://martinfowler.com/apsupp/properties.pdf)
188+
* [Pattern-Oriented Software Architecture Volume 4: A Pattern Language for Distributed Computing (v. 4)](https://www.amazon.com/gp/product/0470059028/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0470059028&linkId=e3aacaea7017258acf184f9f3283b492)
Loading

0 commit comments

Comments
 (0)