Factory Pattern

Example
Optimized code

This code has some sysout to understand the code better .