Java Design Patterns Full
Java Design Patterns Full
Singleton Pattern
Definition: Ensures only one instance of a class is created and provides a global point of access to it.
Analogy: Imagine a print spooler - only one print manager handles all jobs.
Code Example:
class PrinterSpooler {
private static PrinterSpooler instance = new PrinterSpooler();
private PrinterSpooler() { System.out.println("Spooler Initialized."); }
public static PrinterSpooler getInstance() { return instance; }
public void print(String doc) { System.out.println("Printing: " + doc); }
}
When to Use: Logging, Configuration Manager, Database connection pool
class PizzaFactory {
public Pizza getPizza(String type) {
if (type.equalsIgnoreCase("veg")) return new VegPizza();
else if (type.equalsIgnoreCase("chicken")) return new ChickenPizza();
return null;
}
}
When to Use: When object creation logic needs to be hidden from the client.
3. Builder Pattern
Definition: Builds complex objects step-by-step using a builder class.
Analogy: Making a burger by selecting bun, patty, sauce step-by-step.
Code Example:
class Burger {
private String bun, patty, sauce;
private Burger(BurgerBuilder b) {
this.bun = b.bun; this.patty = b.patty; this.sauce = b.sauce;
}
public static class BurgerBuilder {
String bun, patty, sauce;
public BurgerBuilder setBun(String b) { bun = b; return this; }
public BurgerBuilder setPatty(String p) { patty = p; return this; }
public BurgerBuilder setSauce(String s) { sauce = s; return this; }
public Burger build() { return new Burger(this); }
}
}
When to Use: When object has many optional fields or step-by-step construction is needed.
4. Adapter Pattern
Definition: Allows incompatible interfaces to work together.
Analogy: Using USB-C to Micro-USB adapter for charging.
Code Example:
class VLCPlayer {
void playVLC(String file) { System.out.println("Playing VLC: " + file); }
}
5. Decorator Pattern
Definition: Dynamically adds behavior to an object without altering its structure.
Analogy: Adding milk or sugar to plain coffee.
Code Example:
interface Coffee {
String getDescription(); int getCost();
}
class HomeTheaterFacade {
Lights l = new Lights(); Projector p = new Projector(); SoundSystem s = new SoundSystem();
void watchMovie() {
l.dim(); p.on(); s.play();
System.out.println("Movie started.");
}
}
When to Use: When simplifying complex APIs or libraries.
7. Observer Pattern
Definition: One-to-many dependency where one change notifies all observers.
Analogy: YouTube notifies all subscribers when a creator uploads.
Code Example:
class Channel {
List<Subscriber> subs = new ArrayList<>();
void subscribe(Subscriber s) { subs.add(s); }
void upload(String v) {
for (Subscriber s : subs) s.update(v);
}
}
When to Use: Event-driven systems (UI, real-time apps).
8. Strategy Pattern
Definition: Defines a family of algorithms, encapsulates each, and makes them interchangeable at runtime.
Analogy: Choosing payment strategy: UPI, Card, PayPal.
Code Example:
class PaymentContext {
PaymentStrategy strategy;
void setStrategy(PaymentStrategy s) { strategy = s; }
void pay(int amt) { strategy.pay(amt); }
}
When to Use: When multiple algorithms are available for a task.