Blog Archives
Page 1 of 2
1 2

01: OOP vs FP with Java example – placing a trade

A Trade goes through 3 state changes – placed, filled, and settled. Let’s solve this simple problem using both OOP & FP approaches.

Place –> Fill –> Settle

OOP approach by mutating the state

State changes are maintained via a variable named “

Read more ›



02: OOP vs FP with Java example – Conversion strategy pattern

Java code to convert given input values from KM to Meter, Meter to Centimetres, etc. OOP approach using the strategy design pattern Step 1: Define an interface. Step 2: Define a generic “MyMetricConverter” that implements the “Converter”. Step 3: Define different strategy classes that return different conversion rates. … Read...

Members Only Content
Log In Register Home


03: Java FP composing functions example – calculate discount

FP – calculate the invoice price Invoice Price = (markedPrice * (100 – discountRate)/100) + deliveryCharge Example “f.andThen(g)” is the composition of functions. This can be described as g(f()) where f() is 270.0 (i.e. 300.00 * (100 – 10)/100), and g(f()) is 270.00 + 50.00 = 320.00; … Read more...

Members Only Content
Log In Register Home


04: Java OOP vs FP – Builder Pattern

Current Balance = Initial Amounts + Credits – Debits OOP approach using the builder design pattern Creating a constructor to take three BigDecimal values is NOT only readable, but also prone to errors as you can inadvertently supply the debit value instead of the credit value, … Read more ›...

Members Only Content
Log In Register Home


05: Java OOP vs FP – Handling unchecked exceptions

int result = input / divideBy will throw an unchecked exception if divideBy = 0. OOP error handling by throwing exceptions In OOP errors are handled by throwing & catching exceptions. Here is a very simple OOP example. Step 1: Define an interface. … Read more ›...

Members Only Content
Log In Register Home


Page 1 of 2
1 2

800+ Java Interview Q&As Menu

Top