|
| 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 | + |
| 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) |
0 commit comments