Java का Repository Pattern
In this page:
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?
// 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");
}
}
Login to try C/C++/Java/PHP code in the editor
Repository Interface
एक repository interface आमतौर पर save, findById, findAll, और delete जैसे standard operations define करता है, अक्सर generically express किए गए ताकि एक ही interface shape अलग-अलग entity types में reuse हो सके।
उदाहरण: Repository Interface
// 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");
}
}
Login to try C/C++/Java/PHP code in the editor
In-Memory Repository Implementation
एक साधारण Map या List से backed एक in-memory repository implementation, development या testing के दौरान repository interface को satisfy करने का एक lightweight तरीका है, बिना किसी असली database connection की ज़रूरत के।
उदाहरण: In-Memory Repository Implementation
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());
}
}
Login to try C/C++/Java/PHP code in the editor
Database-Backed Repository
एक database-backed repository उसी interface को JDBC या किसी दूसरी persistence technology का उपयोग करके implement करता है ताकि relational database से असल में data store और retrieve किया जा सके, जबकि calling code इस अंतर से अनजान रहती है।
उदाहरण: Database-Backed Repository
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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");
}
}
Login to try C/C++/Java/PHP code in the editor
- कुछ न मिलने पर
findByIdसेnullreturn करना, बजाय एकOptionalके। - repository के अंदर business rules डालना, जबकि इसे सिर्फ data store और load करना चाहिए।
- कई 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 करें।
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