Commit 4ff824d
committed
feat(generic-interface): add generic Container interface with concrete and generic implementations
WHAT:
- Introduced a generic interface `Container<T>` with two core operations:
- `add(T item)` → store an item.
- `T get()` → retrieve the stored item.
- Added two implementations:
1. `StringContainer` → fixed to work only with `String` type.
2. `GenericContainer<T>` → fully generic, supports any type at instantiation.
- Added `Main` class to demonstrate usage with `String` and `Integer`.
WHY:
- To illustrate how generic interfaces enable type-safe contracts that can be implemented differently:
- Specialized implementation (e.g., `StringContainer`).
- Reusable generic implementation (e.g., `GenericContainer<T>`).
- Avoids boilerplate and unsafe casting while maintaining flexibility.
HOW (Logic Flow):
1. Define `Container<T>` interface → abstraction with generic type parameter `T`.
2. Implement with `StringContainer` (type fixed) and `GenericContainer<T>` (fully reusable).
3. Demonstrate instantiation:
- `StringContainer` stores and retrieves `String`.
- `GenericContainer<Integer>` stores and retrieves an `Integer`.
BENEFITS:
- Provides compile-time type safety.
- Eliminates the need for explicit casting when retrieving values.
- Increases reusability by supporting both specific and generic use cases.
REAL-WORLD APPLICATIONS:
- Mirrors patterns in Java Collections API (`List<T>`, `Set<T>`, `Map<K,V>`).
- Useful in dependency injection, caching, and data processing frameworks.
- Can serve as the foundation for building strongly typed containers for domain-specific logic.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 57f610b commit 4ff824d
1 file changed
+6
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
| 44 | + | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
51 | | - | |
52 | 50 | | |
| 51 | + | |
53 | 52 | | |
54 | 53 | | |
55 | 54 | | |
| |||
0 commit comments