Java Design Patterns — Interview Guide
ttern Type Pattern Name Description Java Example Interview Tip
Creatio Singleton Ensures a class has public class Singleton { private Mention
nal only one instance and static Singleton instance; lazy vs
provides a global private Singleton() {} public eager ins
access point. static Singleton getInstance() { ntiation
if(instance == null) instance = and threa
new Singleton(); return safety.
instance; } }
Creatio Factory Defines an interface interface ShapeFactory { Shape Decouples
nal Method for creating objects, create(); } class CircleFactory object
letting subclasses implements ShapeFactory { public creation
decide the concrete Shape create() { return new from usag
class. Circle(); } } useful in
framework
Creatio Builder Separates construction Car car = new [Link]() Great for
nal of a complex object .setColor("Red") .setSeats(4) immutable
from its .build(); objects
representation. with
optional
parameter
Behavio Observer Defines a one-to-many [Link](new Use in
ral dependency; observers Observer() { public void event
are notified on state update() { handling,
changes. [Link]("State MVC
changed"); } }); patterns.
Behavio Strategy Encapsulates [Link](new Replace
ral interchangeable ConcreteStrategy()); condition
algorithms and makes [Link](); logic wit
them interchangeable. polymorph
m.
Behavio Command Encapsulates a request Command cmd = new Mention
ral as an object for LightOnCommand(light); undo/redo
parameterization and [Link](cmd); mplementa
queuing. ons.
Structu Adapter Converts one interface class Adapter implements Target Integrate
ral into another expected { private Adaptee adaptee; legacy co
by clients. public void request() { with new
[Link](); } } systems.
Structu Decorator Adds behavior to Component decorated = new Common in
ral objects dynamically BorderDecorator(new TextBox()); Java IO
without modifying the streams (
class. fferedRea
r, etc).
Structu Proxy Provides a placeholder interface Image { void Use for
ral to control access to display(); } class ProxyImage lazy
another object. implements Image { private loading,
RealImage real; public void access
display() { if(real==null) control,
real=new RealImage(); logging.
[Link](); } }
Behavio Template Defines the skeleton abstract class Game { final void Control
ral Method of an algorithm, play() { start(); playTurn(); invariant
deferring steps to end(); } abstract void start(); steps whi
subclasses. abstract void playTurn(); allowing
abstract void end(); } stomizati
.