Core Java Design Patterns

In core java, there are mainly three types of design patterns, which are further divided into their sub-parts:

  • Creational Design Pattern
Factory Pattern
Abstract Factory Pattern
Singleton Pattern
Prototype Pattern
Builder Pattern.
  • Structural Design Pattern
Adapter Pattern
Bridge Pattern
Composite Pattern
Decorator Pattern
Facade Pattern
Flyweight Pattern
Proxy Pattern
  • Behavioral Design Pattern
Chain Of Responsibility Pattern
Command Pattern
Interpreter Pattern
Iterator Pattern
Mediator Pattern
Memento Pattern
Observer Pattern
State Pattern
Strategy Pattern
Template Pattern
Visitor Pattern
  • Difference between Strategy and State design Pattern in Java?
    Answer.
    Both Strategy and State pattern has the same structure. If you look at UML class diagram for both patterns they look exactly same, but their intent is totally different.
    The State design pattern is used to define and manage the state of an object, while Strategy pattern is used to define a set of an interchangeable algorithm and let’s client choose one of them. So Strategy pattern is a client driven pattern while Object can manage their state itself.
  • How we can write thread-safe Singleton in Java?
    Answer.
    There are multiple ways to write thread-safe singleton in Java e.g. By writing singleton uses double checked locking, by using static Singleton instance initialized during class loading. By the way using Java enum to create thread-safe singleton is the most simple way.
  • What is the difference between Factory and Abstract Factory in Java?
    Answer.
    The main difference is that Abstract Factory creates a factory while Factory pattern creates objects. So both abstract the creation logic, but one abstract is for factory and other items.