Evaluate Functional Programming using Kotlin vs Java Oriented Object Programming
Create a Shopping Cart with following flow
- A Service without UI means no main method, no need for Integration testing
- Implementation based on State Machine approach
- Logging used as simplest implementation of Observability pattern
- Enforced Immutability
- User details, Articles and Cart (Basket) can not be modified
- No user historical data is required
- No progress indicator is required
- No voucher/redemption code is required
- Same user (if name is unique) can be in several states at a given time
- One user only can have one cart associated
- No further requirements are required
- External system are injected as function parameters
-
SOLID principles
-
Criteria for using Abstract class vs Interface
-
Not as concise as Kotlin implementation
-
State machine is divided in
StatesandActionsapplied toStates -
To check error type from one
Stateto another,StatesandActionshierarchy has been used
- Pros:
- Faster coding feedback, since checking error type is done in IDE
- Easier to scale the creation new
StatesandActionsthan if/else statements
- Cons:
- Increase number of Classes
- Mental state model can be overwhelming, recommendation is to create a visual Flowchart as mentioned
- Pros:
-
All objects are immutable
-
Immutable objects solves aliasing problem and thread-safe
- If a external system could trigger an
Action, an API can be implemented with State and Action as parameters - If Logging (Observability) is required in each step it can be done using annotations
- If User or Articles attributes can be modified
Cart Readystate could have another branch that returns to it - If articles added in Cart (Basket) added of subtracted
Cart Readystate could have another branch that returns to it, except if Basket is empty - If recommendations is required, AB Testing can be done to validate assumptions of articles shown
- If voucher or redeem code is required, total price could be calculated in
Stock Checkedstate, thereforeStock Checkedcould have another branch from that returns to it - If progress indicator is required, similar approach as logging can be used
- If historical data is required to be stored, add integration with a data base in the same as logging
- If a timer is required, using Observability to log time for each State change and implement an API to interact
- If restrictions:
- Only one user can be in the same State at the same time.
- Using same approach as logging, save a snapshot of the system relating User to State
- Validation of the snapshot can be done before or after any
Actionwith Annotation or AOP
- The item added in Cart needs to be unique
- Use unique identifiers for Articles in Basket
- Only one user can be in the same State at the same time.
- If external system take longer to give a response than considered acceptable, approaches:
- Inform the user a problem has occurred and user will try again
- Implement an asynchronous system based on:
- Coroutines
- Reactive streams Coroutines can work with reactive Streams
- Events