Design Pattern: Strategy Design Pattern in Java with Example in JDK

Strategy design pattern is one of the behavioral design pattern. Strategy pattern is used when we have multiple algorithm for a specific task and client decides the actual implementation to be used at runtime. Strategy pattern is also known as Policy Pattern. One of the best example of strategy pattern is 1Collections.sort(anyCollection, Comparator implemetation)  method that takes […]

Continue Reading

How to achieve Singleton using Enum in Java?

A singleton is a class that is supposed to have only one instance per JVM. The Same instance of the singleton class is reused by multiple threads.   12345678910public enum SingletonUsingEnum { INSTANCE; int value; public int getValue() { return value; } public void setValue(int value) { this.value = value; } } And in Your main […]

Continue Reading

Difference between Java Singleton and Spring Singleton?

There is common misunderstanding between Spring Singleton and Java Singleton classes.  If you don’t know Singleton design pattern Please read Singleton Design pattern first. Java singleton class is per Classloader and Spring’s singleton is per application context. Java considers something a singleton if it cannot create more than one instance of that class within a […]

Continue Reading

Factorial in Java and What is Tail Recursion

If You have ever written factorial program using recursion You have faced StackOverFlowError as Java does not fully support recursion for large iteration. Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem (as opposed to iteration). Recursions are really cool and […]

Continue Reading