Java Design Patterns परिचय
In this page:
Creational Patterns Concept
Creational patterns object creation की mechanics पर खुद ध्यान देते हैं, objects को जो भी तरीका स्थिति के लिए सबसे उपयुक्त हो उस तरीके से instantiate करने का लक्ष्य रखते हुए, हमेशा एक साधारण constructor उपयोग करने के बजाय। एक classic उदाहरण Prototype pattern है, जो scratch से एक बनाने के बजाय किसी मौजूदा, pre-configured instance को clone करके नए objects बनाता है।
उदाहरण: Creational Patterns Concept
public class Main {
static class Config implements Cloneable {
String name;
Config(String name) { this.name = name; }
public Config clone() {
try { return (Config) super.clone(); } catch (CloneNotSupportedException e) { return null; }
}
}
public static void main(String[] args) {
Config original = new Config("base");
Config copy = original.clone(); // creates a new object by cloning, not a fresh constructor call
System.out.println(copy.name);
}
}
Login to try C/C++/Java/PHP code in the editor
Structural Patterns Concept
Structural patterns describe करते हैं कि objects और classes को बड़ी structures में कैसे assemble किया जाए जबकि उन structures को flexible और loosely coupled रखा जाए। Adapter pattern इसका एक आम उदाहरण है, एक class के interface को किसी दूसरे interface में translate करते हुए जिसकी client code पहले से उम्मीद करती है, किसी भी पक्ष को modify किए बिना।
उदाहरण: Structural Patterns Concept
public class Main {
interface UsSocket { void plugUs(); }
static class EuPlug {
void plugEu() { System.out.println("EU plug connected"); }
}
static class Adapter implements UsSocket {
EuPlug euPlug = new EuPlug();
public void plugUs() { euPlug.plugEu(); } // translates one interface to another
}
public static void main(String[] args) {
UsSocket socket = new Adapter();
socket.plugUs();
}
}
Login to try C/C++/Java/PHP code in the editor
Behavioral Patterns Concept
Behavioral patterns algorithms और objects के बीच responsibilities कैसे distribute और communicate होती हैं इससे संबंधित हैं। Strategy pattern, उदाहरण के लिए, interchangeable algorithms का एक family define करता है और client code को उसका उपयोग करने वाले code को बदले बिना runtime पर उनके बीच switch करने देता है।
उदाहरण: Behavioral Patterns Concept
public class Main {
interface PaymentStrategy { void pay(int amount); }
static class CardPayment implements PaymentStrategy {
public void pay(int amount) { System.out.println("Paid " + amount + " by card"); }
}
static class CashPayment implements PaymentStrategy {
public void pay(int amount) { System.out.println("Paid " + amount + " in cash"); }
}
public static void main(String[] args) {
PaymentStrategy strategy = new CardPayment();
strategy.pay(100); // swappable at runtime
}
}
Login to try C/C++/Java/PHP code in the editor
Why Use Design Patterns?
Design patterns इसलिए मौजूद हैं क्योंकि वे software design में बार-बार आने वाली समस्याओं के well-tested, verified solutions हैं। वे developers को एक shared vocabulary देते हैं — 'यह एक Factory है' कहना तुरंत एक पूरी design संप्रेषित कर देता है — और वे आपको किसी ऐसे solution को फिर से invent (और शायद गलत) करने से बचाते हैं जिसे किसी ने पहले ही refine कर लिया हो।
उदाहरण: Why Use Design Patterns?
public class Main {
interface Factory { Object create(); } // naming this "Factory" instantly communicates the design
public static void main(String[] args) {
Factory factory = () -> "Shared vocabulary saves re-inventing solutions";
System.out.println(factory.create());
}
}
Login to try C/C++/Java/PHP code in the editor
Categorizing Patterns
तीन व्यापक categories — Creational, Structural, और Behavioral — किसी design समस्या का सामना करते समय एक उपयोगी पहला filter हैं: यह पहचानना कि आपकी समस्या असल में किस category में आती है, इससे पहले कि आप individual options को देखें, यह तय करने में मदद करता है कि कौन सा specific pattern fit होने की संभावना है।
उदाहरण: Categorizing Patterns
public class Main {
enum PatternCategory { CREATIONAL, STRUCTURAL, BEHAVIORAL }
public static void main(String[] args) {
PatternCategory category = PatternCategory.CREATIONAL; // identify the category first
System.out.println("Problem falls into: " + category);
}
}
Login to try C/C++/Java/PHP code in the editor
- ऐसे code में एक design pattern को ज़बरदस्ती डालना जिसे एक साधारण class या method हल कर देती, जो सिर्फ अतिरिक्त classes जोड़ता है।
- वह समस्या समझे बिना एक pattern को copy करना जिसे यह हल करता है, जिससे यह गलत जगह चला जाता है।
- Prototype idea के लिए
clone()का उपयोग करना और यह भूल जाना कि यह एक shallow copy बनाता है, इसलिए mutable fields अब भी copies के बीच share होते हैं।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Java Design Patterns Introduction
- Java Singleton Pattern
- Java Factory Pattern
- Java Observer Pattern
- Java Builder Pattern
- Java MVC Architecture
- Java Adapter Pattern
- Java Decorator Pattern
- Java Strategy Pattern
- Java Command Pattern
- Java Facade Pattern
- Java Proxy Pattern
- Java Template Method Pattern
- Java Repository Pattern