← Back to Advanced Java Course | Chapter 6: Design Patterns | Lesson 1 of 14

Java Design Patterns परिचय

Design patterns आम coding समस्याओं के verified solutions हैं, जैसे आज़माई-परखी recipes। वे programmers को अच्छे designs के बारे में बात करने का एक common तरीका देते हैं।

Creational Patterns Concept

Creational patterns object creation की mechanics पर खुद ध्यान देते हैं, objects को जो भी तरीका स्थिति के लिए सबसे उपयुक्त हो उस तरीके से instantiate करने का लक्ष्य रखते हुए, हमेशा एक साधारण constructor उपयोग करने के बजाय। एक classic उदाहरण Prototype pattern है, जो scratch से एक बनाने के बजाय किसी मौजूदा, pre-configured instance को clone करके नए objects बनाता है।

उदाहरण: Creational Patterns Concept

java
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);
	}
}

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

java
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();
	}
}

Behavioral Patterns Concept

Behavioral patterns algorithms और objects के बीच responsibilities कैसे distribute और communicate होती हैं इससे संबंधित हैं। Strategy pattern, उदाहरण के लिए, interchangeable algorithms का एक family define करता है और client code को उसका उपयोग करने वाले code को बदले बिना runtime पर उनके बीच switch करने देता है।

उदाहरण: Behavioral Patterns Concept

java
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
	}
}

Why Use Design Patterns?

Design patterns इसलिए मौजूद हैं क्योंकि वे software design में बार-बार आने वाली समस्याओं के well-tested, verified solutions हैं। वे developers को एक shared vocabulary देते हैं — 'यह एक Factory है' कहना तुरंत एक पूरी design संप्रेषित कर देता है — और वे आपको किसी ऐसे solution को फिर से invent (और शायद गलत) करने से बचाते हैं जिसे किसी ने पहले ही refine कर लिया हो।

उदाहरण: Why Use Design Patterns?

java
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());
	}
}

Categorizing Patterns

तीन व्यापक categories — Creational, Structural, और Behavioral — किसी design समस्या का सामना करते समय एक उपयोगी पहला filter हैं: यह पहचानना कि आपकी समस्या असल में किस category में आती है, इससे पहले कि आप individual options को देखें, यह तय करने में मदद करता है कि कौन सा specific pattern fit होने की संभावना है।

उदाहरण: Categorizing Patterns

java
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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. ऐसे code में एक design pattern को ज़बरदस्ती डालना जिसे एक साधारण class या method हल कर देती, जो सिर्फ अतिरिक्त classes जोड़ता है।
  2. वह समस्या समझे बिना एक pattern को copy करना जिसे यह हल करता है, जिससे यह गलत जगह चला जाता है।
  3. Prototype idea के लिए clone() का उपयोग करना और यह भूल जाना कि यह एक shallow copy बनाता है, इसलिए mutable fields अब भी copies के बीच share होते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.