Commit 51210fa
committed
feat(LambdaExpressionHard): add demo combining FunctionalInterface, class, and lambdas
What
- Added MathOperation functional interface with single abstract method operate(int, int).
- Implemented SumOperation class:
- Overrides operate() by delegating to add().
- Provides additional add() and multiply() methods.
- Added LambdaExpressionHard class demonstrating:
- Lambda for addition: (a, b) -> a + b.
- Lambda for subtraction: (a, b) -> a - b.
- Usage of helper method processOperation() to apply operations.
- Example with SumOperation instance performing add() and multiply().
Why
- Illustrates difference between traditional class-based implementation and modern lambda-based functional programming.
- Demonstrates how @FunctionalInterface enforces Single Abstract Method (SAM) contract.
- Provides a practical example of reusable operations (mini calculator) implemented with both OOP and FP approaches.
- Shows how lambdas improve conciseness compared to verbose class definitions.
How
- Defined @FunctionalInterface MathOperation with method operate().
- Created SumOperation class implementing MathOperation and extending functionality with add() and multiply().
- In main():
- Created lambdas for addition and subtraction.
- Executed them with test inputs a = 10, b = 5.
- Passed lambdas into private helper processOperation() for internal use.
- Instantiated SumOperation and demonstrated add() and multiply().
- Kept processOperation private to restrict usage within class scope.
Logic
- Inputs: integers a = 10, b = 5.
- Outputs (available internally for further processing):
- Lambda addition → 15.
- Lambda subtraction → 5.
- SumOperation.add(10, 5) → 15.
- SumOperation.multiply(10, 5) → 50.
- Flow:
1. Create lambdas for addition/subtraction.
2. Execute lambdas directly and via helper method.
3. Instantiate SumOperation class.
4. Call add() and multiply() for results.
- Edge cases:
- Lambdas work only because MathOperation has single abstract method.
- multiply() is an extension beyond interface contract, showing class flexibility.
- Complexity / performance: O(1) per operation, negligible.
- Concurrency / thread-safety:
- Lambdas are stateless and thread-safe.
- SumOperation methods are stateless, safe across threads.
- Error handling:
- No exceptions expected for basic arithmetic.
- Overflow possible with large integers, inherent to int.
Real-life applications
- Calculator-style applications mixing OOP and functional programming.
- Replacing strategy pattern classes with lambdas for concise logic.
- Passing operations as parameters to methods for higher-order function usage.
- Educational example contrasting pre-Java-8 and Java-8+ approaches.
Notes
- processOperation demonstrates reusability by accepting any MathOperation implementation (class or lambda).
- @FunctionalInterface annotation ensures compiler enforces single abstract method constraint.
- For larger systems, prefer lambdas for simple inline logic and classes for complex, reusable implementations.
- Example illustrates dual paradigms: OOP extensibility (SumOperation) and FP conciseness (lambdas).
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 9e09c71 commit 51210fa
File tree
1 file changed
+40
-2
lines changed- Java 8 Crash Course/Lambda Expression/src
1 file changed
+40
-2
lines changedLines changed: 40 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
| |||
22 | 21 | | |
23 | 22 | | |
24 | 23 | | |
25 | | - | |
26 | 24 | | |
27 | 25 | | |
28 | 26 | | |
| |||
54 | 52 | | |
55 | 53 | | |
56 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
0 commit comments