Skip to content

Commit

Permalink
Merge branch 'iluwatar:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
shahdhoss authored Dec 14, 2024
2 parents a07a7a0 + 8938054 commit 1879624
Show file tree
Hide file tree
Showing 88 changed files with 6,313 additions and 37 deletions.
27 changes: 27 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -3285,6 +3285,33 @@
"contributions": [
"doc"
]
},
{
"login": "SalmaAzeem",
"name": "Salma",
"avatar_url": "https://avatars.githubusercontent.com/u/121863224?v=4",
"profile": "https://github.com/SalmaAzeem",
"contributions": [
"code"
]
},
{
"login": "CodeMaverick-143",
"name": "Arpit Sarang",
"avatar_url": "https://avatars.githubusercontent.com/u/182847716?v=4",
"profile": "https://codemaverick-143.github.io/My-Portfolio/",
"contributions": [
"code"
]
},
{
"login": "mayatarek",
"name": "Maya",
"avatar_url": "https://avatars.githubusercontent.com/u/111644421?v=4",
"profile": "https://github.com/mayatarek",
"contributions": [
"translation"
]
}
],
"contributorsPerLine": 6,
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=coverage)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
[![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)
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-360-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-362-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

<br/>
Expand Down Expand Up @@ -540,6 +540,11 @@ This project is licensed under the terms of the MIT license.
<td align="center" valign="top" width="16.66%"><a href="https://github.com/proceane"><img src="https://avatars.githubusercontent.com/u/62143949?v=4?s=100" width="100px;" alt="Juyeon"/><br /><sub><b>Juyeon</b></sub></a><br /><a href="#translation-proceane" title="Translation">🌍</a></td>
<td align="center" valign="top" width="16.66%"><a href="https://mammadyahya.vercel.app"><img src="https://avatars.githubusercontent.com/u/66476643?v=4?s=100" width="100px;" alt="Mammad Yahyayev"/><br /><sub><b>Mammad Yahyayev</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=mammadyahyayev" title="Documentation">📖</a></td>
</tr>
<tr>
<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>
<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>
<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>
</tr>
</tbody>
</table>

Expand Down
60 changes: 46 additions & 14 deletions composite-view/web/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,53 @@
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<head>
<title>Composite Patterns Mock News Site</title>
<style>
h1 { text-align: center;}
h2 { text-align: center;}
h3 { text-align: center;}
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
}
h1, h2, h3 {
text-align: center;
color: #333;
}
h1 {
font-size: 2.5em;
}
h2 {
font-size: 2em;
margin-top: 20px;
}
h3 {
font-size: 1.5em;
margin-top: 10px;
}
.parameters {
max-width: 600px;
margin: 0 auto;
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
</style>
</head>
<body>
</head>
<body>
<h1>Welcome To The Composite Patterns Mock News Site</h1>
<h2>Send a GET request to the "/news" path to see the composite view with mock news</h2>
<h2>Use the following parameters:</h2>
<h3>name: string name to be dynamically displayed</h3>
<h3>bus: boolean for whether you want to see the mock business news</h3>
<h3>world: boolean for whether you want to see the mock world news</h3>
<h3>sci: boolean for whether you want to see the mock world news</h3>
<h3>sport: boolean for whether you want to see the mock world news</h3>
</body>
<div class="parameters">
<h2>Send a GET request to the "/news" path to see the composite view with mock news</h2>
<h2>Use the following parameters:</h2>
<h3>name: <em>string</em> - Your name to be dynamically displayed</h3>
<h3>bus: <em>boolean</em> - Set to true to see mock business news</h3>
<h3>world: <em>boolean</em> - Set to true to see mock world news</h3>
<h3>sci: <em>boolean</em> - Set to true to see mock science news</h3>
<h3>sport: <em>boolean</em> - Set to true to see mock sports news</h3>
<h2>Example Request:</h2>
<h3>/news?name=John&bus=true&world=false&sci=true&sport=false</h3>
<h3>If the request fails, ensure you have the correct parameters and try again.</h3>
</div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ public void onEvent(Event e) {

@Override
public void timePasses(Weekday day) {
// This method is intentionally left empty because KingsHand does not handle time-based events directly.
// It serves as a placeholder to fulfill the EventObserver interface contract.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/**
* Represents system that is aware of threats that are present in it.
*/
public interface ThreatAwareSystem {
public interface ThreatAwareSystem<T extends Threat> {

/**
* Returns the system id.
Expand All @@ -43,13 +43,13 @@ public interface ThreatAwareSystem {
* Returns list of threats for this system.
* @return list of threats for this system.
*/
List<? extends Threat> threats();
List<T> threats();

/**
* Returns the instance of {@link Filterer} helper interface that allows to covariantly
* specify lower bound for predicate that we want to filter by.
* @return an instance of {@link Filterer} helper interface.
*/
Filterer<? extends ThreatAwareSystem, ? extends Threat> filtered();
Filterer<ThreatAwareSystem<T>, T> filtered();

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
*/
package com.iluwatar.gameloop;

import static org.junit.jupiter.api.Assertions.assertFalse;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -46,6 +45,7 @@ void setup() {
gameLoop = new GameLoop() {
@Override
protected void processGameLoop() {
throw new UnsupportedOperationException("Not supported yet.");
}
};
}
Expand Down
188 changes: 188 additions & 0 deletions localization/ar/abstract-document/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
---
title: Abstract Document
shortTitle: Abstract Document
category: Structural
language: ar
tag:
- Extensibility
---


## الهدف

استخدام الخصائص الديناميكية والحصول على مرونة اللغات غير المتغيرة مع الحفاظ على أمان الأنواع.

## التوضيح

يتيح استخدام نمط الوثيقة المجردة إدارة الخصائص غير الثابتة الإضافية. يستخدم هذا النمط مفهوم
السمات لتمكين أمان الأنواع وخصائص مفصولة من فئات مختلفة في مجموعة من الواجهات.

مثال من العالم الحقيقي

> خذ على سبيل المثال سيارة مكونة من العديد من الأجزاء. ومع ذلك، لا نعرف إذا كانت السيارة تحتوي على جميع الأجزاء أو جزء منها فقط. سياراتنا ديناميكية ومرنة للغاية.
بصيغة أخرى

> يسمح نمط الوثيقة المجردة بإضافة خصائص إلى الكائنات دون أن تكون هذه الكائنات على دراية بذلك.
حسب ويكيبيديا

> نمط تصميم هيكلي موجه للكائنات لتنظيم الكائنات في حاويات من نوع مفتاح-قيمة بشكل فضفاض مع نوعية غير محددة، وكشف البيانات باستخدام طرق عرض مهيكلة. الهدف من هذا النمط هو تحقيق درجة عالية من المرونة بين المكونات في لغة قوية النوع حيث يمكن إضافة خصائص جديدة إلى شجرة الكائنات أثناء العمل دون فقدان دعم أمان الأنواع. يستخدم النمط السمات لفصل خصائص مختلفة للفئة إلى واجهات متعددة.
**مثال برمجي**

أولاً، دعونا نعرف الفئات الأساسية `Document` و `AbstractDocument`. في الأساس، تجعل الكائن يحتوي على خريطة من الخصائص وأي عدد من الكائنات الفرعية.


```java
public interface Document {

Void put(String key, Object value);

Object get(String key);

<T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor);
}

public abstract class AbstractDocument implements Document {

private final Map<String, Object> properties;

protected AbstractDocument(Map<String, Object> properties) {
Objects.requireNonNull(properties, "properties map is required");
this.properties = properties;
}

@Override
public Void put(String key, Object value) {
properties.put(key, value);
return null;
}

@Override
public Object get(String key) {
return properties.get(key);
}

@Override
public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
return Stream.ofNullable(get(key))
.filter(Objects::nonNull)
.map(el -> (List<Map<String, Object>>) el)
.findAny()
.stream()
.flatMap(Collection::stream)
.map(constructor);
}
...
}
```

بعد ذلك، نعرف `enum` لـ `Property` ومجموعة من الواجهات للنمط، السعر، النموذج، والأجزاء. هذا يتيح لنا إنشاء واجهات تظهر بشكل ثابت لفئة `Car`.


```java
public enum Property {

PARTS, TYPE, PRICE, MODEL
}

public interface HasType extends Document {

default Optional<String> getType() {
return Optional.ofNullable((String) get(Property.TYPE.toString()));
}
}

public interface HasPrice extends Document {

default Optional<Number> getPrice() {
return Optional.ofNullable((Number) get(Property.PRICE.toString()));
}
}
public interface HasModel extends Document {

default Optional<String> getModel() {
return Optional.ofNullable((String) get(Property.MODEL.toString()));
}
}

public interface HasParts extends Document {

default Stream<Part> getParts() {
return children(Property.PARTS.toString(), Part::new);
}
}
```

Ahora estamos listos para introducir el Coche `Car`.

```java
public class Car extends AbstractDocument implements HasModel, HasPrice, HasParts {

public Car(Map<String, Object> properties) {
super(properties);
}
}
```

وأخيرًا، هكذا نبني ونستخدم السيارة `Car` في مثال كامل.


```java
LOGGER.info("Constructing parts and car");

var wheelProperties = Map.of(
Property.TYPE.toString(), "wheel",
Property.MODEL.toString(), "15C",
Property.PRICE.toString(), 100L);

var doorProperties = Map.of(
Property.TYPE.toString(), "door",
Property.MODEL.toString(), "Lambo",
Property.PRICE.toString(), 300L);

var carProperties = Map.of(
Property.MODEL.toString(), "300SL",
Property.PRICE.toString(), 10000L,
Property.PARTS.toString(), List.of(wheelProperties, doorProperties));

var car = new Car(carProperties);

LOGGER.info("Here is our car:");
LOGGER.info("-> model: {}", car.getModel().orElseThrow());
LOGGER.info("-> price: {}", car.getPrice().orElseThrow());
LOGGER.info("-> parts: ");
car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}",
p.getType().orElse(null),
p.getModel().orElse(null),
p.getPrice().orElse(null))
);

// Constructing parts and car
// Here is our car:
// model: 300SL
// price: 10000
// parts:
// wheel/15C/100
// door/Lambo/300
```

## Diagrama de clases

![alt text](./etc/abstract-document.png "Abstract Document Traits and Domain")

## التطبيق

استخدم نمط الوثيقة المجردة عندما:

* يوجد حاجة لإضافة خصائص أثناء العمل.
* ترغب في طريقة مرنة لتنظيم النطاق في هيكل مشابه لشجرة.
* ترغب في نظام أقل ترابطًا.

## الحقوق


* [Wikipedia: Abstract Document Pattern](https://en.wikipedia.org/wiki/Abstract_Document_Pattern)
* [Martin Fowler: Dealing with properties](http://martinfowler.com/apsupp/properties.pdf)
* [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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1879624

Please sign in to comment.