Repository with CRUD Example
This program demonstrates Repository with CRUD operations.
Problem Statement
Write a Java program where Repository provides CRUD for Product.
Source Code
| 1 | class Product { |
| 2 | String name; |
| 3 | Product(String n){ name=n; } |
| 4 | } |
| 5 | class ProductRepository { |
| 6 | void save(Product p){ System.out.println("Saved product: "+p.name); } |
| 7 | Product find(){ return new Product("Laptop"); } |
| 8 | } |
| 9 | public class RepositoryCRUD { |
| 10 | public static void main(String[] args) { |
| 11 | System.out.println("Eduinq Repository CRUD"); |
| 12 | ProductRepository pr=new ProductRepository(); |
| 13 | Product p=new Product("Mobile"); |
| 14 | pr.save(p); |
| 15 | Product f=pr.find(); |
| 16 | System.out.println("Found product: "+f.name); |
| 17 | } |
| 18 | } |
Program Output
Eduinq Repository CRUD Saved product: Mobile Found product: Laptop
Explanation
Repository provides CRUD operations. - Abstracts data access logic. - This models repository pattern with CRUD. Conclusion: Shows repository with CRUD example.