Java Design Patterns — Interview Guide
rn Type Pattern Name Description Java Example Interview Tip
onal Singleton Ensures a class has only one instance public class Singleton { private static Mention lazy vs eage
and provides a global access point. Singleton instance; private Singleton() {} instantiation; thread-s
public static Singleton getInstance() {
if(instance==null) instance=new
Singleton(); return instance; } }
onal Factory Method Defines an interface for creating an object interface ShapeFactory { Shape create(); } Useful to decouple o
but lets subclasses decide which class to creation from usage.
instantiate.
onal Builder Separates the construction of a complex Car car = new [Link]().setColor("Re Great for creating
object from its representation. d").setSeats(4).build(); immutable objects wi
optional parameters.
ioral Observer Defines a one-to-many dependency so [Link](new Observer(){ Mention use in event
that when one object changes state, all public void update(){} }); handling, MVC patter
dependents are notified.
ioral Strategy Defines a family of algorithms, [Link](new Emphasize replacing
encapsulates each one, and makes them ConcreteStrategy()); [Link](); conditional logic with
interchangeable. polymorphism.
ioral Command Encapsulates a request as an object, Command cmd = new Mention undo/redo
allowing parameterization of clients with LightOnCommand(light); implementations.
queues, requests, and operations. [Link](cmd);
ural Adapter Converts the interface of a class into class Adapter implements Target { Good to integrate leg
another interface clients expect. [Link](); } code with new system
ural Decorator Adds behavior to objects dynamically Component decorated = new Mention IO streams i
without modifying their class. BorderDecorator(new TextBox()); Java as common exa
ural Proxy Provides a surrogate or placeholder for interface Image { void display(); } class Use for lazy loading,
another object to control access. ProxyImage implements Image { ... } access control, or log
ioral Template Method Defines the skeleton of an algorithm in a abstract class Game { final void play(){ Highlight controlling
method, deferring steps to subclasses. start(); playTurn(); end(); } } invariant steps while
allowing customizatio