Design Patterns are proven, time-tested architectural templates for solving recurring software design challenges. Originating from the famous Gang of Four (GoF) book, mastering these patterns is a mandatory benchmark for technical interviews, BCA/MCA university syllabi, and building production-grade enterprise software.
Guarantees that a class has only one single instance throughout the JVM lifecycle and provides a global access point.
public class DatabasePool {
// 1. Private constructor prevents instantiation via 'new'
private DatabasePool() {
System.out.println("Initializing Database Connection Pool...");
}
// 2. Static inner helper class (Loaded only when getInstance() is called)
private static class PoolHolder {
private static final DatabasePool INSTANCE = new DatabasePool();
}
// 3. Global access point
public static DatabasePool getInstance() {
return PoolHolder.INSTANCE;
}
}
public enum AppConfig {
INSTANCE;
private String serverUrl = "https://api.veewom.com";
public String getServerUrl() { return serverUrl; }
public void setServerUrl(String url) { this.serverUrl = url; }
}
Defines an interface for creating objects, letting subclasses decide which concrete class to instantiate without exposing instantiation logic to the client.
// Common Interface
interface Notification {
void notifyUser();
}
// Concrete Implementations
class SMSNotification implements Notification {
public void notifyUser() { System.out.println("Sending SMS alert via Twilio..."); }
}
class EmailNotification implements Notification {
public void notifyUser() { System.out.println("Sending Email via Amazon SES..."); }
}
// Factory
class NotificationFactory {
public static Notification createNotification(String channel) {
if (channel == null || channel.isEmpty()) return null;
switch (channel.toUpperCase()) {
case "SMS": return new SMSNotification();
case "EMAIL": return new EmailNotification();
default: throw new IllegalArgumentException("Unknown channel: " + channel);
}
}
}
public class FactoryDemo {
public static void main(String[] args) {
Notification note = NotificationFactory.createNotification("SMS");
note.notifyUser();
}
}
Solves the anti-pattern of "telescoping constructors" when an object has many attributes (some mandatory, some optional):
public class Server {
// Mandatory fields
private final String hostname;
private final int port;
// Optional fields
private final boolean sslEnabled;
private final int maxConnections;
private Server(Builder builder) {
this.hostname = builder.hostname;
this.port = builder.port;
this.sslEnabled = builder.sslEnabled;
this.maxConnections = builder.maxConnections;
}
public static class Builder {
private final String hostname;
private final int port;
private boolean sslEnabled = false;
private int maxConnections = 100;
public Builder(String hostname, int port) {
this.hostname = hostname;
this.port = port;
}
public Builder enableSsl(boolean enable) {
this.sslEnabled = enable;
return this;
}
public Builder maxConnections(int max) {
this.maxConnections = max;
return this;
}
public Server build() {
return new Server(this);
}
}
@Override
public String toString() {
return "Server[" + hostname + ":" + port + ", SSL=" + sslEnabled + ", Max=" + maxConnections + "]";
}
}
public class BuilderDemo {
public static void main(String[] args) {
Server prodServer = new Server.Builder("veewom.com", 443)
.enableSsl(true)
.maxConnections(5000)
.build();
System.out.println(prodServer);
}
}
Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified automatically:
import java.util.ArrayList;
import java.util.List;
// Observer Interface
interface StockSubscriber {
void onPriceUpdate(String stockSymbol, double newPrice);
}
// Subject / Publisher
class StockExchange {
private final List subscribers = new ArrayList<>();
public void register(StockSubscriber sub) { subscribers.add(sub); }
public void unregister(StockSubscriber sub) { subscribers.remove(sub); }
public void setPrice(String symbol, double price) {
for (StockSubscriber sub : subscribers) {
sub.onPriceUpdate(symbol, price);
}
}
}
public class ObserverDemo {
public static void main(String[] args) {
StockExchange bse = new StockExchange();
// Lambda expressions as observers!
bse.register((symbol, price) -> System.out.printf("Trader App 1: %s is now $%.2f%n", symbol, price));
bse.register((symbol, price) -> System.out.printf("SMS Notification: Alert! %s at $%.2f%n", symbol, price));
bse.setPrice("GOOGL", 182.50);
}
}
Runtime.getRuntime(), Desktop.getDesktop()StringBuilder.append(), java.net.http.HttpRequest.newBuilder()Calendar.getInstance(), NumberFormat.getInstance()new BufferedReader(new FileReader("file.txt"))Arrays.asList()Collections.sort(list, Comparator)Accelerate your Java mastery by studying these tightly connected core tutorials:
Encapsulation, Polymorphism, and Abstraction foundations.
Thread safety and volatile in Singletons.
Immutable data carriers and builder synergy.
Iterator and Decorator patterns in standard Java.