translation: Added Hindi Translation (#2557)

* Added Hindi Translation for adapter, aggregator, ambassador, api-gateway and arrange-act-assert pattern

* Added hndi translation for async-method-invocation, balking, bridge, builder and buisness-delegate pattern

* Added hindi translation for bytecode, caching, chain-of-responsibility, citcuit-breaker pattern

* Added hindi translation for client-session, collecting-parameter, collection-pipeline, combinator, command pattern
This commit is contained in:
Surjendu
2023-08-20 23:22:11 +05:30
committed by GitHub
parent 82fcbeaee5
commit c983af6003
20 changed files with 3539 additions and 0 deletions
+137
View File
@@ -0,0 +1,137 @@
---
title: Adapter
category: Structural
language: hi
tag:
- Gang of Four
---
## दूसरा नाम
आवरण
## हेतु
किसी क्लास के इंटरफ़ेस को क्लाइंट द्वारा अपेक्षित किसी अन्य इंटरफ़ेस में कनवर्ट करें। एडाप्टर कक्षाओं को एक साथ काम करने देता है
असंगत इंटरफ़ेस के कारण अन्यथा नहीं हो सका।
## व्याख्या
वास्तविक दुनिया का उदाहरण
> विचार करें कि आपके मेमोरी कार्ड पर कुछ चित्र हैं और आपको उन्हें अपने कंप्यूटर पर स्थानांतरित करने की आवश्यकता है। उन्हें स्थानांतरित करने के लिए, आपको किसी प्रकार के एडाप्टर की आवश्यकता होती है जो आपके कंप्यूटर पोर्ट के साथ संगत हो ताकि आप अपने कंप्यूटर में मेमोरी कार्ड संलग्न कर सकें। इस मामले में कार्ड रीडर एक एडाप्टर है।
> एक अन्य उदाहरण प्रसिद्ध पावर एडॉप्टर होगा; तीन-पैर वाले प्लग को दो-आयामी आउटलेट से नहीं जोड़ा जा सकता है, इसके लिए एक पावर एडाप्टर का उपयोग करने की आवश्यकता होती है जो इसे दो-आयामी आउटलेट के साथ संगत बनाता है।
> एक अन्य उदाहरण एक अनुवादक का होगा जो एक व्यक्ति द्वारा दूसरे व्यक्ति द्वारा बोले गए शब्दों का अनुवाद करेगा।
सरल शब्दो मे
> एडेप्टर पैटर्न आपको किसी अन्यथा असंगत ऑब्जेक्ट को किसी अन्य वर्ग के साथ संगत बनाने के लिए एडॉप्टर में लपेटने की सुविधा देता है।
विकिपीडिया कहता है
> सॉफ्टवेयर इंजीनियरिंग में, एडॉप्टर पैटर्न एक सॉफ्टवेयर डिज़ाइन पैटर्न है जो मौजूदा क्लास के इंटरफ़ेस को दूसरे इंटरफ़ेस के रूप में उपयोग करने की अनुमति देता है। इसका उपयोग अक्सर मौजूदा कक्षाओं को उनके स्रोत कोड को संशोधित किए बिना दूसरों के साथ काम करने के लिए किया जाता है।
**प्रोग्रामेटिक उदाहरण**
एक ऐसे कप्तान पर विचार करें जो केवल नाव चला सकता है और बिल्कुल भी नाव नहीं चला सकता।
सबसे पहले, हमारे पास इंटरफ़ेस `RowingBoat` और `FishingBoat` हैं
```java
public interface RowingBoat {
void row();
}
@Slf4j
public class FishingBoat {
public void sail() {
LOGGER.info("The fishing boat is sailing");
}
}
```
और कैप्टन को उम्मीद है कि `RowingBoat` इंटरफ़ेस का कार्यान्वयन आगे बढ़ने में सक्षम होगा
```java
public class Captain {
private final RowingBoat rowingBoat;
// default constructor and setter for rowingBoat
public Captain(RowingBoat rowingBoat) {
this.rowingBoat = rowingBoat;
}
public void row() {
rowingBoat.row();
}
}
```
अब मान लीजिए कि समुद्री डाकू आ रहे हैं और हमारे कप्तान को भागने की जरूरत है लेकिन केवल मछली पकड़ने वाली नाव उपलब्ध है। हमें एक एडॉप्टर बनाने की आवश्यकता है जो कप्तान को नाव चलाने के अपने कौशल के साथ मछली पकड़ने वाली नाव को संचालित करने की अनुमति दे।
```java
@Slf4j
public class FishingBoatAdapter implements RowingBoat {
private final FishingBoat boat;
public FishingBoatAdapter() {
boat = new FishingBoat();
}
@Override
public void row() {
boat.sail();
}
}
```
और अब `Captain` समुद्री डाकुओं से बचने के लिए `FishingBoat` का उपयोग कर सकता है
```java
var captain = new Captain(new FishingBoatAdapter());
captain.row();
```
## क्लास डायग्राम
![alt text](../../../adapter/etc/adapter.urm.png "एडाप्टर क्लास डायग्राम")
## प्रयोज्यता
जब एडॉप्टर पैटर्न का उपयोग करें
* आप किसी मौजूदा कक्षा का उपयोग करना चाहते हैं, और इसका इंटरफ़ेस आपकी ज़रूरत से मेल नहीं खाता है।
* आप एक पुन: प्रयोज्य वर्ग बनाना चाहते हैं जो असंबद्ध या अप्रत्याशित वर्गों के साथ सहयोग करता है, अर्थात, ऐसे वर्ग जिनमें आवश्यक रूप से संगत इंटरफ़ेस नहीं है
* आपको कई मौजूदा उपवर्गों का उपयोग करने की आवश्यकता है, लेकिन सभी को उपवर्गित करके उनके इंटरफ़ेस को अनुकूलित करना अव्यावहारिक है। एक ऑब्जेक्ट एडाप्टर अपने मूल वर्ग के इंटरफ़ेस को अनुकूलित कर सकता है।
* तृतीय-पक्ष लाइब्रेरी का उपयोग करने वाले अधिकांश एप्लिकेशन लाइब्रेरी से एप्लिकेशन को अलग करने के लिए एप्लिकेशन और तृतीय पक्ष लाइब्रेरी के बीच मध्य परत के रूप में एडेप्टर का उपयोग करते हैं। यदि किसी अन्य लाइब्रेरी का उपयोग करना है तो एप्लिकेशन कोड को बदले बिना नई लाइब्रेरी के लिए केवल एक एडाप्टर की आवश्यकता होती है।
## ट्यूटोरियल
* [Dzone](https://dzone.com/articles/adapter-design-pattern-in-java)
* [Refactoring Guru](https://refactoring.guru/design-patterns/adapter/java/example)
* [Baeldung](https://www.baeldung.com/java-adapter-pattern)
## नतीजे
क्लास और ऑब्जेक्ट एडेप्टर के अलग-अलग ट्रेड-ऑफ़ होते हैं। एक क्लास एडॉप्टर
* एक ठोस एडाप्टी वर्ग के लिए प्रतिबद्ध होकर एडाप्टी को लक्ष्य के अनुसार अनुकूलित करता है। परिणामस्वरूप, जब हम किसी क्लास और उसके सभी उपवर्गों को अनुकूलित करना चाहते हैं तो क्लास एडॉप्टर काम नहीं करेगा।
* एडॉप्टर को एडैप्टी के कुछ व्यवहारों को ओवरराइड करने दें क्योंकि एडॉप्टर एडैप्टी का एक उपवर्ग है।
* केवल एक ऑब्जेक्ट का परिचय देता है, और एडाप्टी तक पहुंचने के लिए किसी अतिरिक्त सूचक संकेत की आवश्यकता नहीं होती है।
एक ऑब्जेक्ट एडाप्टर
* एक ही एडॉप्टर को कई एडाप्टीज़ के साथ काम करने देता है, यानी स्वयं एडाप्टी और उसके सभी उपवर्गों (यदि कोई हो) के साथ। एडॉप्टर एक साथ सभी एडेप्टीज़ में कार्यक्षमता भी जोड़ सकता है।
* एडाप्टी व्यवहार को ओवरराइड करना कठिन बना देता है। इसके लिए एडाप्टी को उपवर्गित करने और एडॉप्टर को एडाप्टी के बजाय उपवर्ग को संदर्भित करने की आवश्यकता होगी।
## वास्तविक दुनिया के उदाहरण
* [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29)
* [java.util.Collections#list()](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#list-java.util.Enumeration-)
* [java.util.Collections#enumeration()](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#enumeration-java.util.Collection-)
* [javax.xml.bind.annotation.adapters.XMLAdapter](http://docs.oracle.com/javase/8/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html#marshal-BoundType-)
## श्रेय
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
* [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=48d37c67fb3d845b802fa9b619ad8f31)
* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
* [Refactoring to Patterns](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7)
@@ -0,0 +1,107 @@
---
title: Aggregator Microservices
category: Architectural
language: hi
tag:
- Cloud distributed
- Decoupling
- Microservices
---
## हेतु
उपयोगकर्ता एग्रीगेटर सेवा पर एक कॉल करता है, और एग्रीगेटर फिर प्रत्येक प्रासंगिक माइक्रोसर्विस को कॉल करता है।
## व्याख्या
वास्तविक दुनिया का उदाहरण
> हमारे वेब बाज़ार को उत्पादों और उनकी वर्तमान सूची के बारे में जानकारी की आवश्यकता है। यह एक एग्रीगेटर को कॉल करता है
> सेवा जो बदले में उत्पाद जानकारी माइक्रोसर्विस और उत्पाद इन्वेंट्री माइक्रोसर्विस को कॉल करती है
> संयुक्त जानकारी.
साफ़ शब्दों में
> एग्रीगेटर माइक्रोसर्विस विभिन्न माइक्रोसर्विसेज से डेटा के टुकड़े एकत्र करता है और प्रसंस्करण के लिए एक समुच्चय लौटाता है।
स्टैक ओवरफ्लो कहता है
> एग्रीगेटर माइक्रोसर्विस एप्लिकेशन द्वारा आवश्यक कार्यक्षमता प्राप्त करने के लिए कई सेवाओं को आमंत्रित करता है।
**प्रोग्रामेटिक उदाहरण**
आइए डेटा मॉडल से शुरू करें। यहाँ हमारा `Product` है।
```java
public class Product {
private String title;
private int productInventories;
// getters and setters ->
...
}
```
आगे हम अपना `Aggregator` माइक्रोसर्विस पेश कर सकते हैं। इसमें क्लाइंट `ProductInformationClient` और शामिल हैं
संबंधित माइक्रोसर्विसेज़ को कॉल करने के लिए `ProductInventoryClient`
```java
@RestController
public class Aggregator {
@Resource
private ProductInformationClient informationClient;
@Resource
private ProductInventoryClient inventoryClient;
@RequestMapping(path = "/product", method = RequestMethod.GET)
public Product getProduct() {
var product = new Product();
var productTitle = informationClient.getProductTitle();
var productInventory = inventoryClient.getProductInventories();
//Fallback to error message
product.setTitle(requireNonNullElse(productTitle, "Error: Fetching Product Title Failed"));
//Fallback to default error inventory
product.setProductInventories(requireNonNullElse(productInventory, -1));
return product;
}
}
```
यहां सूचना माइक्रोसर्विस कार्यान्वयन का सार है। इन्वेंटरी माइक्रोसर्विस समान है, यह सिर्फ रिटर्न देता है
इन्वेंट्री मायने रखती है।
```java
@RestController
public class InformationController {
@RequestMapping(value = "/information", method = RequestMethod.GET)
public String getProductTitle() {
return "The Product Title.";
}
}
```
अब हमारे `Aggregator` REST API को कॉल करने से उत्पाद की जानकारी मिलती है।
```bash
curl http://localhost:50004/product
{"title":"The Product Title.","productInventories":5}
```
## क्लास डायग्राम
![alt text](../../../aggregator-microservices/aggregator-service/etc/aggregator-service.png "एग्रीगेटर माइक्रोसर्विस")
## प्रयोज्यता
जब आपको क्लाइंट डिवाइस की परवाह किए बिना विभिन्न माइक्रोसर्विसेज के लिए एकीकृत एपीआई की आवश्यकता हो तो एग्रीगेटर माइक्रोसर्विसेज पैटर्न का उपयोग करें।
## श्रेय
* [Microservice Design Patterns](http://web.archive.org/web/20190705163602/http://blog.arungupta.me/microservice-design-patterns/)
* [Microservices Patterns: With examples in Java](https://www.amazon.com/gp/product/1617294543/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1617294543&linkId=8b4e570267bc5fb8b8189917b461dc60)
* [Architectural Patterns: Uncover essential patterns in the most indispensable realm of enterprise architecture](https://www.amazon.com/gp/product/B077T7V8RC/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=B077T7V8RC&linkId=c34d204bfe1b277914b420189f09c1a4)
+202
View File
@@ -0,0 +1,202 @@
---
title: Ambassador
category: Structural
language: hi
tag:
- Decoupling
- Cloud distributed
---
## हेतु
क्लाइंट पर एक सहायक सेवा उदाहरण प्रदान करें और साझा संसाधन से सामान्य कार्यक्षमता को दूर रखें।
## व्याख्या
वास्तविक दुनिया का उदाहरण
> एक दूरस्थ सेवा में कई ग्राहक उसके द्वारा प्रदान किए गए फ़ंक्शन तक पहुँच प्राप्त करते हैं। यह सेवा एक विरासती एप्लिकेशन है और है
> अद्यतन करना असंभव है. उपयोगकर्ताओं की ओर से बड़ी संख्या में अनुरोधों के कारण कनेक्टिविटी संबंधी समस्याएं उत्पन्न हो रही हैं। अनुरोध के लिए नए नियम
> आवृत्ति को विलंबता जांच और क्लाइंट-साइड लॉगिंग के साथ लागू किया जाना चाहिए।
साफ़ शब्दों में
> एंबेसेडर पैटर्न के साथ, हम विलंबता जांच के साथ-साथ ग्राहकों से कम-बार-बार मतदान लागू कर सकते हैं
> लॉगिंग.
माइक्रोसॉफ्ट दस्तावेज़ बताता है
> एक राजदूत सेवा को एक आउट-ऑफ-प्रोसेस प्रॉक्सी के रूप में सोचा जा सकता है जो क्लाइंट के साथ सह-स्थित होती है। यह पैटर्न
> मॉनिटरिंग, लॉगिंग, रूटिंग जैसे सामान्य क्लाइंट कनेक्टिविटी कार्यों को ऑफ़लोड करने के लिए उपयोगी हो सकता है।
> सुरक्षा (जैसे टीएलएस), और भाषा अज्ञेयवादी तरीके से लचीलापन पैटर्न। इसका उपयोग अक्सर पुराने अनुप्रयोगों के साथ किया जाता है,
> या अन्य एप्लिकेशन जिन्हें अपनी नेटवर्किंग क्षमताओं को बढ़ाने के लिए संशोधित करना मुश्किल है। यह भी हो सकता है
> उन सुविधाओं को लागू करने के लिए एक विशेष टीम को सक्षम करें।
**प्रोग्रामेटिक उदाहरण**
उपरोक्त परिचय को ध्यान में रखते हुए हम इस उदाहरण में कार्यक्षमता का अनुकरण करेंगे। हमने एक इंटरफ़ेस लागू किया है
दूरस्थ सेवा के साथ-साथ राजदूत सेवा द्वारा:
```java
interface RemoteServiceInterface {
long doRemoteFunction(int value) throws Exception;
}
```
एक दूरस्थ सेवा को सिंगलटन के रूप में दर्शाया गया है।
```java
@Slf4j
public class RemoteService implements RemoteServiceInterface {
private static RemoteService service = null;
static synchronized RemoteService getRemoteService() {
if (service == null) {
service = new RemoteService();
}
return service;
}
private RemoteService() {}
@Override
public long doRemoteFunction(int value) {
long waitTime = (long) Math.floor(Math.random() * 1000);
try {
sleep(waitTime);
} catch (InterruptedException e) {
LOGGER.error("Thread sleep interrupted", e);
}
return waitTime >= 200 ? value * 10 : -1;
}
}
```
एक सेवा राजदूत लॉगिंग, विलंबता जांच जैसी अतिरिक्त सुविधाएं जोड़ रहा है
```java
@Slf4j
public class ServiceAmbassador implements RemoteServiceInterface {
private static final int RETRIES = 3;
private static final int DELAY_MS = 3000;
ServiceAmbassador() {
}
@Override
public long doRemoteFunction(int value) {
return safeCall(value);
}
private long checkLatency(int value) {
var startTime = System.currentTimeMillis();
var result = RemoteService.getRemoteService().doRemoteFunction(value);
var timeTaken = System.currentTimeMillis() - startTime;
LOGGER.info("Time taken (ms): " + timeTaken);
return result;
}
private long safeCall(int value) {
var retries = 0;
var result = (long) FAILURE;
for (int i = 0; i < RETRIES; i++) {
if (retries >= RETRIES) {
return FAILURE;
}
if ((result = checkLatency(value)) == FAILURE) {
LOGGER.info("Failed to reach remote: (" + (i + 1) + ")");
retries++;
try {
sleep(DELAY_MS);
} catch (InterruptedException e) {
LOGGER.error("Thread sleep state interrupted", e);
}
} else {
break;
}
}
return result;
}
}
```
एक ग्राहक के पास एक स्थानीय सेवा राजदूत होता है जिसका उपयोग दूरस्थ सेवा के साथ बातचीत करने के लिए किया जाता है:
```java
@Slf4j
public class Client {
private final ServiceAmbassador serviceAmbassador = new ServiceAmbassador();
long useService(int value) {
var result = serviceAmbassador.doRemoteFunction(value);
LOGGER.info("Service result: " + result);
return result;
}
}
```
यहां दो ग्राहक सेवा का उपयोग कर रहे हैं।
```java
public class App {
public static void main(String[] args) {
var host1 = new Client();
var host2 = new Client();
host1.useService(12);
host2.useService(73);
}
}
```
उदाहरण चलाने के लिए आउटपुट यहां दिया गया है:
```java
Time taken (ms): 111
Service result: 120
Time taken (ms): 931
Failed to reach remote: (1)
Time taken (ms): 665
Failed to reach remote: (2)
Time taken (ms): 538
Failed to reach remote: (3)
Service result: -1
```
## क्लास डायग्राम
![alt text](../../../ambassador/etc/ambassador.urm.png "Ambassador class diagram")
## प्रयोज्यता
एंबेसडर एक विरासती दूरस्थ सेवा के साथ काम करते समय लागू होता है जिसे संशोधित नहीं किया जा सकता है या अत्यधिक होगा
संशोधित करना कठिन. रिमोट पर बदलाव की आवश्यकता से बचते हुए क्लाइंट पर कनेक्टिविटी सुविधाएँ लागू की जा सकती हैं
सेवा।
* राजदूत दूरस्थ सेवा के लिए एक स्थानीय इंटरफ़ेस प्रदान करता है।
* एंबेसडर क्लाइंट को लॉगिंग, सर्किट ब्रेकिंग, रिट्रीज़ और सुरक्षा प्रदान करता है।
## विशिष्ट उपयोग का मामला
* किसी अन्य वस्तु तक पहुंच को नियंत्रित करें
* लॉगिंग लागू करें
* सर्किट ब्रेकिंग लागू करें
* दूरस्थ सेवा कार्यों को ऑफलोड करें
* नेटवर्क कनेक्शन की सुविधा
## ज्ञात उपयोग
* [Kubernetes-native API gateway for microservices](https://github.com/datawire/ambassador)
## संबंधित पैटर्न
* [Proxy](https://java-design-patterns.com/patterns/proxy/)
## श्रेय
* [Ambassador pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/ambassador)
* [Designing Distributed Systems: Patterns and Paradigms for Scalable, Reliable Services](https://www.amazon.com/s?k=designing+distributed+systems&sprefix=designing+distri%2Caps%2C156&linkCode=ll2&tag=javadesignpat-20&linkId=a12581e625462f9038557b01794e5341&language=en_US&ref_=as_li_ss_tl)
+166
View File
@@ -0,0 +1,166 @@
---
title: API Gateway
category: Architectural
language: hi
tag:
- Cloud distributed
- Decoupling
- Microservices
---
## हेतु
एक ही स्थान, एपीआई गेटवे पर माइक्रोसर्विसेज के लिए एकत्रित कॉल। उपयोगकर्ता एक ही कॉल करता है
एपीआई गेटवे पर, और एपीआई गेटवे फिर प्रत्येक प्रासंगिक माइक्रोसर्विस को कॉल करता है।
## व्याख्या
माइक्रोसर्विसेज पैटर्न के साथ, एक क्लाइंट को कई अलग-अलग माइक्रोसर्विसेज से डेटा की आवश्यकता हो सकती है। यदि
क्लाइंट ने प्रत्येक माइक्रोसर्विस को सीधे कॉल किया, जो लंबे समय तक लोड करने में योगदान दे सकता है
क्लाइंट को कॉल की गई प्रत्येक माइक्रोसर्विस के लिए नेटवर्क अनुरोध करना होगा। इसके अलावा, होने
क्लाइंट कॉल प्रत्येक माइक्रोसर्विस क्लाइंट को सीधे उस माइक्रोसर्विस से जोड़ती है - यदि आंतरिक है
माइक्रोसर्विसेज का कार्यान्वयन बदल जाता है (उदाहरण के लिए, यदि दो माइक्रोसर्विसेज को कभी-कभी संयोजित किया जाता है
भविष्य में) या यदि किसी माइक्रोसर्विस का स्थान (होस्ट और पोर्ट) बदलता है, तो प्रत्येक ग्राहक
उन माइक्रोसर्विसेज का उपयोग अद्यतन किया जाना चाहिए।
एपीआई गेटवे पैटर्न का इरादा इनमें से कुछ मुद्दों को कम करना है। एपीआई गेटवे में
पैटर्न, एक अतिरिक्त इकाई (एपीआई गेटवे) क्लाइंट और माइक्रोसर्विसेज के बीच रखी गई है।
एपीआई गेटवे का काम माइक्रोसर्विसेज पर कॉल को एकत्रित करना है। ग्राहक के बजाय
प्रत्येक माइक्रोसर्विस को व्यक्तिगत रूप से कॉल करने पर, क्लाइंट एपीआई गेटवे को एक बार कॉल करता है। एपीआई
गेटवे फिर प्रत्येक माइक्रोसर्विसेज को कॉल करता है जिनकी ग्राहक को आवश्यकता होती है।
वास्तविक दुनिया का उदाहरण
> हम एक ई-कॉमर्स साइट के लिए माइक्रोसर्विसेज और एपीआई गेटवे पैटर्न लागू कर रहे हैं। इस व्यवस्था में
> एपीआई गेटवे इमेज और प्राइस माइक्रोसर्विसेज को कॉल करता है।
साफ़ शब्दों में
> माइक्रोसर्विसेज आर्किटेक्चर का उपयोग करके कार्यान्वित सिस्टम के लिए, एपीआई गेटवे एकल प्रवेश बिंदु है
> जो व्यक्तिगत माइक्रोसर्विसेज के लिए कॉलों को एकत्रित करता है।
विकिपीडिया कहता है
> एपीआई गेटवे एक सर्वर है जो एपीआई फ्रंट-एंड के रूप में कार्य करता है, एपीआई अनुरोध प्राप्त करता है, थ्रॉटलिंग लागू करता है
> और सुरक्षा नीतियां, अनुरोधों को बैक-एंड सेवा तक भेजती हैं और फिर प्रतिक्रिया को वापस भेजती हैं
> अनुरोधकर्ता को. एक गेटवे में अक्सर व्यवस्थित करने और संशोधित करने के लिए एक परिवर्तन इंजन शामिल होता है
> तुरंत अनुरोध और प्रतिक्रियाएँ। एक गेटवे संग्रहण जैसी कार्यक्षमता भी प्रदान कर सकता है
> एनालिटिक्स डेटा और कैशिंग प्रदान करना। गेटवे समर्थन के लिए कार्यक्षमता प्रदान कर सकता है
> प्रमाणीकरण, प्राधिकरण, सुरक्षा, ऑडिट और नियामक अनुपालन।
**प्रोग्रामेटिक उदाहरण**
यह कार्यान्वयन दिखाता है कि ई-कॉमर्स साइट के लिए एपीआई गेटवे पैटर्न कैसा दिख सकता है।
`ApiGateway` `ImageClientImpl` का उपयोग करके इमेज और प्राइस माइक्रोसर्विसेज को कॉल करता है और
क्रमशः `PriceClientImpl`। डेस्कटॉप डिवाइस पर साइट देखने वाले ग्राहक दोनों कीमतें देख सकते हैं
जानकारी और उत्पाद की एक छवि, इसलिए `ApiGateway` दोनों माइक्रोसर्विसेज को कॉल करता है
डेटा को `DesktopProduct` मॉडल में एकत्रित करता है। हालाँकि, मोबाइल उपयोगकर्ता केवल कीमत की जानकारी देखते हैं;
उन्हें उत्पाद की छवि नहीं दिखती. मोबाइल उपयोगकर्ताओं के लिए, `ApiGateway` केवल मूल्य प्राप्त करता है
जानकारी, जिसका उपयोग यह `MobileProduct` को पॉप्युलेट करने के लिए करता है।
यहां इमेज माइक्रोसर्विस कार्यान्वयन है।
```java
public interface ImageClient {
String getImagePath();
}
public class ImageClientImpl implements ImageClient {
@Override
public String getImagePath() {
var httpClient = HttpClient.newHttpClient();
var httpGet = HttpRequest.newBuilder()
.GET()
.uri(URI.create("http://localhost:50005/image-path"))
.build();
try {
var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
return httpResponse.body();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return null;
}
}
```
यहां प्राइस माइक्रोसर्विस कार्यान्वयन है।
```java
public interface PriceClient {
String getPrice();
}
public class PriceClientImpl implements PriceClient {
@Override
public String getPrice() {
var httpClient = HttpClient.newHttpClient();
var httpGet = HttpRequest.newBuilder()
.GET()
.uri(URI.create("http://localhost:50006/price"))
.build();
try {
var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
return httpResponse.body();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return null;
}
}
```
यहां हम देख सकते हैं कि एपीआई गेटवे माइक्रोसर्विसेज के अनुरोधों को कैसे मैप करता है।
```java
public class ApiGateway {
@Resource
private ImageClient imageClient;
@Resource
private PriceClient priceClient;
@RequestMapping(path = "/desktop", method = RequestMethod.GET)
public DesktopProduct getProductDesktop() {
var desktopProduct = new DesktopProduct();
desktopProduct.setImagePath(imageClient.getImagePath());
desktopProduct.setPrice(priceClient.getPrice());
return desktopProduct;
}
@RequestMapping(path = "/mobile", method = RequestMethod.GET)
public MobileProduct getProductMobile() {
var mobileProduct = new MobileProduct();
mobileProduct.setPrice(priceClient.getPrice());
return mobileProduct;
}
}
```
## क्लास डायग्राम
![alt text](../../../api-gateway/etc/api-gateway.png "API Gateway")
## प्रयोज्यता
एपीआई गेटवे पैटर्न का उपयोग करें जब
* आप माइक्रोसर्विसेज आर्किटेक्चर का उपयोग कर रहे हैं और आपको अपने माइक्रोसर्विसेज कॉल के लिए एकत्रीकरण के एक बिंदु की आवश्यकता है।
## ट्यूटोरियल
* [Exploring the New Spring Cloud Gateway](https://www.baeldung.com/spring-cloud-gateway)
* [Spring Cloud - Gateway](https://www.tutorialspoint.com/spring_cloud/spring_cloud_gateway.htm)
* [Getting Started With Spring Cloud Gateway](https://dzone.com/articles/getting-started-with-spring-cloud-gateway)
## श्रेय
* [microservices.io - API Gateway](http://microservices.io/patterns/apigateway.html)
* [NGINX - Building Microservices: Using an API Gateway](https://www.nginx.com/blog/building-microservices-using-an-api-gateway/)
* [Microservices Patterns: With examples in Java](https://www.amazon.com/gp/product/1617294543/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1617294543&linkId=ac7b6a57f866ac006a309d9086e8cfbd)
* [Building Microservices: Designing Fine-Grained Systems](https://www.amazon.com/gp/product/1491950358/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1491950358&linkId=4c95ca9831e05e3f0dadb08841d77bf1)
@@ -0,0 +1,143 @@
---
title: Arrange/Act/Assert
category: Idiom
language: hi
tag:
- Testing
---
## दूसरा नाम
दिया/कब/तब
## हेतु
अरेंज/एक्ट/एसर्ट (एएए) यूनिट परीक्षणों के आयोजन के लिए एक पैटर्न है।
यह परीक्षणों को तीन स्पष्ट और विशिष्ट चरणों में विभाजित करता है:
1. व्यवस्थित करें: परीक्षण के लिए आवश्यक सेटअप और आरंभीकरण करें।
2. अधिनियम: परीक्षण के लिए आवश्यक कार्रवाई करें।
3. दावा: परीक्षण के परिणाम सत्यापित करें।
## व्याख्या
इस पैटर्न के कई महत्वपूर्ण लाभ हैं. यह एक परीक्षण के बीच स्पष्ट अलगाव पैदा करता है
सेटअप, संचालन और परिणाम। यह संरचना कोड को पढ़ने और समझने में आसान बनाती है। अगर
आप चरणों को क्रम में रखते हैं और उन्हें अलग करने के लिए अपना कोड प्रारूपित करते हैं, आप एक परीक्षण स्कैन कर सकते हैं और
जल्दी से समझें कि यह क्या करता है।
जब आप अपनी परीक्षाएँ लिखते हैं तो यह कुछ हद तक अनुशासन भी लागू करता है। आपको सोचना होगा
आपके परीक्षण द्वारा निष्पादित किए जाने वाले तीन चरणों के बारे में स्पष्ट रूप से बताएं। यह परीक्षणों को लिखने के लिए अधिक स्वाभाविक बनाता है
उसी समय, चूँकि आपके पास पहले से ही एक रूपरेखा है।
वास्तविक दुनिया का उदाहरण
> हमें एक कक्षा के लिए व्यापक और स्पष्ट यूनिट टेस्ट सूट लिखने की जरूरत है।
साफ़ शब्दों में
> अरेंज/एक्ट/एसर्ट एक परीक्षण पैटर्न है जो परीक्षणों को आसान बनाने के लिए तीन स्पष्ट चरणों में व्यवस्थित करता है
> रखरखाव।
विकीविकीवेब कहता है
> अरेंज/एक्ट/एसर्ट यूनिटटेस्ट विधियों में कोड को व्यवस्थित करने और फ़ॉर्मेट करने का एक पैटर्न है।
**प्रोग्रामेटिक उदाहरण**
आइए सबसे पहले इकाई परीक्षण के लिए अपने `Cash` वर्ग का परिचय दें।
```java
public class Cash {
private int amount;
Cash(int amount) {
this.amount = amount;
}
void plus(int addend) {
amount += addend;
}
boolean minus(int subtrahend) {
if (amount >= subtrahend) {
amount -= subtrahend;
return true;
} else {
return false;
}
}
int count() {
return amount;
}
}
```
फिर हम अपने यूनिट परीक्षण को अरेंज/एक्ट/एसर्ट पैटर्न के अनुसार लिखते हैं। स्पष्ट रूप से ध्यान दें
प्रत्येक इकाई परीक्षण के लिए अलग-अलग चरण।
```java
class CashAAATest {
@Test
void testPlus() {
//Arrange
var cash = new Cash(3);
//Act
cash.plus(4);
//Assert
assertEquals(7, cash.count());
}
@Test
void testMinus() {
//Arrange
var cash = new Cash(8);
//Act
var result = cash.minus(5);
//Assert
assertTrue(result);
assertEquals(3, cash.count());
}
@Test
void testInsufficientMinus() {
//Arrange
var cash = new Cash(1);
//Act
var result = cash.minus(6);
//Assert
assertFalse(result);
assertEquals(1, cash.count());
}
@Test
void testUpdate() {
//Arrange
var cash = new Cash(5);
//Act
cash.plus(6);
var result = cash.minus(3);
//Assert
assertTrue(result);
assertEquals(8, cash.count());
}
}
```
## प्रयोज्यता
जब अरेंज/एक्ट/एसर्ट पैटर्न का उपयोग करें
* आपको अपने यूनिट परीक्षणों की संरचना करने की आवश्यकता है ताकि उन्हें पढ़ना, बनाए रखना और बढ़ाना आसान हो।
## श्रेय
* [Arrange, Act, Assert: What is AAA Testing?](https://blog.ncrunch.net/post/arrange-act-assert-aaa-testing.aspx)
* [Bill Wake: 3A Arrange, Act, Assert](https://xp123.com/articles/3a-arrange-act-assert/)
* [Martin Fowler: GivenWhenThen](https://martinfowler.com/bliki/GivenWhenThen.html)
* [xUnit Test Patterns: Refactoring Test Code](https://www.amazon.com/gp/product/0131495054/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0131495054&linkId=99701e8f4af2f7e8dd50d720c9b63dbf)
* [Unit Testing Principles, Practices, and Patterns](https://www.amazon.com/gp/product/1617296279/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1617296279&linkId=74c75cf22a63c3e4758ae08aa0a0cc35)
* [Test Driven Development: By Example](https://www.amazon.com/gp/product/0321146530/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0321146530&linkId=5c63a93d8c1175b84ca5087472ef0e05)
@@ -0,0 +1,170 @@
---
title: Async Method Invocation
category: Concurrency
language: hi
tag:
- Reactive
---
## हेतु
एसिंक्रोनस विधि मंगलाचरण एक पैटर्न है जहां कॉलिंग थ्रेड होता है
कार्यों के परिणाम की प्रतीक्षा करते समय अवरुद्ध नहीं किया जाता है। पैटर्न समानांतर प्रदान करता है
अनेक स्वतंत्र कार्यों को संसाधित करना और इनके माध्यम से परिणाम प्राप्त करना
कॉलबैक या सब कुछ पूरा होने तक प्रतीक्षा करना।
## व्याख्या
वास्तविक दुनिया का उदाहरण
>अंतरिक्ष रॉकेट लॉन्च करना एक रोमांचक व्यवसाय है। मिशन कमांड लॉन्च करने का आदेश देता है और
> कुछ अनिश्चित समय के बाद, रॉकेट या तो सफलतापूर्वक लॉन्च होता है या बुरी तरह विफल हो जाता है।
साफ़ शब्दों में
> एसिंक्रोनस विधि मंगलाचरण कार्य प्रसंस्करण शुरू करता है और कार्य पूरा होने से तुरंत पहले वापस आ जाता है
> तैयार. कार्य प्रसंस्करण के परिणाम बाद में कॉल करने वाले को लौटा दिए जाते हैं।
विकिपीडिया कहता है
> मल्टीथ्रेडेड कंप्यूटर प्रोग्रामिंग में, एसिंक्रोनस मेथड इनवोकेशन (एएमआई) के रूप में भी जाना जाता है
> एसिंक्रोनस विधि कॉल या एसिंक्रोनस पैटर्न एक डिज़ाइन पैटर्न है जिसमें कॉल साइट
> कॉल किए गए कोड के समाप्त होने की प्रतीक्षा करते समय अवरुद्ध नहीं होता है। इसके बजाय, कॉलिंग थ्रेड है
> उत्तर आने पर सूचित किया जाएगा। उत्तर के लिए मतदान एक अवांछित विकल्प है।
**प्रोग्रामेटिक उदाहरण**
इस उदाहरण में, हम अंतरिक्ष रॉकेट लॉन्च कर रहे हैं और चंद्र रोवर्स तैनात कर रहे हैं।
एप्लिकेशन एसिंक विधि मंगलाचरण पैटर्न प्रदर्शित करता है। पैटर्न के प्रमुख भाग हैं
`AsyncResult` जो अतुल्यकालिक रूप से मूल्यांकन किए गए मान के लिए एक मध्यवर्ती कंटेनर है,
`AsyncCallback` जिसे कार्य पूरा होने पर निष्पादित करने के लिए प्रदान किया जा सकता है और `AsyncExecutor`
async कार्यों के निष्पादन का प्रबंधन करता है।
```java
public interface AsyncResult<T> {
boolean isCompleted();
T getValue() throws ExecutionException;
void await() throws InterruptedException;
}
```
```java
public interface AsyncCallback<T> {
void onComplete(T value, Optional<Exception> ex);
}
```
```java
public interface AsyncExecutor {
<T> AsyncResult<T> startProcess(Callable<T> task);
<T> AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callback);
<T> T endProcess(AsyncResult<T> asyncResult) throws ExecutionException, InterruptedException;
}
```
`ThreadAsyncExecutor` is an implementation of `AsyncExecutor`. Some of its key parts are highlighted
next.
```java
public class ThreadAsyncExecutor implements AsyncExecutor {
@Override
public <T> AsyncResult<T> startProcess(Callable<T> task) {
return startProcess(task, null);
}
@Override
public <T> AsyncResult<T> startProcess(Callable<T> task, AsyncCallback<T> callback) {
var result = new CompletableResult<>(callback);
new Thread(
() -> {
try {
result.setValue(task.call());
} catch (Exception ex) {
result.setException(ex);
}
},
"executor-" + idx.incrementAndGet())
.start();
return result;
}
@Override
public <T> T endProcess(AsyncResult<T> asyncResult)
throws ExecutionException, InterruptedException {
if (!asyncResult.isCompleted()) {
asyncResult.await();
}
return asyncResult.getValue();
}
}
```
फिर हम यह देखने के लिए कुछ रॉकेट लॉन्च करने के लिए तैयार हैं कि सब कुछ एक साथ कैसे काम करता है।
```java
public static void main(String[] args) throws Exception {
// construct a new executor that will run async tasks
var executor = new ThreadAsyncExecutor();
// start few async tasks with varying processing times, two last with callback handlers
final var asyncResult1 = executor.startProcess(lazyval(10, 500));
final var asyncResult2 = executor.startProcess(lazyval("test", 300));
final var asyncResult3 = executor.startProcess(lazyval(50L, 700));
final var asyncResult4 = executor.startProcess(lazyval(20, 400), callback("Deploying lunar rover"));
final var asyncResult5 =
executor.startProcess(lazyval("callback", 600), callback("Deploying lunar rover"));
// emulate processing in the current thread while async tasks are running in their own threads
Thread.sleep(350); // Oh boy, we are working hard here
log("Mission command is sipping coffee");
// wait for completion of the tasks
final var result1 = executor.endProcess(asyncResult1);
final var result2 = executor.endProcess(asyncResult2);
final var result3 = executor.endProcess(asyncResult3);
asyncResult4.await();
asyncResult5.await();
// log the results of the tasks, callbacks log immediately when complete
log("Space rocket <" + result1 + "> launch complete");
log("Space rocket <" + result2 + "> launch complete");
log("Space rocket <" + result3 + "> launch complete");
}
```
यहां प्रोग्राम कंसोल आउटपुट है।
```java
21:47:08.227 [executor-2] INFO com.iluwatar.async.method.invocation.App - Space rocket <test> launched successfully
21:47:08.269 [main] INFO com.iluwatar.async.method.invocation.App - Mission command is sipping coffee
21:47:08.318 [executor-4] INFO com.iluwatar.async.method.invocation.App - Space rocket <20> launched successfully
21:47:08.335 [executor-4] INFO com.iluwatar.async.method.invocation.App - Deploying lunar rover <20>
21:47:08.414 [executor-1] INFO com.iluwatar.async.method.invocation.App - Space rocket <10> launched successfully
21:47:08.519 [executor-5] INFO com.iluwatar.async.method.invocation.App - Space rocket <callback> launched successfully
21:47:08.519 [executor-5] INFO com.iluwatar.async.method.invocation.App - Deploying lunar rover <callback>
21:47:08.616 [executor-3] INFO com.iluwatar.async.method.invocation.App - Space rocket <50> launched successfully
21:47:08.617 [main] INFO com.iluwatar.async.method.invocation.App - Space rocket <10> launch complete
21:47:08.617 [main] INFO com.iluwatar.async.method.invocation.App - Space rocket <test> launch complete
21:47:08.618 [main] INFO com.iluwatar.async.method.invocation.App - Space rocket <50> launch complete
```
# क्लास डायग्राम
![alt text](../../../async-method-invocation/etc/async-method-invocation.png "Async Method Invocation")
## प्रयोज्यता
जब async विधि मंगलाचरण पैटर्न का उपयोग करें
* आपके पास कई स्वतंत्र कार्य हैं जो समानांतर में चल सकते हैं
* आपको अनुक्रमिक कार्यों के समूह के प्रदर्शन में सुधार करने की आवश्यकता है
* आपके पास सीमित मात्रा में प्रसंस्करण क्षमता या लंबे समय तक चलने वाले कार्य हैं और कॉल करने वाले को कार्यों के तैयार होने का इंतजार नहीं करना चाहिए
## वास्तविक दुनिया के उदाहरण
* [FutureTask](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/FutureTask.html)
* [CompletableFuture](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html)
* [ExecutorService](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html)
* [Task-based Asynchronous Pattern](https://msdn.microsoft.com/en-us/library/hh873175.aspx)
+137
View File
@@ -0,0 +1,137 @@
---
title: Balking
category: Concurrency
language: hi
tag:
- Decoupling
---
## हेतु
बाल्किंग पैटर्न का उपयोग किसी ऑब्जेक्ट को किसी निश्चित कोड को निष्पादित करने से रोकने के लिए किया जाता है यदि वह अधूरा है
या अनुचित स्थिति.
## व्याख्या
वास्तविक दुनिया का उदाहरण
> कपड़े धोने की मशीन में कपड़े धोने की शुरुआत करने के लिए एक स्टार्ट-बटन होता है। धोते समय
> मशीन निष्क्रिय है तो बटन अपेक्षानुसार काम करता है, लेकिन यदि वह पहले से ही धुलाई कर रहा है तो बटन काम करता है
> कुछ नहीं.
साफ़ शब्दों में
> बैल्किंग पैटर्न का उपयोग करते हुए, एक निश्चित कोड केवल तभी निष्पादित होता है जब ऑब्जेक्ट विशेष स्थिति में हो।
विकिपीडिया कहता है
> बैल्किंग पैटर्न एक सॉफ्टवेयर डिज़ाइन पैटर्न है जो किसी ऑब्जेक्ट पर केवल तभी क्रिया निष्पादित करता है
> वस्तु एक विशेष अवस्था में है। उदाहरण के लिए, यदि कोई ऑब्जेक्ट ज़िप फ़ाइलें और कॉलिंग पढ़ता है
> विधि ऑब्जेक्ट पर एक गेट विधि को लागू करती है जब ज़िप फ़ाइल खुली नहीं होती है, तो ऑब्जेक्ट "बाल्क" हो जाएगा
> अनुरोध पर.
**प्रोग्रामेटिक उदाहरण**
इस उदाहरण के कार्यान्वयन में, `WashingMachine` एक ऑब्जेक्ट है जिसमें दो स्थितियाँ हैं जिनमें यह हो सकता है
होना: सक्षम और धुलाई। यदि मशीन सक्षम है, तो थ्रेड-सेफ का उपयोग करके स्थिति वॉशिंग में बदल जाती है
तरीका। दूसरी ओर, यदि यह पहले से ही धुलाई कर रहा है और कोई अन्य थ्रेड `wash()` निष्पादित करता है
यह ऐसा नहीं करेगा और बिना कुछ किए वापस लौट आएगा।
यहां `WashingMachine` वर्ग के प्रासंगिक भाग दिए गए हैं।
```java
@Slf4j
public class WashingMachine {
private final DelayProvider delayProvider;
private WashingMachineState washingMachineState;
public WashingMachine(DelayProvider delayProvider) {
this.delayProvider = delayProvider;
this.washingMachineState = WashingMachineState.ENABLED;
}
public WashingMachineState getWashingMachineState() {
return washingMachineState;
}
public void wash() {
synchronized (this) {
var machineState = getWashingMachineState();
LOGGER.info("{}: Actual machine state: {}", Thread.currentThread().getName(), machineState);
if (this.washingMachineState == WashingMachineState.WASHING) {
LOGGER.error("Cannot wash if the machine has been already washing!");
return;
}
this.washingMachineState = WashingMachineState.WASHING;
}
LOGGER.info("{}: Doing the washing", Thread.currentThread().getName());
this.delayProvider.executeAfterDelay(50, TimeUnit.MILLISECONDS, this::endOfWashing);
}
public synchronized void endOfWashing() {
washingMachineState = WashingMachineState.ENABLED;
LOGGER.info("{}: Washing completed.", Thread.currentThread().getId());
}
}
```
यहां `WashingMachine` द्वारा उपयोग किया जाने वाला सरल `DelayProvider` इंटरफ़ेस है।
```java
public interface DelayProvider {
void executeAfterDelay(long interval, TimeUnit timeUnit, Runnable task);
}
```
अब हम `WashingMachine` का उपयोग करके एप्लिकेशन का परिचय देते हैं।
```java
public static void main(String... args) {
final var washingMachine = new WashingMachine();
var executorService = Executors.newFixedThreadPool(3);
for (int i = 0; i < 3; i++) {
executorService.execute(washingMachine::wash);
}
executorService.shutdown();
try {
executorService.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException ie) {
LOGGER.error("ERROR: Waiting on executor service shutdown!");
Thread.currentThread().interrupt();
}
}
```
यहां प्रोग्राम का कंसोल आउटपुट है।
```
14:02:52.268 [pool-1-thread-2] INFO com.iluwatar.balking.WashingMachine - pool-1-thread-2: Actual machine state: ENABLED
14:02:52.272 [pool-1-thread-2] INFO com.iluwatar.balking.WashingMachine - pool-1-thread-2: Doing the washing
14:02:52.272 [pool-1-thread-3] INFO com.iluwatar.balking.WashingMachine - pool-1-thread-3: Actual machine state: WASHING
14:02:52.273 [pool-1-thread-3] ERROR com.iluwatar.balking.WashingMachine - Cannot wash if the machine has been already washing!
14:02:52.273 [pool-1-thread-1] INFO com.iluwatar.balking.WashingMachine - pool-1-thread-1: Actual machine state: WASHING
14:02:52.273 [pool-1-thread-1] ERROR com.iluwatar.balking.WashingMachine - Cannot wash if the machine has been already washing!
14:02:52.324 [pool-1-thread-2] INFO com.iluwatar.balking.WashingMachine - 14: Washing completed.
```
## क्लास डायग्राम
![alt text](../../../balking/etc/balking.png "Balking")
## प्रयोज्यता
जब बाल्किंग पैटर्न का प्रयोग करें
* आप किसी वस्तु पर तभी कोई कार्रवाई करना चाहते हैं जब वह किसी विशेष स्थिति में हो
* वस्तुएँ आम तौर पर केवल ऐसी स्थिति में होती हैं जो अस्थायी रूप से लेकिन किसी अज्ञात के लिए झुकने की संभावना होती है
लगने वाला समय
## संबंधित पैटर्न
* [Guarded Suspension Pattern](https://java-design-patterns.com/patterns/guarded-suspension/)
* [Double Checked Locking Pattern](https://java-design-patterns.com/patterns/double-checked-locking/)
## श्रेय
* [Patterns in Java: A Catalog of Reusable Design Patterns Illustrated with UML, 2nd Edition, Volume 1](https://www.amazon.com/gp/product/0471227293/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0471227293&linkId=0e39a59ffaab93fb476036fecb637b99)
+214
View File
@@ -0,0 +1,214 @@
---
title: Bridge
category: Structural
language: hi
tag:
- Gang of Four
---
## दूसरा नाम
हैंडल/बॉडी
## हेतु
इसके कार्यान्वयन से एक अमूर्त को अलग करें ताकि दोनों स्वतंत्र रूप से भिन्न हो सकें।
## व्याख्या
वास्तविक दुनिया का उदाहरण
> विचार करें कि आपके पास विभिन्न जादूओं वाला एक हथियार है, और आपको मिश्रण की अनुमति देनी चाहिए
> अलग-अलग जादू वाले अलग-अलग हथियार। आप क्या करेंगे? प्रत्येक की एकाधिक प्रतिलिपियाँ बनाएँ
> प्रत्येक जादू के लिए हथियारों का या आप बस अलग-अलग जादू और सेट बनाएंगे
> क्या यह आवश्यकतानुसार हथियार के लिए है? ब्रिज पैटर्न आपको दूसरा करने की अनुमति देता है।
सादे शब्दों में
> ब्रिज पैटर्न वंशानुक्रम पर संरचना को प्राथमिकता देने के बारे में है। कार्यान्वयन विवरण आगे बढ़ाए गए हैं
> एक पदानुक्रम से दूसरे ऑब्जेक्ट पर एक अलग पदानुक्रम के साथ।
विकिपीडिया कहता है
> ब्रिज पैटर्न सॉफ्टवेयर इंजीनियरिंग में उपयोग किया जाने वाला एक डिज़ाइन पैटर्न है जिसका अर्थ है "इसके कार्यान्वयन से एक अमूर्तता को अलग करना ताकि दोनों स्वतंत्र रूप से भिन्न हो सकें"
**प्रोग्रामेटिक उदाहरण**
ऊपर से हमारे हथियार उदाहरण का अनुवाद। यहां हमारे पास `Weapon` पदानुक्रम है:
```java
public interface Weapon {
void wield();
void swing();
void unwield();
Enchantment getEnchantment();
}
public class Sword implements Weapon {
private final Enchantment enchantment;
public Sword(Enchantment enchantment) {
this.enchantment = enchantment;
}
@Override
public void wield() {
LOGGER.info("The sword is wielded.");
enchantment.onActivate();
}
@Override
public void swing() {
LOGGER.info("The sword is swinged.");
enchantment.apply();
}
@Override
public void unwield() {
LOGGER.info("The sword is unwielded.");
enchantment.onDeactivate();
}
@Override
public Enchantment getEnchantment() {
return enchantment;
}
}
public class Hammer implements Weapon {
private final Enchantment enchantment;
public Hammer(Enchantment enchantment) {
this.enchantment = enchantment;
}
@Override
public void wield() {
LOGGER.info("The hammer is wielded.");
enchantment.onActivate();
}
@Override
public void swing() {
LOGGER.info("The hammer is swinged.");
enchantment.apply();
}
@Override
public void unwield() {
LOGGER.info("The hammer is unwielded.");
enchantment.onDeactivate();
}
@Override
public Enchantment getEnchantment() {
return enchantment;
}
}
```
यहां अलग मंत्रमुग्धता पदानुक्रम है:
```java
public interface Enchantment {
void onActivate();
void apply();
void onDeactivate();
}
public class FlyingEnchantment implements Enchantment {
@Override
public void onActivate() {
LOGGER.info("The item begins to glow faintly.");
}
@Override
public void apply() {
LOGGER.info("The item flies and strikes the enemies finally returning to owner's hand.");
}
@Override
public void onDeactivate() {
LOGGER.info("The item's glow fades.");
}
}
public class SoulEatingEnchantment implements Enchantment {
@Override
public void onActivate() {
LOGGER.info("The item spreads bloodlust.");
}
@Override
public void apply() {
LOGGER.info("The item eats the soul of enemies.");
}
@Override
public void onDeactivate() {
LOGGER.info("Bloodlust slowly disappears.");
}
}
```
यहां दोनों पदानुक्रम क्रियान्वित हैं:
```java
LOGGER.info("The knight receives an enchanted sword.");
var enchantedSword = new Sword(new SoulEatingEnchantment());
enchantedSword.wield();
enchantedSword.swing();
enchantedSword.unwield();
LOGGER.info("The valkyrie receives an enchanted hammer.");
var hammer = new Hammer(new FlyingEnchantment());
hammer.wield();
hammer.swing();
hammer.unwield();
```
यहाँ कंसोल आउटपुट है.
```
The knight receives an enchanted sword.
The sword is wielded.
The item spreads bloodlust.
The sword is swung.
The item eats the soul of enemies.
The sword is unwielded.
Bloodlust slowly disappears.
The valkyrie receives an enchanted hammer.
The hammer is wielded.
The item begins to glow faintly.
The hammer is swung.
The item flies and strikes the enemies finally returning to owner's hand.
The hammer is unwielded.
The item's glow fades.
```
## क्लास डायग्राम
![alt text](../../../bridge/etc/bridge.urm.png "Bridge class diagram")
## प्रयोज्यता
जब ब्रिज पैटर्न का प्रयोग करें
* आप किसी अमूर्तता और उसके कार्यान्वयन के बीच स्थायी बंधन से बचना चाहते हैं। यह मामला हो सकता है, उदाहरण के लिए, जब कार्यान्वयन को रन-टाइम पर चुना या स्विच किया जाना चाहिए।
* अमूर्तीकरण और उनके कार्यान्वयन दोनों को उपवर्गीकरण द्वारा विस्तार योग्य होना चाहिए। इस मामले में, ब्रिज पैटर्न आपको विभिन्न अमूर्तताओं और कार्यान्वयनों को संयोजित करने और उन्हें स्वतंत्र रूप से विस्तारित करने की सुविधा देता है।
* किसी अमूर्त के कार्यान्वयन में परिवर्तन का ग्राहकों पर कोई प्रभाव नहीं पड़ना चाहिए; अर्थात्, उनके कोड को पुनः संकलित नहीं करना पड़ेगा।
* आपके पास वर्गों का प्रसार है। ऐसा वर्ग पदानुक्रम किसी वस्तु को दो भागों में विभाजित करने की आवश्यकता को इंगित करता है। रुंबॉघ ऐसे वर्ग पदानुक्रमों को संदर्भित करने के लिए "नेस्टेड सामान्यीकरण" शब्द का उपयोग करता है।
* आप एक कार्यान्वयन को कई ऑब्जेक्ट्स के बीच साझा करना चाहते हैं (शायद संदर्भ गणना का उपयोग करके), और यह तथ्य क्लाइंट से छिपा होना चाहिए। एक सरल उदाहरण कोप्लियन का स्ट्रिंग क्लास है, जिसमें कई ऑब्जेक्ट एक ही स्ट्रिंग प्रतिनिधित्व साझा कर सकते हैं।
## ट्यूटोरियल
* [Bridge Pattern Tutorial](https://www.journaldev.com/1491/bridge-design-pattern-java)
## श्रेय
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
+154
View File
@@ -0,0 +1,154 @@
---
title: Builder
category: Creational
language: hi
tag:
- Gang of Four
---
## हेतु
किसी जटिल वस्तु के निर्माण को उसके प्रतिनिधित्व से अलग करें ताकि निर्माण समान हो
प्रक्रिया विभिन्न अभ्यावेदन बना सकती है।
## व्याख्या
वास्तविक दुनिया का उदाहरण
> रोल-प्लेइंग गेम के लिए एक चरित्र जनरेटर की कल्पना करें। सबसे आसान विकल्प कंप्यूटर को चालू करना है
> अपने लिए चरित्र बनाएं. यदि आप मैन्युअल रूप से चरित्र विवरण का चयन करना चाहते हैं जैसे
> पेशा, लिंग, बालों का रंग आदि, चरित्र निर्माण एक चरण-दर-चरण प्रक्रिया बन जाती है
> सभी चयन तैयार होने पर पूरा होता है।
साफ़ शब्दों में
> कंस्ट्रक्टर प्रदूषण से बचते हुए आपको किसी वस्तु के विभिन्न स्वाद बनाने की अनुमति देता है। उपयोगी
>जब किसी वस्तु के कई स्वाद हो सकते हैं। या जब इसमें बहुत सारे चरण शामिल हों
> किसी वस्तु का निर्माण।
विकिपीडिया कहता है
> बिल्डर पैटर्न खोजने के इरादे से एक ऑब्जेक्ट निर्माण सॉफ़्टवेयर डिज़ाइन पैटर्न है
> टेलिस्कोपिंग कंस्ट्रक्टर एंटी-पैटर्न का समाधान।
यह कहने के बाद, मैं इस बारे में थोड़ा जोड़ना चाहूंगा कि टेलीस्कोपिंग कंस्ट्रक्टर एंटी-पैटर्न क्या है। एक बिंदु पर
या अन्य, हम सभी ने नीचे जैसा एक कंस्ट्रक्टर देखा है:
```java
public Hero(Profession profession, String name, HairType hairType, HairColor hairColor, Armor armor, Weapon weapon) {
}
```
जैसा कि आप देख सकते हैं, कंस्ट्रक्टर मापदंडों की संख्या जल्दी से नियंत्रण से बाहर हो सकती है, और यह बन सकती है
मापदंडों की व्यवस्था को समझना कठिन है। साथ ही यह पैरामीटर सूची चालू रह सकती है
यदि आप भविष्य में और विकल्प जोड़ना चाहेंगे तो बढ़ रहा हूँ। इसे टेलिस्कोपिंग कंस्ट्रक्टर कहा जाता है
विरोधी पैटर्न.
**प्रोग्रामेटिक उदाहरण**
बिल्डर पैटर्न का उपयोग करना समझदारी भरा विकल्प है। सबसे पहले, हमारे पास अपना हीरो है जिसे हम चाहते हैं
बनाएं:
```java
public final class Hero {
private final Profession profession;
private final String name;
private final HairType hairType;
private final HairColor hairColor;
private final Armor armor;
private final Weapon weapon;
private Hero(Builder builder) {
this.profession = builder.profession;
this.name = builder.name;
this.hairColor = builder.hairColor;
this.hairType = builder.hairType;
this.weapon = builder.weapon;
this.armor = builder.armor;
}
}
```
फिर हमारे पास बिल्डर है:
```java
public static class Builder {
private final Profession profession;
private final String name;
private HairType hairType;
private HairColor hairColor;
private Armor armor;
private Weapon weapon;
public Builder(Profession profession, String name) {
if (profession == null || name == null) {
throw new IllegalArgumentException("profession and name can not be null");
}
this.profession = profession;
this.name = name;
}
public Builder withHairType(HairType hairType) {
this.hairType = hairType;
return this;
}
public Builder withHairColor(HairColor hairColor) {
this.hairColor = hairColor;
return this;
}
public Builder withArmor(Armor armor) {
this.armor = armor;
return this;
}
public Builder withWeapon(Weapon weapon) {
this.weapon = weapon;
return this;
}
public Hero build() {
return new Hero(this);
}
}
```
तब इसका उपयोग इस प्रकार किया जा सकता है:
```java
var mage = new Hero.Builder(Profession.MAGE, "Riobard").withHairColor(HairColor.BLACK).withWeapon(Weapon.DAGGER).build();
```
## क्लास डायग्राम
![alt text](../../../builder/etc/builder.urm.png "Builder class diagram")
## प्रयोज्यता
जब बिल्डर पैटर्न का उपयोग करें
* एक जटिल वस्तु बनाने के लिए एल्गोरिदम उन हिस्सों से स्वतंत्र होना चाहिए जो वस्तु बनाते हैं और उन्हें कैसे इकट्ठा किया जाता है
* निर्माण प्रक्रिया में निर्मित वस्तु के लिए अलग-अलग प्रतिनिधित्व की अनुमति होनी चाहिए
## ट्यूटोरियल
* [Refactoring Guru](https://refactoring.guru/design-patterns/builder)
* [Oracle Blog](https://blogs.oracle.com/javamagazine/post/exploring-joshua-blochs-builder-design-pattern-in-java)
* [Journal Dev](https://www.journaldev.com/1425/builder-design-pattern-in-java)
## ज्ञात उपयोग
* [java.lang.StringBuilder](http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html)
* [java.nio.ByteBuffer](http://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html#put-byte-) साथ ही समान बफ़र्स जैसे फ़्लोटबफ़र, इंटबफ़र इत्यादि।
* [java.lang.StringBuffer](http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html#append-boolean-)
* के सभी कार्यान्वयन [java.lang.Appendable](http://docs.oracle.com/javase/8/docs/api/java/lang/Appendable.html)
* [Apache Camel builders](https://github.com/apache/camel/tree/0e195428ee04531be27a0b659005e3aa8d159d23/camel-core/src/main/java/org/apache/camel/builder)
* [Apache Commons Option.Builder](https://commons.apache.org/proper/commons-cli/apidocs/org/apache/commons/cli/Option.Builder.html)
## श्रेय
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
* [Effective Java](https://www.amazon.com/gp/product/0134685997/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0134685997&linkCode=as2&tag=javadesignpat-20&linkId=4e349f4b3ff8c50123f8147c828e53eb)
* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
* [Refactoring to Patterns](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7)
+162
View File
@@ -0,0 +1,162 @@
---
title: Business Delegate
category: Structural
language: hi
tag:
- Decoupling
---
## हेतु
बिजनेस डेलीगेट पैटर्न बीच में एक अमूर्त परत जोड़ता है
प्रस्तुतिकरण और व्यावसायिक स्तर। पैटर्न का उपयोग करके हम ढीला युग्मन प्राप्त करते हैं
स्तरों के बीच और पता लगाने, कनेक्ट करने के तरीके के बारे में ज्ञान को समाहित करें,
और एप्लिकेशन बनाने वाली व्यावसायिक वस्तुओं के साथ इंटरैक्ट करें।
## व्याख्या
वास्तविक दुनिया का उदाहरण
> एक मोबाइल फोन एप्लिकेशन आपके फोन पर मौजूदा किसी भी फिल्म को स्ट्रीम करने का वादा करता है। यह पकड़ लेता है
> उपयोगकर्ता की खोज स्ट्रिंग और इसे व्यवसाय प्रतिनिधि को भेजता है। व्यापार प्रतिनिधि
> सबसे उपयुक्त वीडियो स्ट्रीमिंग सेवा का चयन करता है और वहां से वीडियो चलाता है।
सादे शब्दों में
> व्यावसायिक प्रतिनिधि प्रस्तुतिकरण और व्यावसायिक स्तरों के बीच एक अमूर्त परत जोड़ता है।
विकिपीडिया कहता है
> बिजनेस डेलिगेट एक जावा ईई डिज़ाइन पैटर्न है। यह पैटर्न युग्मन को कम करने का निर्देश दे रहा है
> व्यावसायिक सेवाओं और कनेक्टेड प्रेजेंटेशन स्तर के बीच, और कार्यान्वयन को छिपाने के लिए
> सेवाओं का विवरण (ईजेबी आर्किटेक्चर की खोज और पहुंच सहित)। व्यापार प्रतिनिधि
> प्रेजेंटेशन स्तर से व्यावसायिक वस्तुओं को आमंत्रित करने के लिए एक एडाप्टर के रूप में कार्य करता है।
**प्रोग्रामेटिक उदाहरण**
सबसे पहले, हमारे पास वीडियो स्ट्रीमिंग सेवाओं और कुछ कार्यान्वयन के लिए एक सार है।
```java
public interface VideoStreamingService {
void doProcessing();
}
@Slf4j
public class NetflixService implements VideoStreamingService {
@Override
public void doProcessing() {
LOGGER.info("NetflixService is now processing");
}
}
@Slf4j
public class YouTubeService implements VideoStreamingService {
@Override
public void doProcessing() {
LOGGER.info("YouTubeService is now processing");
}
}
```
फिर हमारे पास एक लुकअप सेवा है जो यह तय करती है कि कौन सी वीडियो स्ट्रीमिंग सेवा का उपयोग किया जाए।
```java
@Setter
public class BusinessLookup {
private NetflixService netflixService;
private YouTubeService youTubeService;
public VideoStreamingService getBusinessService(String movie) {
if (movie.toLowerCase(Locale.ROOT).contains("die hard")) {
return netflixService;
} else {
return youTubeService;
}
}
}
```
व्यवसाय प्रतिनिधि मूवी प्लेबैक अनुरोधों को उपयुक्त स्थान पर रूट करने के लिए व्यवसाय लुकअप का उपयोग करता है
वीडियो स्ट्रीमिंग सेवा.
```java
@Setter
public class BusinessDelegate {
private BusinessLookup lookupService;
public void playbackMovie(String movie) {
VideoStreamingService videoStreamingService = lookupService.getBusinessService(movie);
videoStreamingService.doProcessing();
}
}
```
मोबाइल क्लाइंट व्यवसाय स्तर पर कॉल करने के लिए व्यवसाय प्रतिनिधि का उपयोग करता है।
```java
public class MobileClient {
private final BusinessDelegate businessDelegate;
public MobileClient(BusinessDelegate businessDelegate) {
this.businessDelegate = businessDelegate;
}
public void playbackMovie(String movie) {
businessDelegate.playbackMovie(movie);
}
}
```
अंत में, हम कार्रवाई में पूरा उदाहरण दिखा सकते हैं।
```java
public static void main(String[] args) {
// prepare the objects
var businessDelegate = new BusinessDelegate();
var businessLookup = new BusinessLookup();
businessLookup.setNetflixService(new NetflixService());
businessLookup.setYouTubeService(new YouTubeService());
businessDelegate.setLookupService(businessLookup);
// create the client and use the business delegate
var client = new MobileClient(businessDelegate);
client.playbackMovie("Die Hard 2");
client.playbackMovie("Maradona: The Greatest Ever");
}
```
यहाँ कंसोल आउटपुट है.
```
21:15:33.790 [main] INFO com.iluwatar.business.delegate.NetflixService - NetflixService is now processing
21:15:33.794 [main] INFO com.iluwatar.business.delegate.YouTubeService - YouTubeService is now processing
```
## क्लास डायग्राम
![alt text](../../../business-delegate/etc/business-delegate.urm.png "Business Delegate")
## संबंधित पैटर्न
* [Service locator pattern](https://java-design-patterns.com/patterns/service-locator/)
## प्रयोज्यता
बिजनेस डेलीगेट पैटर्न का उपयोग कब करें
* आप प्रस्तुतिकरण और व्यावसायिक स्तरों के बीच ढीला युग्मन चाहते हैं
* आप एकाधिक व्यावसायिक सेवाओं के लिए कॉल व्यवस्थित करना चाहते हैं
* आप सेवा लुकअप और सेवा कॉल को समाहित करना चाहते हैं
## ट्यूटोरियल
* [Business Delegate Pattern at TutorialsPoint](https://www.tutorialspoint.com/design_pattern/business_delegate_pattern.htm)
## Credits
* [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=48d37c67fb3d845b802fa9b619ad8f31)
* [Core J2EE Patterns: Best Practices and Design Strategies](https://www.amazon.com/gp/product/0130648841/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0130648841&linkId=a0100de2b28c71ede8db1757fb2b5947)
+241
View File
@@ -0,0 +1,241 @@
---
title: Bytecode
category: Behavioral
language: hi
tag:
- Game programming
---
## हेतु
वर्चुअल मशीन के लिए निर्देशों के रूप में एन्कोडिंग व्यवहार की अनुमति देता है।
## व्याख्या
वास्तविक दुनिया का उदाहरण
> एक टीम एक नए गेम पर काम कर रही है जहां जादूगर एक-दूसरे के खिलाफ लड़ते हैं। जादूगर का व्यवहार
> प्लेटेस्टिंग के माध्यम से सैकड़ों बार सावधानीपूर्वक समायोजित और पुनरावृत्त करने की आवश्यकता है। यह
> हर बार जब गेम डिजाइनर बदलाव करना चाहता है तो प्रोग्रामर से बदलाव करने के लिए कहना सबसे अच्छा है
> व्यवहार, इसलिए विज़ार्ड व्यवहार को डेटा-संचालित वर्चुअल मशीन के रूप में कार्यान्वित किया जाता है।
साफ़ शब्दों में
> बाइटकोड पैटर्न कोड के बजाय डेटा द्वारा संचालित व्यवहार को सक्षम बनाता है।
[Gameprogrammingpatterns.com](https://gameprogrammingpatterns.com/bytecode.html) प्रलेखन
बताता है:
> एक निर्देश सेट निम्न-स्तरीय संचालन को परिभाषित करता है जिन्हें निष्पादित किया जा सकता है। की एक श्रृंखला
> निर्देश बाइट्स के अनुक्रम के रूप में एन्कोड किया गया है। एक वर्चुअल मशीन इन निर्देशों को एक-एक करके क्रियान्वित करती है
> एक समय में, मध्यवर्ती मानों के लिए स्टैक का उपयोग करना। अनुदेशों के संयोजन से, जटिल उच्च-स्तरीय
> व्यवहार को परिभाषित किया जा सकता है।
**प्रोग्रामेटिक उदाहरण**
सबसे महत्वपूर्ण गेम ऑब्जेक्ट में से एक `Wizard` वर्ग है।
```java
@AllArgsConstructor
@Setter
@Getter
@Slf4j
public class Wizard {
private int health;
private int agility;
private int wisdom;
private int numberOfPlayedSounds;
private int numberOfSpawnedParticles;
public void playSound() {
LOGGER.info("Playing sound");
numberOfPlayedSounds++;
}
public void spawnParticles() {
LOGGER.info("Spawning particles");
numberOfSpawnedParticles++;
}
}
```
इसके बाद, हम अपनी वर्चुअल मशीन के लिए उपलब्ध निर्देश दिखाते हैं। प्रत्येक निर्देश का अपना है
यह स्टैक डेटा के साथ कैसे संचालित होता है, इसका अपना शब्दार्थ। उदाहरण के लिए, ADD निर्देश शीर्ष पर है
स्टैक से दो आइटम, उन्हें एक साथ जोड़ता है और परिणाम को स्टैक पर भेजता है।
```java
@AllArgsConstructor
@Getter
public enum Instruction {
LITERAL(1), // e.g. "LITERAL 0", push 0 to stack
SET_HEALTH(2), // e.g. "SET_HEALTH", pop health and wizard number, call set health
SET_WISDOM(3), // e.g. "SET_WISDOM", pop wisdom and wizard number, call set wisdom
SET_AGILITY(4), // e.g. "SET_AGILITY", pop agility and wizard number, call set agility
PLAY_SOUND(5), // e.g. "PLAY_SOUND", pop value as wizard number, call play sound
SPAWN_PARTICLES(6), // e.g. "SPAWN_PARTICLES", pop value as wizard number, call spawn particles
GET_HEALTH(7), // e.g. "GET_HEALTH", pop value as wizard number, push wizard's health
GET_AGILITY(8), // e.g. "GET_AGILITY", pop value as wizard number, push wizard's agility
GET_WISDOM(9), // e.g. "GET_WISDOM", pop value as wizard number, push wizard's wisdom
ADD(10), // e.g. "ADD", pop 2 values, push their sum
DIVIDE(11); // e.g. "DIVIDE", pop 2 values, push their division
// ...
}
```
हमारे उदाहरण के केंद्र में `VirtualMachine` वर्ग है। यह निर्देशों को इनपुट के रूप में लेता है और
गेम ऑब्जेक्ट व्यवहार प्रदान करने के लिए उन्हें निष्पादित करता है।
```java
@Getter
@Slf4j
public class VirtualMachine {
private final Stack<Integer> stack = new Stack<>();
private final Wizard[] wizards = new Wizard[2];
public VirtualMachine() {
wizards[0] = new Wizard(randomInt(3, 32), randomInt(3, 32), randomInt(3, 32),
0, 0);
wizards[1] = new Wizard(randomInt(3, 32), randomInt(3, 32), randomInt(3, 32),
0, 0);
}
public VirtualMachine(Wizard wizard1, Wizard wizard2) {
wizards[0] = wizard1;
wizards[1] = wizard2;
}
public void execute(int[] bytecode) {
for (var i = 0; i < bytecode.length; i++) {
Instruction instruction = Instruction.getInstruction(bytecode[i]);
switch (instruction) {
case LITERAL:
// Read the next byte from the bytecode.
int value = bytecode[++i];
// Push the next value to stack
stack.push(value);
break;
case SET_AGILITY:
var amount = stack.pop();
var wizard = stack.pop();
setAgility(wizard, amount);
break;
case SET_WISDOM:
amount = stack.pop();
wizard = stack.pop();
setWisdom(wizard, amount);
break;
case SET_HEALTH:
amount = stack.pop();
wizard = stack.pop();
setHealth(wizard, amount);
break;
case GET_HEALTH:
wizard = stack.pop();
stack.push(getHealth(wizard));
break;
case GET_AGILITY:
wizard = stack.pop();
stack.push(getAgility(wizard));
break;
case GET_WISDOM:
wizard = stack.pop();
stack.push(getWisdom(wizard));
break;
case ADD:
var a = stack.pop();
var b = stack.pop();
stack.push(a + b);
break;
case DIVIDE:
a = stack.pop();
b = stack.pop();
stack.push(b / a);
break;
case PLAY_SOUND:
wizard = stack.pop();
getWizards()[wizard].playSound();
break;
case SPAWN_PARTICLES:
wizard = stack.pop();
getWizards()[wizard].spawnParticles();
break;
default:
throw new IllegalArgumentException("Invalid instruction value");
}
LOGGER.info("Executed " + instruction.name() + ", Stack contains " + getStack());
}
}
public void setHealth(int wizard, int amount) {
wizards[wizard].setHealth(amount);
}
// other setters ->
// ...
}
```
अब हम वर्चुअल मशीन का उपयोग करके पूरा उदाहरण दिखा सकते हैं।
```java
public static void main(String[] args) {
var vm = new VirtualMachine(
new Wizard(45, 7, 11, 0, 0),
new Wizard(36, 18, 8, 0, 0));
vm.execute(InstructionConverterUtil.convertToByteCode("LITERAL 0"));
vm.execute(InstructionConverterUtil.convertToByteCode("LITERAL 0"));
vm.execute(InstructionConverterUtil.convertToByteCode("GET_HEALTH"));
vm.execute(InstructionConverterUtil.convertToByteCode("LITERAL 0"));
vm.execute(InstructionConverterUtil.convertToByteCode("GET_AGILITY"));
vm.execute(InstructionConverterUtil.convertToByteCode("LITERAL 0"));
vm.execute(InstructionConverterUtil.convertToByteCode("GET_WISDOM"));
vm.execute(InstructionConverterUtil.convertToByteCode("ADD"));
vm.execute(InstructionConverterUtil.convertToByteCode("LITERAL 2"));
vm.execute(InstructionConverterUtil.convertToByteCode("DIVIDE"));
vm.execute(InstructionConverterUtil.convertToByteCode("ADD"));
vm.execute(InstructionConverterUtil.convertToByteCode("SET_HEALTH"));
}
```
यहाँ कंसोल आउटपुट है.
```
16:20:10.193 [main] INFO com.iluwatar.bytecode.VirtualMachine - Executed LITERAL, Stack contains [0]
16:20:10.196 [main] INFO com.iluwatar.bytecode.VirtualMachine - Executed LITERAL, Stack contains [0, 0]
16:20:10.197 [main] INFO com.iluwatar.bytecode.VirtualMachine - Executed GET_HEALTH, Stack contains [0, 45]
16:20:10.197 [main] INFO com.iluwatar.bytecode.VirtualMachine - Executed LITERAL, Stack contains [0, 45, 0]
16:20:10.197 [main] INFO com.iluwatar.bytecode.VirtualMachine - Executed GET_AGILITY, Stack contains [0, 45, 7]
16:20:10.197 [main] INFO com.iluwatar.bytecode.VirtualMachine - Executed LITERAL, Stack contains [0, 45, 7, 0]
16:20:10.197 [main] INFO com.iluwatar.bytecode.VirtualMachine - Executed GET_WISDOM, Stack contains [0, 45, 7, 11]
16:20:10.197 [main] INFO com.iluwatar.bytecode.VirtualMachine - Executed ADD, Stack contains [0, 45, 18]
16:20:10.197 [main] INFO com.iluwatar.bytecode.VirtualMachine - Executed LITERAL, Stack contains [0, 45, 18, 2]
16:20:10.198 [main] INFO com.iluwatar.bytecode.VirtualMachine - Executed DIVIDE, Stack contains [0, 45, 9]
16:20:10.198 [main] INFO com.iluwatar.bytecode.VirtualMachine - Executed ADD, Stack contains [0, 54]
16:20:10.198 [main] INFO com.iluwatar.bytecode.VirtualMachine - Executed SET_HEALTH, Stack contains []
```
## क्लास डायग्राम
![alt text](../../../bytecode/etc/bytecode.urm.png "Bytecode class diagram")
## प्रयोज्यता
जब आपके पास बहुत सारे व्यवहार हों जिन्हें आपको परिभाषित करने की आवश्यकता हो तो बाइटकोड पैटर्न का उपयोग करें
गेम की कार्यान्वयन भाषा उपयुक्त नहीं है क्योंकि:
* यह बहुत निम्न स्तर का है, जिससे इसे प्रोग्राम करना कठिन या त्रुटि-प्रवण हो जाता है।
* धीमे संकलन समय या अन्य टूलींग समस्याओं के कारण इस पर पुनरावृत्ति करने में बहुत अधिक समय लगता है।
* इस पर बहुत ज्यादा भरोसा है. यदि आप यह सुनिश्चित करना चाहते हैं कि परिभाषित किया जा रहा व्यवहार गेम को तोड़ न सके, तो आपको इसे शेष कोडबेस से सैंडबॉक्स करना होगा।
## संबंधित पैटर्न
* [Interpreter](https://java-design-patterns.com/patterns/interpreter/)
## श्रेय
* [Game programming patterns](http://gameprogrammingpatterns.com/bytecode.html)
+342
View File
@@ -0,0 +1,342 @@
---
title: Caching
category: Behavioral
language: hi
tag:
- Performance
- Cloud distributed
---
## हेतु
कैशिंग पैटर्न संसाधनों को तुरंत जारी न करके उनके महंगे पुन: अधिग्रहण से बचाता है
उपयोग के बाद। संसाधन अपनी पहचान बनाए रखते हैं, कुछ तेज़-पहुँच वाले भंडारण में रखे जाते हैं, और हैं
उन्हें दोबारा प्राप्त करने से बचने के लिए पुन: उपयोग किया जाता है।
## व्याख्या
वास्तविक दुनिया का उदाहरण
> एक टीम एक ऐसी वेबसाइट पर काम कर रही है जो परित्यक्त बिल्लियों के लिए नए घर उपलब्ध कराती है। लोग अपनी पोस्ट कर सकते हैं
> पंजीकरण के बाद वेबसाइट पर बिल्लियाँ, लेकिन सभी नए पोस्ट के लिए इनमें से किसी एक से अनुमोदन की आवश्यकता होती है
> साइट मॉडरेटर. साइट मॉडरेटर के उपयोगकर्ता खातों में एक विशिष्ट ध्वज और डेटा होता है
> MongoDB डेटाबेस में संग्रहीत है। हर बार पोस्ट देखे जाने पर मॉडरेटर फ़्लैग की जाँच करना
> महंगा हो जाता है और यहां कैशिंग का उपयोग करना एक अच्छा विचार है।
साफ़ शब्दों में
> कैशिंग पैटर्न प्रदर्शन को बेहतर बनाने के लिए अक्सर आवश्यक डेटा को फास्ट-एक्सेस स्टोरेज में रखता है।
विकिपीडिया कहता है:
> कंप्यूटिंग में, कैश एक हार्डवेयर या सॉफ़्टवेयर घटक है जो भविष्य में डेटा संग्रहीत करता है
> उस डेटा के लिए अनुरोध तेजी से प्रस्तुत किए जा सकते हैं; कैश में संग्रहीत डेटा का परिणाम हो सकता है
> पहले की गणना या अन्यत्र संग्रहीत डेटा की एक प्रति। अनुरोध किए जाने पर कैश हिट होता है
> डेटा कैश में पाया जा सकता है, जबकि कैश मिस तब होता है जब ऐसा नहीं हो पाता। कैश हिट द्वारा परोसे जाते हैं
> कैश से डेटा पढ़ना, जो किसी परिणाम की पुन: गणना करने या धीमी गति से पढ़ने की तुलना में तेज़ है
> डेटा स्टोर; इस प्रकार, कैश से जितने अधिक अनुरोध दिए जा सकेंगे, सिस्टम उतना ही तेज़ होगा
> प्रदर्शन करता है.
**प्रोग्रामेटिक उदाहरण**
आइए सबसे पहले हमारे एप्लिकेशन के डेटा स्तर को देखें। दिलचस्प कक्षाएं `UserAccount` हैं
जो एक सरल जावा ऑब्जेक्ट है जिसमें उपयोगकर्ता खाता विवरण और `DbManager` इंटरफ़ेस शामिल है जो संभालता है
डेटाबेस से/इन ऑब्जेक्ट को पढ़ना और लिखना।
```java
@Data
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class UserAccount {
private String userId;
private String userName;
private String additionalInfo;
}
public interface DbManager {
void connect();
void disconnect();
UserAccount readFromDb(String userId);
UserAccount writeToDb(UserAccount userAccount);
UserAccount updateDb(UserAccount userAccount);
UserAccount upsertDb(UserAccount userAccount);
}
```
उदाहरण में, हम विभिन्न कैशिंग नीतियों का प्रदर्शन कर रहे हैं
* राइट-थ्रू एक ही लेनदेन में कैश और डीबी में डेटा लिखता है
* राइट-अराउंड डेटा को कैश के बजाय तुरंत डीबी में लिखता है
* राइट-बैक शुरू में डेटा को कैश में लिखता है जबकि डेटा केवल डीबी में लिखा जाता है
जब कैश भर जाए
* कैश-साइड दोनों डेटा स्रोतों में डेटा को सिंक्रनाइज़ रखने की ज़िम्मेदारी को आगे बढ़ाता है
आवेदन ही
* उपरोक्त रणनीतियों में रीड-थ्रू रणनीति भी शामिल है और यह डेटा लौटाती है
यदि यह मौजूद है तो कॉल करने वाले को कैश, अन्यथा डीबी से क्वेरी करता है और इसे कैश में संग्रहीत करता है
भविष्य के काम।
`LruCache` में कैश कार्यान्वयन एक डबल के साथ एक हैश तालिका है
लिंक्ड सूची। लिंक्ड-लिस्ट कैश में एलआरयू डेटा को कैप्चर करने और बनाए रखने में मदद करती है। कब
डेटा को क्वेरी किया जाता है (कैश से), जोड़ा जाता है (कैश में), या अपडेट किया जाता है, डेटा को सामने ले जाया जाता है
सूची में खुद को सबसे हाल ही में उपयोग किए गए डेटा के रूप में दर्शाया गया है। एलआरयू डेटा हमेशा अंत में होता है
सूची।
```java
@Slf4j
public class LruCache {
static class Node {
String userId;
UserAccount userAccount;
Node previous;
Node next;
public Node(String userId, UserAccount userAccount) {
this.userId = userId;
this.userAccount = userAccount;
}
}
/* ... omitted details ... */
public LruCache(int capacity) {
this.capacity = capacity;
}
public UserAccount get(String userId) {
if (cache.containsKey(userId)) {
var node = cache.get(userId);
remove(node);
setHead(node);
return node.userAccount;
}
return null;
}
public void set(String userId, UserAccount userAccount) {
if (cache.containsKey(userId)) {
var old = cache.get(userId);
old.userAccount = userAccount;
remove(old);
setHead(old);
} else {
var newNode = new Node(userId, userAccount);
if (cache.size() >= capacity) {
LOGGER.info("# Cache is FULL! Removing {} from cache...", end.userId);
cache.remove(end.userId); // remove LRU data from cache.
remove(end);
setHead(newNode);
} else {
setHead(newNode);
}
cache.put(userId, newNode);
}
}
public boolean contains(String userId) {
return cache.containsKey(userId);
}
public void remove(Node node) { /* ... */ }
public void setHead(Node node) { /* ... */ }
public void invalidate(String userId) { /* ... */ }
public boolean isFull() { /* ... */ }
public UserAccount getLruData() { /* ... */ }
public void clear() { /* ... */ }
public List<UserAccount> getCacheDataInListForm() { /* ... */ }
public void setCapacity(int newCapacity) { /* ... */ }
}
```
अगली परत जिसे हम देखने जा रहे हैं वह `CacheStore` है जो विभिन्न कैशिंग को लागू करती है
रणनीतियाँ।
```java
@Slf4j
public class CacheStore {
private static final int CAPACITY = 3;
private static LruCache cache;
private final DbManager dbManager;
/* ... details omitted ... */
public UserAccount readThrough(final String userId) {
if (cache.contains(userId)) {
LOGGER.info("# Found in Cache!");
return cache.get(userId);
}
LOGGER.info("# Not found in cache! Go to DB!!");
UserAccount userAccount = dbManager.readFromDb(userId);
cache.set(userId, userAccount);
return userAccount;
}
public void writeThrough(final UserAccount userAccount) {
if (cache.contains(userAccount.getUserId())) {
dbManager.updateDb(userAccount);
} else {
dbManager.writeToDb(userAccount);
}
cache.set(userAccount.getUserId(), userAccount);
}
public void writeAround(final UserAccount userAccount) {
if (cache.contains(userAccount.getUserId())) {
dbManager.updateDb(userAccount);
// Cache data has been updated -- remove older
cache.invalidate(userAccount.getUserId());
// version from cache.
} else {
dbManager.writeToDb(userAccount);
}
}
public static void clearCache() {
if (cache != null) {
cache.clear();
}
}
public static void flushCache() {
LOGGER.info("# flushCache...");
Optional.ofNullable(cache)
.map(LruCache::getCacheDataInListForm)
.orElse(List.of())
.forEach(DbManager::updateDb);
}
/* ... omitted the implementation of other caching strategies ... */
}
```
`AppManager` मुख्य वर्ग और एप्लिकेशन के बीच संचार में अंतर को पाटने में मदद करता है
पिछला भाग। इस वर्ग के माध्यम से DB कनेक्शन प्रारंभ किया जाता है। चुनी गई कैशिंग रणनीति/नीति है
यहाँ भी आरंभ किया गया। कैश का उपयोग करने से पहले, कैश का आकार सेट करना होगा। निर्भर करता है
चुनी गई कैशिंग नीति पर, `AppManager` `CacheStore` में उपयुक्त फ़ंक्शन को कॉल करेगा
कक्षा।
```java
@Slf4j
public final class AppManager {
private static CachingPolicy cachingPolicy;
private final DbManager dbManager;
private final CacheStore cacheStore;
private AppManager() {
}
public void initDb() { /* ... */ }
public static void initCachingPolicy(CachingPolicy policy) { /* ... */ }
public static void initCacheCapacity(int capacity) { /* ... */ }
public UserAccount find(final String userId) {
LOGGER.info("Trying to find {} in cache", userId);
if (cachingPolicy == CachingPolicy.THROUGH
|| cachingPolicy == CachingPolicy.AROUND) {
return cacheStore.readThrough(userId);
} else if (cachingPolicy == CachingPolicy.BEHIND) {
return cacheStore.readThroughWithWriteBackPolicy(userId);
} else if (cachingPolicy == CachingPolicy.ASIDE) {
return findAside(userId);
}
return null;
}
public void save(final UserAccount userAccount) {
LOGGER.info("Save record!");
if (cachingPolicy == CachingPolicy.THROUGH) {
cacheStore.writeThrough(userAccount);
} else if (cachingPolicy == CachingPolicy.AROUND) {
cacheStore.writeAround(userAccount);
} else if (cachingPolicy == CachingPolicy.BEHIND) {
cacheStore.writeBehind(userAccount);
} else if (cachingPolicy == CachingPolicy.ASIDE) {
saveAside(userAccount);
}
}
public static String printCacheContent() {
return CacheStore.print();
}
/* ... details omitted ... */
}
```
एप्लिकेशन के मुख्य वर्ग में हम यही करते हैं।
```java
@Slf4j
public class App {
public static void main(final String[] args) {
boolean isDbMongo = isDbMongo(args);
if(isDbMongo){
LOGGER.info("Using the Mongo database engine to run the application.");
} else {
LOGGER.info("Using the 'in Memory' database to run the application.");
}
App app = new App(isDbMongo);
app.useReadAndWriteThroughStrategy();
String splitLine = "==============================================";
LOGGER.info(splitLine);
app.useReadThroughAndWriteAroundStrategy();
LOGGER.info(splitLine);
app.useReadThroughAndWriteBehindStrategy();
LOGGER.info(splitLine);
app.useCacheAsideStategy();
LOGGER.info(splitLine);
}
public void useReadAndWriteThroughStrategy() {
LOGGER.info("# CachingPolicy.THROUGH");
appManager.initCachingPolicy(CachingPolicy.THROUGH);
var userAccount1 = new UserAccount("001", "John", "He is a boy.");
appManager.save(userAccount1);
LOGGER.info(appManager.printCacheContent());
appManager.find("001");
appManager.find("001");
}
public void useReadThroughAndWriteAroundStrategy() { /* ... */ }
public void useReadThroughAndWriteBehindStrategy() { /* ... */ }
public void useCacheAsideStategy() { /* ... */ }
}
```
## क्लास डायग्राम
![alt text](../../../caching/etc/caching.png "Caching")
## प्रयोज्यता
जब कैशिंग पैटर्न का उपयोग करें
* एक ही संसाधन का बार-बार अधिग्रहण, आरंभीकरण और जारी करना अनावश्यक कारण बनता है
प्रदर्शन उपरि.
## संबंधित पैटर्न
* [Proxy](https://java-design-patterns.com/patterns/proxy/)
## श्रेय
* [Write-through, write-around, write-back: Cache explained](http://www.computerweekly.com/feature/Write-through-write-around-write-back-Cache-explained)
* [Read-Through, Write-Through, Write-Behind, and Refresh-Ahead Caching](https://docs.oracle.com/cd/E15357_01/coh.360/e15723/cache_rtwtwbra.htm#COHDG5177)
* [Cache-Aside pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/cache-aside)
* [Java EE 8 High Performance: Master techniques such as memory optimization, caching, concurrency, and multithreading to achieve maximum performance from your enterprise applications](https://www.amazon.com/gp/product/178847306X/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=178847306X&linkId=e948720055599f248cdac47da9125ff4)
* [Java Performance: In-Depth Advice for Tuning and Programming Java 8, 11, and Beyond](https://www.amazon.com/gp/product/1492056111/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1492056111&linkId=7e553581559b9ec04221259e52004b08)
* [Effective Java](https://www.amazon.com/gp/product/B078H61SCH/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=B078H61SCH&linkId=f06607a0b48c76541ef19c5b8b9e7882)
* [Java Performance: The Definitive Guide: Getting the Most Out of Your Code](https://www.amazon.com/gp/product/1449358454/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1449358454&linkId=475c18363e350630cc0b39ab681b2687)
+84
View File
@@ -0,0 +1,84 @@
---
title: Callback
category: Idiom
language: hi
tag:
- Reactive
---
## हेतु
कॉलबैक निष्पादन योग्य कोड का एक टुकड़ा है जिसे अन्य कोड के तर्क के रूप में पारित किया जाता है
किसी सुविधाजनक समय पर तर्क को वापस बुलाने (निष्पादित) करने की अपेक्षा की जाती है।
## व्याख्या
वास्तविक दुनिया का उदाहरण
>कार्य निष्पादन समाप्त होने के बाद हमें सूचित किया जाना आवश्यक है। हम इसके लिए कॉलबैक विधि पास करते हैं
> निष्पादक और उसके हमें वापस बुलाने की प्रतीक्षा करें।
साफ़ शब्दों में
> कॉलबैक निष्पादक को दी गई एक विधि है जिसे निर्धारित समय पर कॉल किया जाएगा।
विकिपीडिया कहता है
> कंप्यूटर प्रोग्रामिंग में, कॉलबैक, जिसे "कॉल-आफ्टर" फ़ंक्शन के रूप में भी जाना जाता है, कोई भी निष्पादन योग्य है
> वह कोड जो अन्य कोड के तर्क के रूप में पारित किया जाता है; उस अन्य कोड को कॉल करने की उम्मीद है
> किसी निश्चित समय पर तर्क को वापस (निष्पादित) करें।
**प्रोग्रामेटिक उदाहरण**
कॉलबैक एकल विधि वाला एक सरल इंटरफ़ेस है।
```java
public interface Callback {
void call();
}
```
आगे हम एक कार्य को परिभाषित करते हैं जो कार्य निष्पादन समाप्त होने के बाद कॉलबैक निष्पादित करेगा।
```java
public abstract class Task {
final void executeWith(Callback callback) {
execute();
Optional.ofNullable(callback).ifPresent(Callback::call);
}
public abstract void execute();
}
@Slf4j
public final class SimpleTask extends Task {
@Override
public void execute() {
LOGGER.info("Perform some important activity and after call the callback method.");
}
}
```
अंत में, यहां बताया गया है कि हम किसी कार्य को कैसे निष्पादित करते हैं और उसके समाप्त होने पर कॉलबैक प्राप्त करते हैं।
```java
var task = new SimpleTask();
task.executeWith(() -> LOGGER.info("I'm done now."));
```
## क्लास डायग्राम
![alt text](../../../callback/etc/callback.png "Callback")
## प्रयोज्यता
जब कॉलबैक पैटर्न का उपयोग करें
* जब कुछ परिभाषित गतिविधि के निष्पादन के बाद कुछ मनमाना सिंक्रोनस या एसिंक्रोनस क्रिया निष्पादित की जानी चाहिए।
## वास्तविक दुनिया के उदाहरण
* [CyclicBarrier](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html#CyclicBarrier%28int,%20java.lang.Runnable%29) कंस्ट्रक्टर एक कॉलबैक स्वीकार कर सकता है जो हर बार बैरियर ट्रिप होने पर ट्रिगर हो जाएगा।
@@ -0,0 +1,173 @@
---
title: Chain of responsibility
category: Behavioral
language: hi
tag:
- Gang of Four
---
## हेतु
एक से अधिक ऑब्जेक्ट को मौका देकर अनुरोध भेजने वाले को उसके प्राप्तकर्ता से जोड़ने से बचें
अनुरोध संभालें. प्राप्त वस्तुओं को श्रृंखलाबद्ध करें और किसी वस्तु तक श्रृंखला के साथ अनुरोध को पास करें
इसे संभालता है.
## व्याख्या
वास्तविक दुनिया का उदाहरण
> ऑर्क किंग अपनी सेना को ऊंचे स्वर में आदेश देता है। फिर, प्रतिक्रिया देने वाला सबसे करीबी व्यक्ति कमांडर होता है
> एक अधिकारी, और फिर एक सैनिक। कमांडर, अधिकारी और सैनिक जिम्मेदारी की एक श्रृंखला बनाते हैं।
साफ़ शब्दों में
> यह वस्तुओं की श्रृंखला बनाने में मदद करता है। एक अनुरोध एक छोर से प्रवेश करता है और एक वस्तु से जाता रहता है
> दूसरे को तब तक भेजें जब तक उसे कोई उपयुक्त हैंडलर न मिल जाए।
विकिपीडिया कहता है
> ऑब्जेक्ट-ओरिएंटेड डिज़ाइन में, श्रृंखला-की-जिम्मेदारी पैटर्न एक डिज़ाइन पैटर्न है जिसमें शामिल है
> कमांड ऑब्जेक्ट का एक स्रोत और प्रोसेसिंग ऑब्जेक्ट की एक श्रृंखला। प्रत्येक प्रोसेसिंग ऑब्जेक्ट में शामिल है
> तर्क जो कमांड ऑब्जेक्ट के प्रकार को परिभाषित करता है जिसे वह संभाल सकता है; बाकी को पास कर दिया गया है
> श्रृंखला में अगली प्रसंस्करण वस्तु।
**प्रोग्रामेटिक उदाहरण**
ऊपर से ओर्क्स के साथ हमारे उदाहरण का अनुवाद करना। सबसे पहले, हमारे पास `Request` वर्ग है:
```java
public class Request {
private final RequestType requestType;
private final String requestDescription;
private boolean handled;
public Request(final RequestType requestType, final String requestDescription) {
this.requestType = Objects.requireNonNull(requestType);
this.requestDescription = Objects.requireNonNull(requestDescription);
}
public String getRequestDescription() { return requestDescription; }
public RequestType getRequestType() { return requestType; }
public void markHandled() { this.handled = true; }
public boolean isHandled() { return this.handled; }
@Override
public String toString() { return getRequestDescription(); }
}
public enum RequestType {
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX
}
```
इसके बाद, हम अनुरोध हैंडलर पदानुक्रम दिखाते हैं।
```java
public interface RequestHandler {
boolean canHandleRequest(Request req);
int getPriority();
void handle(Request req);
String name();
}
@Slf4j
public class OrcCommander implements RequestHandler {
@Override
public boolean canHandleRequest(Request req) {
return req.getRequestType() == RequestType.DEFEND_CASTLE;
}
@Override
public int getPriority() {
return 2;
}
@Override
public void handle(Request req) {
req.markHandled();
LOGGER.info("{} handling request \"{}\"", name(), req);
}
@Override
public String name() {
return "Orc commander";
}
}
// OrcOfficer and OrcSoldier are defined similarly as OrcCommander
```
ऑर्क किंग आदेश देता है और श्रृंखला बनाता है।
```java
public class OrcKing {
private List<RequestHandler> handlers;
public OrcKing() {
buildChain();
}
private void buildChain() {
handlers = Arrays.asList(new OrcCommander(), new OrcOfficer(), new OrcSoldier());
}
public void makeRequest(Request req) {
handlers
.stream()
.sorted(Comparator.comparing(RequestHandler::getPriority))
.filter(handler -> handler.canHandleRequest(req))
.findFirst()
.ifPresent(handler -> handler.handle(req));
}
}
```
कार्रवाई में जिम्मेदारी की श्रृंखला.
```java
var king = new OrcKing();
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
```
कंसोल आउटपुट.
```
Orc commander handling request "defend castle"
Orc officer handling request "torture prisoner"
Orc soldier handling request "collect tax"
```
## क्लास डायग्राम
![alt text](../../../chain-of-responsibility/etc/chain-of-responsibility.urm.png "Chain of Responsibility class diagram")
## प्रयोज्यता
जिम्मेदारी की श्रृंखला का प्रयोग कब करें
* एक से अधिक ऑब्जेक्ट एक अनुरोध को संभाल सकते हैं, और हैंडलर को पहले से ज्ञात नहीं है। हैंडलर का स्वचालित रूप से पता लगाया जाना चाहिए।
* आप रिसीवर को स्पष्ट रूप से निर्दिष्ट किए बिना कई ऑब्जेक्ट में से एक के लिए अनुरोध जारी करना चाहते हैं।
* ऑब्जेक्ट का सेट जो अनुरोध को संभाल सकता है उसे गतिशील रूप से निर्दिष्ट किया जाना चाहिए।
## ज्ञात उपयोग
* [java.util.logging.Logger#log()](http://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html#log%28java.util.logging.Level,%20java.lang.String%29)
* [Apache Commons Chain](https://commons.apache.org/proper/commons-chain/index.html)
* [javax.servlet.Filter#doFilter()](http://docs.oracle.com/javaee/7/api/javax/servlet/Filter.html#doFilter-javax.servlet.ServletRequest-javax.servlet.ServletResponse-javax.servlet.FilterChain-)
## श्रेय
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
+326
View File
@@ -0,0 +1,326 @@
---
title: Circuit Breaker
category: Behavioral
language: hi
tag:
- Performance
- Decoupling
- Cloud distributed
---
## हेतु
महँगी दूरस्थ सेवा कॉलों को इस प्रकार संभालें कि किसी एक सेवा/घटक की विफलता हो
संपूर्ण एप्लिकेशन को नीचे नहीं लाया जा सकता है, और हम यथाशीघ्र सेवा से पुनः कनेक्ट कर सकते हैं।
## व्याख्या
वास्तविक दुनिया का उदाहरण
> एक ऐसे वेब एप्लिकेशन की कल्पना करें जिसमें स्थानीय फ़ाइलें/छवियां और दूरस्थ सेवाएं दोनों हैं जिनका उपयोग किया जाता है
> डेटा लाया जा रहा है। ये दूरस्थ सेवाएँ कभी-कभी स्वस्थ और उत्तरदायी हो सकती हैं, या बन सकती हैं
> विभिन्न कारणों से किसी समय धीमी और अनुत्तरदायी। तो अगर रिमोट में से एक
> सेवाएँ धीमी हैं या सफलतापूर्वक प्रतिक्रिया नहीं दे रही हैं, हमारा एप्लिकेशन प्रतिक्रिया प्राप्त करने का प्रयास करेगा
> कई थ्रेड्स/प्रक्रियाओं का उपयोग करने वाली दूरस्थ सेवा, जल्द ही वे सभी हैंग हो जाएंगी (जिन्हें भी कहा जाता है)।
> [Thread starvation](https://en.wikipedia.org/wiki/Starvation_(computer_science)) जिसके कारण हमारा संपूर्ण वेब एप्लिकेशन क्रैश हो गया है। हमें पता लगाने में सक्षम होना चाहिए
> यह स्थिति और उपयोगकर्ता को एक उपयुक्त संदेश दिखाएं ताकि वह इसके अन्य हिस्सों का पता लगा सके
> ऐप दूरस्थ सेवा विफलता से अप्रभावित है। इस बीच, अन्य सेवाएँ जो काम कर रही हैं
> सामान्यतः, इस विफलता से अप्रभावित रहकर कार्य करते रहना चाहिए।
साफ़ शब्दों में
> सर्किट ब्रेकर विफल दूरस्थ सेवाओं को शानदार ढंग से संभालने की अनुमति देता है। यह विशेष रूप से तब उपयोगी होता है जब
> हमारे एप्लिकेशन के सभी हिस्से एक-दूसरे से अत्यधिक अलग हैं, और एक घटक विफल हो गया है
> इसका मतलब यह नहीं है कि अन्य हिस्से काम करना बंद कर देंगे।
विकिपीडिया कहता है
> सर्किट ब्रेकर आधुनिक सॉफ्टवेयर विकास में उपयोग किया जाने वाला एक डिज़ाइन पैटर्न है। इसका उपयोग पता लगाने के लिए किया जाता है
> विफलताएं और विफलता को लगातार दोहराए जाने से रोकने के तर्क को समाहित करती है
> रखरखाव, अस्थायी बाहरी सिस्टम विफलता या अप्रत्याशित सिस्टम कठिनाइयाँ।
## प्रोग्रामेटिक उदाहरण
तो, यह सब एक साथ कैसे आता है? उपरोक्त उदाहरण को ध्यान में रखते हुए हम इसका अनुकरण करेंगे
एक सरल उदाहरण में कार्यक्षमता. एक निगरानी सेवा वेब ऐप की नकल करती है और स्थानीय और दोनों बनाती है
दूरस्थ कॉल.
सेवा वास्तुकला इस प्रकार है:
![alt text](../../../circuit-breaker/etc/ServiceDiagram.png "Service Diagram")
कोड के संदर्भ में, अंतिम उपयोगकर्ता एप्लिकेशन है:
```java
@Slf4j
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
/**
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
var serverStartTime = System.nanoTime();
var delayedService = new DelayedRemoteService(serverStartTime, 5);
var delayedServiceCircuitBreaker = new DefaultCircuitBreaker(delayedService, 3000, 2,
2000 * 1000 * 1000);
var quickService = new QuickRemoteService();
var quickServiceCircuitBreaker = new DefaultCircuitBreaker(quickService, 3000, 2,
2000 * 1000 * 1000);
//Create an object of monitoring service which makes both local and remote calls
var monitoringService = new MonitoringService(delayedServiceCircuitBreaker,
quickServiceCircuitBreaker);
//Fetch response from local resource
LOGGER.info(monitoringService.localResourceResponse());
//Fetch response from delayed service 2 times, to meet the failure threshold
LOGGER.info(monitoringService.delayedServiceResponse());
LOGGER.info(monitoringService.delayedServiceResponse());
//Fetch current state of delayed service circuit breaker after crossing failure threshold limit
//which is OPEN now
LOGGER.info(delayedServiceCircuitBreaker.getState());
//Meanwhile, the delayed service is down, fetch response from the healthy quick service
LOGGER.info(monitoringService.quickServiceResponse());
LOGGER.info(quickServiceCircuitBreaker.getState());
//Wait for the delayed service to become responsive
try {
LOGGER.info("Waiting for delayed service to become responsive");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//Check the state of delayed circuit breaker, should be HALF_OPEN
LOGGER.info(delayedServiceCircuitBreaker.getState());
//Fetch response from delayed service, which should be healthy by now
LOGGER.info(monitoringService.delayedServiceResponse());
//As successful response is fetched, it should be CLOSED again.
LOGGER.info(delayedServiceCircuitBreaker.getState());
}
}
```
निगरानी सेवा:
```java
public class MonitoringService {
private final CircuitBreaker delayedService;
private final CircuitBreaker quickService;
public MonitoringService(CircuitBreaker delayedService, CircuitBreaker quickService) {
this.delayedService = delayedService;
this.quickService = quickService;
}
//Assumption: Local service won't fail, no need to wrap it in a circuit breaker logic
public String localResourceResponse() {
return "Local Service is working";
}
/**
* Fetch response from the delayed service (with some simulated startup time).
*
* @return response string
*/
public String delayedServiceResponse() {
try {
return this.delayedService.attemptRequest();
} catch (RemoteServiceException e) {
return e.getMessage();
}
}
/**
* Fetches response from a healthy service without any failure.
*
* @return response string
*/
public String quickServiceResponse() {
try {
return this.quickService.attemptRequest();
} catch (RemoteServiceException e) {
return e.getMessage();
}
}
}
```
जैसा कि देखा जा सकता है, यह स्थानीय संसाधनों को सीधे प्राप्त करने के लिए कॉल करता है, लेकिन यह कॉल को लपेट देता है
सर्किट ब्रेकर ऑब्जेक्ट में रिमोट (महंगी) सेवा, जो निम्नानुसार दोषों को रोकती है:
```java
public class DefaultCircuitBreaker implements CircuitBreaker {
private final long timeout;
private final long retryTimePeriod;
private final RemoteService service;
long lastFailureTime;
private String lastFailureResponse;
int failureCount;
private final int failureThreshold;
private State state;
private final long futureTime = 1000 * 1000 * 1000 * 1000;
/**
* Constructor to create an instance of Circuit Breaker.
*
* @param timeout Timeout for the API request. Not necessary for this simple example
* @param failureThreshold Number of failures we receive from the depended service before changing
* state to 'OPEN'
* @param retryTimePeriod Time period after which a new request is made to remote service for
* status check.
*/
DefaultCircuitBreaker(RemoteService serviceToCall, long timeout, int failureThreshold,
long retryTimePeriod) {
this.service = serviceToCall;
// We start in a closed state hoping that everything is fine
this.state = State.CLOSED;
this.failureThreshold = failureThreshold;
// Timeout for the API request.
// Used to break the calls made to remote resource if it exceeds the limit
this.timeout = timeout;
this.retryTimePeriod = retryTimePeriod;
//An absurd amount of time in future which basically indicates the last failure never happened
this.lastFailureTime = System.nanoTime() + futureTime;
this.failureCount = 0;
}
// Reset everything to defaults
@Override
public void recordSuccess() {
this.failureCount = 0;
this.lastFailureTime = System.nanoTime() + futureTime;
this.state = State.CLOSED;
}
@Override
public void recordFailure(String response) {
failureCount = failureCount + 1;
this.lastFailureTime = System.nanoTime();
// Cache the failure response for returning on open state
this.lastFailureResponse = response;
}
// Evaluate the current state based on failureThreshold, failureCount and lastFailureTime.
protected void evaluateState() {
if (failureCount >= failureThreshold) { //Then something is wrong with remote service
if ((System.nanoTime() - lastFailureTime) > retryTimePeriod) {
//We have waited long enough and should try checking if service is up
state = State.HALF_OPEN;
} else {
//Service would still probably be down
state = State.OPEN;
}
} else {
//Everything is working fine
state = State.CLOSED;
}
}
@Override
public String getState() {
evaluateState();
return state.name();
}
/**
* Break the circuit beforehand if it is known service is down Or connect the circuit manually if
* service comes online before expected.
*
* @param state State at which circuit is in
*/
@Override
public void setState(State state) {
this.state = state;
switch (state) {
case OPEN -> {
this.failureCount = failureThreshold;
this.lastFailureTime = System.nanoTime();
}
case HALF_OPEN -> {
this.failureCount = failureThreshold;
this.lastFailureTime = System.nanoTime() - retryTimePeriod;
}
default -> this.failureCount = 0;
}
}
/**
* Executes service call.
*
* @return Value from the remote resource, stale response or a custom exception
*/
@Override
public String attemptRequest() throws RemoteServiceException {
evaluateState();
if (state == State.OPEN) {
// return cached response if the circuit is in OPEN state
return this.lastFailureResponse;
} else {
// Make the API request if the circuit is not OPEN
try {
//In a real application, this would be run in a thread and the timeout
//parameter of the circuit breaker would be utilized to know if service
//is working. Here, we simulate that based on server response itself
var response = service.call();
// Yay!! the API responded fine. Let's reset everything.
recordSuccess();
return response;
} catch (RemoteServiceException ex) {
recordFailure(ex.getMessage());
throw ex;
}
}
}
}
```
उपरोक्त पैटर्न विफलताओं को कैसे रोकता है? आइए इस परिमित राज्य मशीन के माध्यम से समझें
इसके द्वारा कार्यान्वित किया गया।
![alt text](../../../circuit-breaker/etc/StateDiagram.png "State Diagram")
- हम सर्किट ब्रेकर ऑब्जेक्ट को कुछ मापदंडों के साथ आरंभ करते हैं: `timeout`, `failureThreshold` और `retryTimePeriod` जो यह निर्धारित करने में मदद करते हैं कि एपीआई कितना लचीला है।
- प्रारंभ में, हम `closed` स्थिति में हैं और एपीआई पर कोई दूरस्थ कॉल नहीं हुई है।
- हर बार जब कॉल सफल हो जाती है, तो हम स्थिति को उसी स्थिति में रीसेट कर देते हैं जैसी वह शुरुआत में थी।
- यदि विफलताओं की संख्या एक निश्चित सीमा को पार कर जाती है, तो हम `open` स्थिति में चले जाते हैं, जो एक ओपन सर्किट की तरह कार्य करता है और दूरस्थ सेवा कॉल को करने से रोकता है, इस प्रकार संसाधनों की बचत होती है। (यहां, हम ```stale response from API``` नामक प्रतिक्रिया लौटाते हैं)
- एक बार जब हम पुन: प्रयास की समय-सीमा पार कर लेते हैं, तो हम `half-open` स्थिति में चले जाते हैं और यह जांचने के लिए दूरस्थ सेवा पर दोबारा कॉल करते हैं कि सेवा काम कर रही है या नहीं ताकि हम ताजा सामग्री पेश कर सकें। एक विफलता इसे वापस `open` स्थिति में सेट कर देती है और दूसरा प्रयास पुनः प्रयास की समयावधि के बाद किया जाता है, जबकि एक सफलता इसे `closed` स्थिति में सेट कर देती है ताकि सब कुछ फिर से सामान्य रूप से काम करना शुरू कर दे।
## क्लास डायग्राम
![alt text](../../../circuit-breaker/etc/circuit-breaker.urm.png "Circuit Breaker class diagram")
## प्रयोज्यता
जब सर्किट ब्रेकर पैटर्न का उपयोग करें
- एक दोष-सहिष्णु एप्लिकेशन का निर्माण जहां कुछ सेवाओं की विफलता से संपूर्ण एप्लिकेशन बंद नहीं होना चाहिए।
- लगातार चलने वाले (हमेशा चालू रहने वाले) एप्लिकेशन का निर्माण करना, ताकि इसके घटकों को पूरी तरह से बंद किए बिना अपग्रेड किया जा सके।
## संबंधित पैटर्न
- [Retry Pattern](https://github.com/iluwatar/java-design-patterns/tree/master/retry)
## वास्तविक दुनिया के उदाहरण
* [Spring Circuit Breaker module](https://spring.io/guides/gs/circuit-breaker)
* [Netflix Hystrix API](https://github.com/Netflix/Hystrix)
## श्रेय
* [Understanding Circuit Breaker Pattern](https://itnext.io/understand-circuitbreaker-design-pattern-with-simple-practical-example-92a752615b42)
* [Martin Fowler on Circuit Breaker](https://martinfowler.com/bliki/CircuitBreaker.html)
* [Fault tolerance in a high volume, distributed system](https://medium.com/netflix-techblog/fault-tolerance-in-a-high-volume-distributed-system-91ab4faae74a)
* [Circuit Breaker pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/circuit-breaker)
+114
View File
@@ -0,0 +1,114 @@
---
title: Client Session Pattern
category: Architectural
language: hi
tags:
- Decoupling
---
## नाम
[Client Session pattern](https://dzone.com/articles/practical-php-patterns/practical-php-patterns-client)
## हेतु
- स्टेटलेस सर्वर बनाएं जो क्लस्टरिंग की समस्या को दूर करता है, क्योंकि उपयोगकर्ता सर्वर के बीच निर्बाध रूप से स्विच कर सकते हैं।
- सर्वर फेल-ओवर की स्थिति में डेटा को अधिक लचीला बनाता है।
- छोटे डेटा आकार के साथ अच्छा काम करता है।
## व्याख्या
वास्तविक दुनिया का उदाहरण
> आप एक डेटा प्रबंधन ऐप बनाना चाह रहे हैं जो उपयोगकर्ताओं को सर्वर पर अनुरोध भेजने की अनुमति दे
> अपने उपकरणों पर संग्रहीत डेटा को संशोधित और परिवर्तित करें। ये अनुरोध आकार में छोटे हैं और
> बड़े पैमाने पर डेटाबेस कार्यान्वयन की आवश्यकता को नकारते हुए, डेटा प्रत्येक उपयोगकर्ता के लिए अलग-अलग होता है।
> क्लाइंट सत्र पैटर्न का उपयोग करके, आप एकाधिक समवर्ती अनुरोधों, लोड को संभालने में सक्षम हैं
> सर्वर के स्टेटलेस रहने के कारण विभिन्न सर्वरों पर क्लाइंट को आसानी से संतुलित करना। आप भी ना
> क्लाइंट द्वारा सभी सुविधाएं प्रदान करने के कारण सर्वर साइड पर सत्र आईडी संग्रहीत करने की आवश्यकता को हटा दें
> वह जानकारी जो सर्वर को अपनी प्रक्रिया निष्पादित करने के लिए आवश्यक होती है।
साफ़ शब्दों में
> मौजूदा क्लाइंट और उस पर एक्सेस की जा रही जानकारी के बारे में जानकारी संग्रहीत करने के बजाय
> सर्वर, इसे केवल क्लाइंट साइड में ही बनाए रखा जाता है। क्लाइंट को प्रत्येक अनुरोध के साथ सत्र डेटा भेजना होगा
> सर्वर को क्लाइंट को एक अद्यतन स्थिति वापस भेजनी होती है, जो क्लाइंट पर संग्रहीत होती है
> मशीन. सर्वर को क्लाइंट जानकारी संग्रहीत करने की आवश्यकता नहीं है।
> ([ref](https://dzone.com/articles/practical-php-patterns/practical-php-patterns-client))
**प्रोग्रामेटिक उदाहरण**
क्लाइंट-सत्र पैटर्न का वर्णन करने के लिए नमूना कोड यहां दिया गया है। नीचे दिए गए कोड में हम पहले हैं
सर्वर का एक उदाहरण बनाना। इस सर्वर इंस्टेंस का उपयोग सत्र ऑब्जेक्ट प्राप्त करने के लिए किया जाएगा
दो ग्राहकों के लिए. जैसा कि आप नीचे दिए गए कोड से देख सकते हैं, सेशन ऑब्जेक्ट का उपयोग किसी भी चीज़ को स्टोर करने के लिए किया जा सकता है
प्रासंगिक जानकारी जो क्लाइंट अनुरोध को संसाधित करने के लिए सर्वर द्वारा आवश्यक है। ये सत्र
फिर प्रत्येक अनुरोध के साथ ऑब्जेक्ट को सर्वर पर भेज दिया जाएगा। अनुरोध में सत्र होगा
ऑब्जेक्ट जो प्रसंस्करण के लिए आवश्यक डेटा के साथ प्रासंगिक ग्राहक विवरण संग्रहीत करता है
अनुरोध। प्रत्येक अनुरोध में सत्र की जानकारी सर्वर को क्लाइंट और प्रक्रिया की पहचान करने में मदद करती है
तदनुसार अनुरोध.
```java
public class App {
public static void main(String[] args) {
var server = new Server("localhost", 8080);
var session1 = server.getSession("Session1");
var session2 = server.getSession("Session2");
var request1 = new Request("Data1", session1);
var request2 = new Request("Data2", session2);
server.process(request1);
server.process(request2);
}
}
@Data
@AllArgsConstructor
public class Session {
/**
* Session id.
*/
private String id;
/**
* Client name.
*/
private String clientName;
}
@Data
@AllArgsConstructor
public class Request {
private String data;
private Session session;
}
```
## वास्तुकला आरेख
![alt text](../../../client-session/etc/session_state_pattern.png "Session State Pattern")
## प्रयोज्यता
क्लाइंट स्थिति पैटर्न का उपयोग तब करें जब:
- बड़े अनुरोधों और प्रतिक्रिया आकारों को रोकने के लिए छोटी मात्रा में डेटा संसाधित करना।
- क्लाइंट स्थिति को सहेजने के लिए सर्वर की आवश्यकता को हटा दें। ऐसा करने से सत्र आईडी संग्रहीत करने की आवश्यकता भी समाप्त हो जाती है।
- क्लस्टरिंग एक मुद्दा है और इससे बचा जाना चाहिए। स्टेटलेस सर्वर क्लाइंट को सर्वर पर आसानी से वितरित करने की अनुमति देते हैं।
- सर्वर विफलता के कारण डेटा हानि से लचीलापन बनाता है।
## नतीजे
- सर्वर स्टेटलेस है. कोई भी कंप्यूट एपीआई कोई डेटा संग्रहीत नहीं करेगा।
- बड़ी मात्रा में डेटा से निपटने के लिए संघर्ष करना पड़ता है। प्रबंधित करने के लिए सत्र डेटा की बड़ी मात्रा के कारण भेजने और प्राप्त करने में अधिक समय लगता है।
- सुरक्षा। सारा डेटा क्लाइंट की मशीन पर संग्रहीत होता है। इसका मतलब यह है कि क्लाइंट की ओर से कोई भी भेद्यता सर्वर द्वारा भेजे और प्राप्त किए जा रहे सभी डेटा को उजागर कर सकती है।
## श्रेय
- [Dzone - Practical PHP patterns](https://dzone.com/articles/practical-php-patterns/practical-php-patterns-client)
- [Client Session State Design Pattern - Ram N Java](https://www.youtube.com/watch?v=ycOSj9g41pc)
@@ -0,0 +1,196 @@
---
title: Collecting Parameter
category: Idiom
language: hi
tag:
- Generic
---
## नाम
पैरामीटर एकत्रित करना
## हेतु
एक संग्रह के भीतर अनेक विधियों के सहयोगात्मक परिणाम को संग्रहीत करना।
## व्याख्या
### वास्तविक दुनिया का उदाहरण
एक बड़े कॉर्पोरेट भवन के भीतर, एक वैश्विक प्रिंटर कतार मौजूद है जो सभी मुद्रण कार्यों का एक संग्रह है
जो फिलहाल लंबित हैं। विभिन्न मंजिलों पर प्रिंटर के अलग-अलग मॉडल होते हैं, प्रत्येक की अलग-अलग प्रिंटिंग होती है
नीति। हमें एक ऐसे प्रोग्राम का निर्माण करना चाहिए जो किसी संग्रह में लगातार उपयुक्त मुद्रण कार्य जोड़ सके, जिसे *कलेक्टिंग पैरामीटर* कहा जाता है।
### साफ़ शब्दों में
एक विशाल विधि के बजाय जिसमें एक चर में जानकारी एकत्र करने के लिए कई नीतियां शामिल हैं, हम ऐसा कर सकते हैं
कई छोटे फ़ंक्शन बनाएं जो प्रत्येक पैरामीटर लें, और नई जानकारी जोड़ें। हम पैरामीटर को पास कर सकते हैं
ये सभी छोटे कार्य और अंत तक, हमें वही मिलेगा जो हम मूल रूप से चाहते थे। इस बार, कोड साफ़ है
और समझने में आसान है. क्योंकि बड़े फ़ंक्शन को तोड़ दिया गया है, परिवर्तन के रूप में कोड को संशोधित करना भी आसान है
छोटे कार्यों के लिए स्थानीयकृत हैं।
### विकिपीडिया कहता है
कलेक्टिंग पैरामीटर मुहावरे में एक संग्रह (सूची, मानचित्र, आदि) को एक विधि के पैरामीटर के रूप में बार-बार पारित किया जाता है जो संग्रह में आइटम जोड़ता है।
### प्रोग्रामेटिक उदाहरण
ऊपर से हमारे उदाहरण को कोड करते हुए, हम संग्रह `result` को एक संग्रहण पैरामीटर के रूप में उपयोग कर सकते हैं। निम्नलिखित प्रतिबंध
कार्यान्वित हैं:
- यदि A4 कागज रंगीन है, तो वह भी एक तरफा होना चाहिए। अन्य सभी गैर-रंगीन कागज स्वीकार किए जाते हैं
- A3 कागज़ गैर-रंगीन और एक तरफा होने चाहिए
- A2 पेपर एकल-पृष्ठ, एकल-पक्षीय और गैर-रंगीन होने चाहिए
```java
package com.iluwatar.collectingparameter;
import java.util.LinkedList;
import java.util.Queue;
public class App {
static PrinterQueue printerQueue = PrinterQueue.getInstance();
/**
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
/*
Initialising the printer queue with jobs
*/
printerQueue.addPrinterItem(new PrinterItem(PaperSizes.A4, 5, false, false));
printerQueue.addPrinterItem(new PrinterItem(PaperSizes.A3, 2, false, false));
printerQueue.addPrinterItem(new PrinterItem(PaperSizes.A2, 5, false, false));
/*
This variable is the collecting parameter.
*/
var result = new LinkedList<PrinterItem>();
/*
* Using numerous sub-methods to collaboratively add information to the result collecting parameter
*/
addA4Papers(result);
addA3Papers(result);
addA2Papers(result);
}
}
```
हम `result` संग्रहण पैरामीटर को पॉप्युलेट करने के लिए `addA4Paper`, `addA3Paper`, और `addA2Paper` विधियों का उपयोग करते हैं
पहले वर्णित नीति के अनुसार उपयुक्त मुद्रण कार्य। तीन नीतियां नीचे एन्कोड की गई हैं,
```java
public class App {
static PrinterQueue printerQueue = PrinterQueue.getInstance();
/**
* Adds A4 document jobs to the collecting parameter according to some policy that can be whatever the client
* (the print center) wants.
*
* @param printerItemsCollection the collecting parameter
*/
public static void addA4Papers(Queue<PrinterItem> printerItemsCollection) {
/*
Iterate through the printer queue, and add A4 papers according to the correct policy to the collecting parameter,
which is 'printerItemsCollection' in this case.
*/
for (PrinterItem nextItem : printerQueue.getPrinterQueue()) {
if (nextItem.paperSize.equals(PaperSizes.A4)) {
var isColouredAndSingleSided = nextItem.isColour && !nextItem.isDoubleSided;
if (isColouredAndSingleSided) {
printerItemsCollection.add(nextItem);
} else if (!nextItem.isColour) {
printerItemsCollection.add(nextItem);
}
}
}
}
/**
* Adds A3 document jobs to the collecting parameter according to some policy that can be whatever the client
* (the print center) wants. The code is similar to the 'addA4Papers' method. The code can be changed to accommodate
* the wants of the client.
*
* @param printerItemsCollection the collecting parameter
*/
public static void addA3Papers(Queue<PrinterItem> printerItemsCollection) {
for (PrinterItem nextItem : printerQueue.getPrinterQueue()) {
if (nextItem.paperSize.equals(PaperSizes.A3)) {
// Encoding the policy into a Boolean: the A3 paper cannot be coloured and double-sided at the same time
var isNotColouredAndSingleSided = !nextItem.isColour && !nextItem.isDoubleSided;
if (isNotColouredAndSingleSided) {
printerItemsCollection.add(nextItem);
}
}
}
}
/**
* Adds A2 document jobs to the collecting parameter according to some policy that can be whatever the client
* (the print center) wants. The code is similar to the 'addA4Papers' method. The code can be changed to accommodate
* the wants of the client.
*
* @param printerItemsCollection the collecting parameter
*/
public static void addA2Papers(Queue<PrinterItem> printerItemsCollection) {
for (PrinterItem nextItem : printerQueue.getPrinterQueue()) {
if (nextItem.paperSize.equals(PaperSizes.A2)) {
// Encoding the policy into a Boolean: the A2 paper must be single page, single-sided, and non-coloured.
var isNotColouredSingleSidedAndOnePage = nextItem.pageCount == 1 && !nextItem.isDoubleSided
&& !nextItem.isColour;
if (isNotColouredSingleSidedAndOnePage) {
printerItemsCollection.add(nextItem);
}
}
}
}
}
```
प्रत्येक विधि एक संग्रह पैरामीटर को तर्क के रूप में लेती है। इसके बाद यह वैश्विक चर से लिए गए तत्वों को जोड़ता है,
यदि प्रत्येक तत्व किसी दिए गए मानदंड को पूरा करता है तो इस संग्रहण पैरामीटर पर। इन विधियों में ग्राहक की इच्छानुसार कोई भी नीति हो सकती है।
इस प्रोग्रामेटिक उदाहरण में, तीन प्रिंट कार्य कतार में जोड़े गए हैं। केवल पहले दो प्रिंट कार्य ही जोड़े जाने चाहिए
नीति के अनुसार संग्रहण पैरामीटर। निष्पादन के बाद `result` चर के तत्व हैं,
| paperSize | pageCount | isDoubleSided | isColour |
|-----------|-----------|---------------|----------|
| A4 | 5 | false | false |
| A3 | 2 | false | false |
जिसकी हमें अपेक्षा थी।
## क्लास डायग्राम
![alt text](../../../collecting-parameter/etc/collecting-parameter.urm.png "Collecting Parameter")
## प्रयोज्यता
जब कलेक्टिंग पैरामीटर डिज़ाइन पैटर्न का उपयोग करें
- आप एक संग्रह या ऑब्जेक्ट वापस करना चाहते हैं जो कई विधियों का सहयोगात्मक परिणाम है
- आप एक ऐसी विधि को सरल बनाना चाहते हैं जो डेटा जमा करती है क्योंकि मूल विधि बहुत जटिल है
## ट्यूटोरियल
इस विधि के लिए ट्यूटोरियल यहां पाए जाते हैं:
- [रिफैक्टरिंग टू पैटर्न्स](http://www.tarrani.net/RefactoringToPatterns.pdf) जोशुआ केरिवस्की द्वारा
- [स्मॉलटॉक सर्वोत्तम अभ्यास पैटर्न] (https://ptgmedia.pearsoncmg.com/images/9780134769042/samplepages/013476904X.pdf) केंट बेक द्वारा
## ज्ञात उपयोग
जोशुआ केरिव्स्की ने अपनी पुस्तक 'रिफैक्टरिंग टू पैटर्न्स' में वास्तविक दुनिया का उदाहरण दिया है। वह इसका उपयोग करने का एक उदाहरण देता है
XML ट्री के लिए `toString()` विधि बनाने के लिए पैरामीटर डिज़ाइन पैटर्न एकत्रित करना। इस डिज़ाइन पैटर्न का उपयोग किए बिना,
इसके लिए सशर्त और संयोजन के साथ एक भारी फ़ंक्शन की आवश्यकता होगी जो कोड पठनीयता को खराब कर देगा। ऐसी विधि
इसे छोटी-छोटी विधियों में विभाजित किया जा सकता है, जिनमें से प्रत्येक जानकारी के अपने स्वयं के सेट को संग्रहण पैरामीटर में जोड़ता है।
इसे [रिफैक्टरिंग टू पैटर्न](http://www.tarrani.net/RefactoringToPatterns.pdf) में देखें।
## नतीजे
पेशेवर:
- कोड को अधिक पठनीय बनाता है
- 'लिंकेज' से बचें, जहां कई विधियां एक ही वैश्विक चर का संदर्भ देती हैं
- बड़े कार्यों को विघटित करके रखरखाव बढ़ाता है
दोष:
- कोड की लंबाई बढ़ सकती है
- तरीकों की 'परतें' जोड़ता है
## संबंधित पैटर्न
- [Compose Methods](https://www.geeksforgeeks.org/composite-design-pattern/)
## श्रेय
निम्नलिखित पुस्तकों का उपयोग किया गया:
- [Refactoring To Patterns](http://www.tarrani.net/RefactoringToPatterns.pdf) जोशुआ केरिवस्की द्वारा
- [Smalltalk Best Practice Patterns](https://ptgmedia.pearsoncmg.com/images/9780134769042/samplepages/013476904X.pdf) केंट बेक द्वारा
साइटें:
- [Wiki](https://wiki.c2.com/?CollectingParameter)
@@ -0,0 +1,27 @@
---
title: Collection Pipeline
category: Functional
language: hi
tag:
- Reactive
---
## हेतु
कलेक्शन पाइपलाइन फ़ंक्शन कंपोज़िशन और कलेक्शन पाइपलाइन पेश करती है, दो कार्यात्मक-शैली पैटर्न जिन्हें आप अपने कोड में संग्रहों को पुनरावृत्त करने के लिए जोड़ सकते हैं।
कार्यात्मक प्रोग्रामिंग में, छोटे मॉड्यूलर कार्यों या संचालन की एक श्रृंखला के माध्यम से जटिल संचालन को अनुक्रमित करना आम बात है। श्रृंखला को फ़ंक्शंस की संरचना, या फ़ंक्शंस संरचना कहा जाता है। जब डेटा का संग्रह किसी फ़ंक्शन संरचना के माध्यम से प्रवाहित होता है, तो यह एक संग्रह पाइपलाइन बन जाता है। फ़ंक्शन संरचना और संग्रह पाइपलाइन दो डिज़ाइन पैटर्न हैं जिनका उपयोग अक्सर कार्यात्मक-शैली प्रोग्रामिंग में किया जाता है।
## क्लास डायग्राम
![alt text](../../../collection-pipeline/etc/collection-pipeline.png "Collection Pipeline")
## प्रयोज्यता
जब संग्रह पाइपलाइन पैटर्न का उपयोग करें
* जब आप ऑपरेशनों का एक क्रम निष्पादित करना चाहते हैं जहां एक ऑपरेशन का एकत्रित आउटपुट अगले में फीड किया जाता है
* जब आप अपने कोड में बहुत सारे स्टेटमेंट का उपयोग करते हैं
* जब आप अपने कोड में बहुत सारे लूप का उपयोग करते हैं
## श्रेय
* [Function composition and the Collection Pipeline pattern](https://www.ibm.com/developerworks/library/j-java8idioms2/index.html)
* [Martin Fowler](https://martinfowler.com/articles/collection-pipeline/)
* [Java8 Streams](https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html)
+208
View File
@@ -0,0 +1,208 @@
---
title: Combinator
category: Idiom
language: hi
tag:
- Reactive
---
## दूसरा नाम
संयोजन पैटर्न
## हेतु
कार्यों के संयोजन के विचार पर केंद्रित पुस्तकालयों को व्यवस्थित करने की शैली का प्रतिनिधित्व करने वाला कार्यात्मक पैटर्न।
इसे सीधे शब्दों में कहें तो, कुछ प्रकार टी हैं, प्रकार टी के "आदिम" मूल्यों के निर्माण के लिए कुछ कार्य हैं, और कुछ "कॉम्बिनेटर" हैं जो प्रकार टी के अधिक जटिल मूल्यों को बनाने के लिए विभिन्न तरीकों से प्रकार टी के मूल्यों को जोड़ सकते हैं।
## व्याख्या
वास्तविक दुनिया का उदाहरण
> कंप्यूटर विज्ञान में, कॉम्बिनेटरी लॉजिक का उपयोग गणना के सरलीकृत मॉडल के रूप में किया जाता है, जिसका उपयोग कंप्यूटेबिलिटी सिद्धांत और प्रूफ सिद्धांत में किया जाता है। अपनी सरलता के बावजूद, संयोजनात्मक तर्क गणना की कई आवश्यक विशेषताओं को पकड़ लेता है।
>
साफ़ शब्दों में
> कॉम्बिनेटर आपको पहले से परिभाषित "things" से नई "things" बनाने की अनुमति देता है।
>
विकिपीडिया कहता है
> कॉम्बिनेटर एक उच्च-क्रम वाला फ़ंक्शन है जो अपने तर्कों से परिणाम को परिभाषित करने के लिए केवल फ़ंक्शन एप्लिकेशन और पहले से परिभाषित कॉम्बिनेटर का उपयोग करता है।
>
**प्रोग्रामेटिक उदाहरण**
उपरोक्त कॉम्बिनेटर उदाहरण का अनुवाद करना। सबसे पहले, हमारे पास एक इंटरफ़ेस है जिसमें कई विधियाँ `contains`, `not`, `or`, `and` शामिल हैं।
```java
// Functional interface to find lines in text.
public interface Finder {
// The function to find lines in text.
List<String> find(String text);
// Simple implementation of function {@link #find(String)}.
static Finder contains(String word) {
return txt -> Stream.of(txt.split("\n"))
.filter(line -> line.toLowerCase().contains(word.toLowerCase()))
.collect(Collectors.toList());
}
// combinator not.
default Finder not(Finder notFinder) {
return txt -> {
List<String> res = this.find(txt);
res.removeAll(notFinder.find(txt));
return res;
};
}
// combinator or.
default Finder or(Finder orFinder) {
return txt -> {
List<String> res = this.find(txt);
res.addAll(orFinder.find(txt));
return res;
};
}
// combinator and.
default Finder and(Finder andFinder) {
return
txt -> this
.find(txt)
.stream()
.flatMap(line -> andFinder.find(line).stream())
.collect(Collectors.toList());
}
...
}
```
फिर हमारे पास कुछ जटिल खोजकों `advancedFinder`, `filteredFinder`, `specializedFinder` and `expandedFinder` के लिए एक और कॉम्बिनेटर भी है।
```java
// Complex finders consisting of simple finder.
public class Finders {
private Finders() {
}
// Finder to find a complex query.
public static Finder advancedFinder(String query, String orQuery, String notQuery) {
return
Finder.contains(query)
.or(Finder.contains(orQuery))
.not(Finder.contains(notQuery));
}
// Filtered finder looking a query with excluded queries as well.
public static Finder filteredFinder(String query, String... excludeQueries) {
var finder = Finder.contains(query);
for (String q : excludeQueries) {
finder = finder.not(Finder.contains(q));
}
return finder;
}
// Specialized query. Every next query is looked in previous result.
public static Finder specializedFinder(String... queries) {
var finder = identMult();
for (String query : queries) {
finder = finder.and(Finder.contains(query));
}
return finder;
}
// Expanded query. Looking for alternatives.
public static Finder expandedFinder(String... queries) {
var finder = identSum();
for (String query : queries) {
finder = finder.or(Finder.contains(query));
}
return finder;
}
...
}
```
अब हमने कॉम्बिनेटरों के लिए इंटरफ़ेस और विधियाँ बना ली हैं। अब हमारे पास इन कॉम्बिनेटरों पर काम करने वाला एक एप्लिकेशन है।
```java
var queriesOr = new String[]{"many", "Annabel"};
var finder = Finders.expandedFinder(queriesOr);
var res = finder.find(text());
LOGGER.info("the result of expanded(or) query[{}] is {}", queriesOr, res);
var queriesAnd = new String[]{"Annabel", "my"};
finder = Finders.specializedFinder(queriesAnd);
res = finder.find(text());
LOGGER.info("the result of specialized(and) query[{}] is {}", queriesAnd, res);
finder = Finders.advancedFinder("it was", "kingdom", "sea");
res = finder.find(text());
LOGGER.info("the result of advanced query is {}", res);
res = Finders.filteredFinder(" was ", "many", "child").find(text());
LOGGER.info("the result of filtered query is {}", res);
private static String text() {
return
"It was many and many a year ago,\n"
+ "In a kingdom by the sea,\n"
+ "That a maiden there lived whom you may know\n"
+ "By the name of ANNABEL LEE;\n"
+ "And this maiden she lived with no other thought\n"
+ "Than to love and be loved by me.\n"
+ "I was a child and she was a child,\n"
+ "In this kingdom by the sea;\n"
+ "But we loved with a love that was more than love-\n"
+ "I and my Annabel Lee;\n"
+ "With a love that the winged seraphs of heaven\n"
+ "Coveted her and me.";
}
```
**प्रोग्राम आउटपुट:**
```java
the result of expanded(or) query[[many, Annabel]] is [It was many and many a year ago,, By the name of ANNABEL LEE;, I and my Annabel Lee;]
the result of specialized(and) query[[Annabel, my]] is [I and my Annabel Lee;]
the result of advanced query is [It was many and many a year ago,]
the result of filtered query is [But we loved with a love that was more than love-]
```
अब हम अपने ऐप को `expandedFinder`, `specializedFinder`, `advancedFinder`, `filteredFinder` खोजने वाले प्रश्नों के साथ डिज़ाइन कर सकते हैं, जो सभी `contains`, `or`, `not`, `and` से प्राप्त होते हैं।
## क्लास डायग्राम
![alt text](../../../combinator/etc/combinator.urm.png "Combinator class diagram")
## प्रयोज्यता
कॉम्बिनेटर पैटर्न का उपयोग तब करें जब:
- आप अधिक सादे मानों से अधिक जटिल मान बनाने में सक्षम हैं, लेकिन उनका प्रकार समान है (उनका संयोजन)
## फ़ायदे
- डेवलपर्स के नजरिए से एपीआई डोमेन के शब्दों से बना है।
- संयोजन और अनुप्रयोग चरण के बीच स्पष्ट अंतर है।
- पहले एक इंस्टेंस बनाता है और फिर उसे निष्पादित करता है।
- यह पैटर्न को समानांतर वातावरण में लागू बनाता है।
## वास्तविक दुनिया के उदाहरण
- java.util.function.Function#compose
- java.util.function.Function#andThen
## श्रेय
- [Example for java](https://gtrefs.github.io/code/combinator-pattern/)
- [Combinator pattern](https://wiki.haskell.org/Combinator_pattern)
- [Combinatory logic](https://wiki.haskell.org/Combinatory_logic)
+236
View File
@@ -0,0 +1,236 @@
---
title: Command
category: Behavioral
language: hi
tag:
- Gang of Four
---
## दूसरा नाम
क्रिया, लेन-देन
## हेतु
एक अनुरोध को एक ऑब्जेक्ट के रूप में इनकैप्सुलेट करें, जिससे आपको क्लाइंट को अलग-अलग पैरामीटराइज़ करने की सुविधा मिलती है
अनुरोध, कतार या लॉग अनुरोध, और पूर्ववत संचालन का समर्थन करते हैं।
## व्याख्या
वास्तविक दुनिया का उदाहरण
> एक जादूगर भूत पर जादू कर रहा है। मंत्रों को एक-एक करके भूत पर क्रियान्वित किया जाता है।
> पहला जादू भूत को सिकोड़ देता है और दूसरा उसे अदृश्य बना देता है। फिर विज़ार्ड उलट जाता है
> मंत्र एक-एक करके। यहां प्रत्येक मंत्र एक कमांड ऑब्जेक्ट है जिसे पूर्ववत किया जा सकता है।
साफ़ शब्दों में
> अनुरोधों को कमांड ऑब्जेक्ट के रूप में संग्रहीत करने से कोई कार्रवाई करने या बाद में उसे पूर्ववत करने की अनुमति मिलती है।
विकिपीडिया कहता है
> ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग में, कमांड पैटर्न एक व्यवहारिक डिज़ाइन पैटर्न है जिसमें a
> ऑब्जेक्ट का उपयोग किसी क्रिया को करने या किसी ईवेंट को ट्रिगर करने के लिए आवश्यक सभी जानकारी को समाहित करने के लिए किया जाता है
> बाद में।
**प्रोग्रामेटिक उदाहरण**
यहां विज़ार्ड और गॉब्लिन के साथ नमूना कोड दिया गया है। आइए `Wizard` वर्ग से शुरू करें।
```java
@Slf4j
public class Wizard {
private final Deque<Runnable> undoStack = new LinkedList<>();
private final Deque<Runnable> redoStack = new LinkedList<>();
public Wizard() {}
public void castSpell(Runnable runnable) {
runnable.run();
undoStack.offerLast(runnable);
}
public void undoLastSpell() {
if (!undoStack.isEmpty()) {
var previousSpell = undoStack.pollLast();
redoStack.offerLast(previousSpell);
previousSpell.run();
}
}
public void redoLastSpell() {
if (!redoStack.isEmpty()) {
var previousSpell = redoStack.pollLast();
undoStack.offerLast(previousSpell);
previousSpell.run();
}
}
@Override
public String toString() {
return "Wizard";
}
}
```
इसके बाद, हमारे पास वह भूत है जो मंत्रों का लक्ष्य है।
```java
@Slf4j
public abstract class Target {
private Size size;
private Visibility visibility;
public Size getSize() {
return size;
}
public void setSize(Size size) {
this.size = size;
}
public Visibility getVisibility() {
return visibility;
}
public void setVisibility(Visibility visibility) {
this.visibility = visibility;
}
@Override
public abstract String toString();
public void printStatus() {
LOGGER.info("{}, [size={}] [visibility={}]", this, getSize(), getVisibility());
}
}
public class Goblin extends Target {
public Goblin() {
setSize(Size.NORMAL);
setVisibility(Visibility.VISIBLE);
}
@Override
public String toString() {
return "Goblin";
}
public void changeSize() {
var oldSize = getSize() == Size.NORMAL ? Size.SMALL : Size.NORMAL;
setSize(oldSize);
}
public void changeVisibility() {
var visible = getVisibility() == Visibility.INVISIBLE
? Visibility.VISIBLE : Visibility.INVISIBLE;
setVisibility(visible);
}
}
```
अंत में, हमारे पास मुख्य फ़ंक्शन कास्टिंग मंत्र में विज़ार्ड है।
```java
public static void main(String[] args) {
var wizard = new Wizard();
var goblin = new Goblin();
// casts shrink/unshrink spell
wizard.castSpell(goblin::changeSize);
// casts visible/invisible spell
wizard.castSpell(goblin::changeVisibility);
// undo and redo casts
wizard.undoLastSpell();
wizard.redoLastSpell();
```
यहाँ कार्रवाई में पूरा उदाहरण है।
```java
var wizard = new Wizard();
var goblin = new Goblin();
goblin.printStatus();
wizard.castSpell(goblin::changeSize);
goblin.printStatus();
wizard.castSpell(goblin::changeVisibility);
goblin.printStatus();
wizard.undoLastSpell();
goblin.printStatus();
wizard.undoLastSpell();
goblin.printStatus();
wizard.redoLastSpell();
goblin.printStatus();
wizard.redoLastSpell();
goblin.printStatus();
```
यहाँ प्रोग्राम आउटपुट है:
```java
Goblin, [size=normal] [visibility=visible]
Goblin, [size=small] [visibility=visible]
Goblin, [size=small] [visibility=invisible]
Goblin, [size=small] [visibility=visible]
Goblin, [size=normal] [visibility=visible]
Goblin, [size=small] [visibility=visible]
Goblin, [size=small] [visibility=invisible]
```
## क्लास डायग्राम
![alt text](../../../command/etc/command.png "Command")
## प्रयोज्यता
जब आप चाहें तो कमांड पैटर्न का उपयोग करें:
* निष्पादित की जाने वाली क्रिया द्वारा वस्तुओं का मापन करें। आप ऐसे मानकीकरण को a में व्यक्त कर सकते हैं
कॉलबैक फ़ंक्शन के साथ प्रक्रियात्मक भाषा, यानी, एक फ़ंक्शन जो कहीं पंजीकृत है
बाद में बुलाया गया। कमांड कॉलबैक के लिए ऑब्जेक्ट-ओरिएंटेड प्रतिस्थापन हैं।
* अलग-अलग समय पर अनुरोध निर्दिष्ट करें, कतारबद्ध करें और निष्पादित करें। एक कमांड ऑब्जेक्ट में जीवन हो सकता है
मूल अनुरोध से स्वतंत्र. यदि किसी अनुरोध के प्राप्तकर्ता को किसी पते में दर्शाया जा सकता है
अंतरिक्ष-स्वतंत्र तरीके से, फिर आप अनुरोध के लिए एक कमांड ऑब्जेक्ट को एक अलग प्रक्रिया में स्थानांतरित कर सकते हैं
और वहां अनुरोध पूरा करें।
* समर्थन पूर्ववत करें। कमांड का निष्पादन ऑपरेशन इसके प्रभावों को उलटने के लिए स्थिति को संग्रहीत कर सकता है
स्वयं आदेश दें. कमांड इंटरफ़ेस में एक अतिरिक्त अन-एक्ज़िक्यूट ऑपरेशन होना चाहिए जो उलट देता है
निष्पादित करने के लिए पिछली कॉल के प्रभाव। निष्पादित आदेश इतिहास सूची में संग्रहीत होते हैं।
इस सूची को आगे और पीछे घुमाकर असीमित स्तर की पूर्ववत और पुनः करें कार्यक्षमता प्राप्त की जाती है
क्रमशः अन-एक्ज़ीक्यूट और एक्ज़िक्यूट को कॉल करना।
* लॉगिंग परिवर्तनों का समर्थन करें ताकि सिस्टम क्रैश होने की स्थिति में उन्हें फिर से लागू किया जा सके। को बढ़ाकर
लोड और स्टोर संचालन के साथ कमांड इंटरफ़ेस, आप परिवर्तनों का लगातार लॉग रख सकते हैं।
क्रैश से उबरने में डिस्क से लॉग किए गए कमांड को फिर से लोड करना और उन्हें फिर से निष्पादित करना शामिल है
निष्पादित ऑपरेशन.
* उच्च-स्तरीय संचालन के आसपास एक प्रणाली की संरचना करें जो आदिम संचालन पर आधारित हो। ऐसी संरचना है
लेनदेन का समर्थन करने वाली सूचना प्रणालियों में आम है। एक लेन-देन डेटा के एक सेट को समाहित करता है
परिवर्तन। कमांड पैटर्न लेनदेन को मॉडल करने का एक तरीका प्रदान करता है। कमांड का एक सामान्य इंटरफ़ेस होता है,
आपको सभी लेन-देन एक ही तरीके से शुरू करने देता है। यह पैटर्न विस्तार करना भी आसान बनाता है
नए लेनदेन के साथ प्रणाली.
* अनुरोधों का इतिहास रखें।
* कॉलबैक कार्यक्षमता लागू करें।
* पूर्ववत कार्यक्षमता लागू करें।
## ज्ञात उपयोग
* [java.lang.Runnable](http://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html)
* [org.junit.runners.model.Statement](https://github.com/junit-team/junit4/blob/master/src/main/java/org/junit/runners/model/Statement.java)
* [Netflix Hystrix](https://github.com/Netflix/Hystrix/wiki)
* [javax.swing.Action](http://docs.oracle.com/javase/8/docs/api/javax/swing/Action.html)
## श्रेय
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
* [Refactoring to Patterns](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7)
* [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=f27d2644fbe5026ea448791a8ad09c94)