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

Java का Repository Pattern

Repository pattern domain model और किसी data storage mechanism के बीच mediate करता है, domain objects को access करने के लिए एक collection-जैसा interface प्रस्तुत करते हुए जबकि यह छुपाते हुए कि वे असल में कैसे persist किए जाते हैं।
Syntax
java
interface Repository<T, ID> {
    T findById(ID id);
    List<T> findAll();
    void save(T entity);
    void delete(ID id);
}

What is the Repository Pattern?

Repository pattern domain model और किसी data storage mechanism के बीच mediate करता है, domain objects जोड़ने, पाने, और हटाने के लिए एक collection-जैसा interface प्रस्तुत करते हुए जबकि यह छुपाते हुए कि वे असल में कैसे या कहां persist किए जाते हैं।

उदाहरण: What is the Repository Pattern?

java
// Import java.util.* so it can be used by its short name
import java.util.*;
// Define the class Main
public class Main {
	// Define the interface Repository
	interface Repository<T> {
		void save(T item);
		List<T> findAll();
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println("Repository presents a collection-like interface over storage");
	}
}

Repository Interface

एक repository interface आमतौर पर save, findById, findAll, और delete जैसे standard operations define करता है, अक्सर generically express किए गए ताकि एक ही interface shape अलग-अलग entity types में reuse हो सके।

उदाहरण: Repository Interface

java
// Import java.util.* so it can be used by its short name
import java.util.*;
// Define the class Main
public class Main {
	// Define the interface Repository
	interface Repository<T> {
		void save(T item);
		// Optional wraps a value that may be absent
		Optional<T> findById(int id);
		List<T> findAll();
		void delete(int id);
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println("Same generic shape reused across entity types");
	}
}

In-Memory Repository Implementation

एक साधारण Map या List से backed एक in-memory repository implementation, development या testing के दौरान repository interface को satisfy करने का एक lightweight तरीका है, बिना किसी असली database connection की ज़रूरत के।

उदाहरण: In-Memory Repository Implementation

java
import java.util.*;
public class Main {
	interface Repository<T> { void save(T item); List<T> findAll(); }
	static class InMemoryUserRepository implements Repository<String> {
		List<String> users = new ArrayList<>();
		public void save(String item) { users.add(item); } // backed by a simple List
		public List<String> findAll() { return users; }
	}
	public static void main(String[] args) {
		Repository<String> repo = new InMemoryUserRepository();
		repo.save("Riya");
		System.out.println(repo.findAll());
	}
}

Database-Backed Repository

एक database-backed repository उसी interface को JDBC या किसी दूसरी persistence technology का उपयोग करके implement करता है ताकि relational database से असल में data store और retrieve किया जा सके, जबकि calling code इस अंतर से अनजान रहती है।

उदाहरण: Database-Backed Repository

java
import java.util.*;
public class Main {
	interface Repository<T> { void save(T item); List<T> findAll(); }
	static class JdbcUserRepository implements Repository<String> {
		List<String> simulatedTable = new ArrayList<>(); // stand-in for a real JDBC-backed store
		public void save(String item) { simulatedTable.add(item); } // real version would run an INSERT
		public List<String> findAll() { return simulatedTable; } // real version would run a SELECT
	}
	public static void main(String[] args) {
		Repository<String> repo = new JdbcUserRepository();
		repo.save("Aman");
		System.out.println(repo.findAll());
	}
}

Repository with Spring Data JPA

Spring Data JPA Repository pattern को आगे ले जाता है सिर्फ एक interface definition से अपने आप एक काम करता implementation generate करके, यहां तक कि findByName जैसे specially named methods से सीधे database queries भी निकालता है।

उदाहरण: Repository with Spring Data JPA

java
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public class Main {
	static class User { String name; }
	interface UserRepository extends JpaRepository<User, Long> {
		List<User> findByName(String name); // implementation generated automatically
	}
	public static void main(String[] args) {
		System.out.println("Spring Data JPA generates the implementation from the interface alone");
	}
}
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. कुछ न मिलने पर findById से null return करना, बजाय एक Optional के।
  2. repository के अंदर business rules डालना, जबकि इसे सिर्फ data store और load करना चाहिए।
  3. कई threads द्वारा share की गई किसी in-memory repository में एक साधारण HashMap का उपयोग करना, जो thread-safe नहीं है।
चैप्टर सारांश
  • Design patterns आम design समस्याओं के verified solutions हैं।
  • Singleton, Factory, और Builder जैसे Creational patterns object creation को control करते हैं, जबकि Adapter, Decorator, Facade, और Proxy जैसे structural patterns objects को organize करते हैं।
  • Observer, Strategy, Command, और Template Method जैसे Behavioral patterns, साथ ही MVC और Repository, यह organize करते हैं कि objects कैसे interact करें।

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.