๐จ Most Important Java Design Patterns
Design patterns provide reusable solutions to common software problems. They are the backbone of clean, flexible, and maintainable Java architecture. ๐
1. Creational Patterns ๐️
These patterns handle object creation — they abstract the instantiation logic, making systems more flexible and decoupled.
-
Singleton ✅
Ensures a class has only one instance and provides a global access point. Commonly used for logging, configuration, or caching.public class Logger { private static Logger instance; private Logger() {} public static Logger getInstance() { if (instance == null) instance = new Logger(); return instance; } }Examples:
Runtime.getRuntime(), Spring Beans (default singleton scope). -
Factory Method ๐ก
Defines an interface for creating objects, but lets subclasses decide which class to instantiate.interface Shape { void draw(); } class Circle implements Shape { public void draw() { /* ... */ } } class Square implements Shape { public void draw() { /* ... */ } } class ShapeFactory { Shape getShape(String type) { if (type.equals("circle")) return new Circle(); else if (type.equals("square")) return new Square(); return null; } }Used in: JDBC
DriverManager.getConnection(), Spring’sBeanFactory. -
Builder ๐งฐ
Builds complex objects step by step. Useful when constructors have too many parameters.class Car { private String engine; private int wheels; static class Builder { private String engine; private int wheels; Builder setEngine(String e) { engine = e; return this; } Builder setWheels(int w) { wheels = w; return this; } Car build() { return new Car(engine, wheels); } } private Car(String engine, int wheels) { this.engine = engine; this.wheels = wheels; } }Examples:
StringBuilder,Stream.Builder, and Lombok’s@Builder. -
Prototype ๐งฌ
Creates new objects by copying existing ones instead of creating from scratch.class Prototype implements Cloneable { private int value; public Prototype(int value) { this.value = value; } public Prototype clone() throws CloneNotSupportedException { return (Prototype) super.clone(); } }Used when: object creation is expensive and similar instances are frequently needed (e.g., configuration templates).
2. Structural Patterns ๐งฉ
These patterns deal with class and object composition. They help form larger structures while keeping them flexible and efficient.
-
Adapter ๐
Allows incompatible interfaces to work together by acting as a translator.// Adapter example interface MediaPlayer { void play(String audioType, String fileName); } class AudioPlayer implements MediaPlayer { public void play(String audioType, String fileName) { /* ... */ } } class MediaAdapter implements MediaPlayer { // Converts one interface to another }Examples:
InputStreamReader,Arrays.asList(). -
Decorator ๐
Dynamically adds new responsibilities to an object without altering its structure.// Decorator example interface Coffee { double cost(); } class SimpleCoffee implements Coffee { public double cost() { return 2; } } class MilkDecorator implements Coffee { private Coffee coffee; MilkDecorator(Coffee c) { coffee = c; } public double cost() { return coffee.cost() + 1; } }Used in: Java I/O Streams (
BufferedReader,DataOutputStream). -
Facade ๐
Provides a unified, simplified interface to a complex subsystem.// Facade example class HomeTheaterFacade { void watchMovie() { /* turn on projector, dim lights, play movie */ } }Examples: Spring’s
JdbcTemplate,RestTemplate. -
Proxy ๐งค
Provides a placeholder or control access to another object — can add caching, logging, or security.// Proxy example interface Service { void execute(); } class RealService implements Service { public void execute() { /* heavy task */ } } class ProxyService implements Service { private RealService realService = new RealService(); public void execute() { System.out.println("Logging access..."); realService.execute(); } }Examples: Spring AOP proxies, Hibernate lazy loading.
3. Behavioral Patterns ๐
These patterns focus on object interaction and communication. They define how responsibilities are distributed among objects.
-
Observer ๐
Defines a one-to-many dependency between objects, so when one changes state, all dependents are notified.interface Observer { void update(); } class Subject { private Listobservers = new ArrayList<>(); void addObserver(Observer o) { observers.add(o); } void notifyObservers() { observers.forEach(Observer::update); } } Used in: Java
PropertyChangeListener, Spring Events, GUI frameworks. -
Strategy ⚙️
Defines a family of algorithms, encapsulates each one, and makes them interchangeable.interface PaymentStrategy { void pay(int amount); } class CreditCardPayment implements PaymentStrategy { public void pay(int amount) { /* ... */ } } class PayPalPayment implements PaymentStrategy { public void pay(int amount) { /* ... */ } }Examples:
Comparator, sorting algorithms, authentication strategies. -
Command ๐ฎ
Encapsulates a request as an object, allowing parameterization and queuing of operations.interface Command { void execute(); } class LightOnCommand implements Command { public void execute() { /* turn light on */ } } class RemoteControl { private Command command; void setCommand(Command cmd) { command = cmd; } void pressButton() { command.execute(); } }Used in: GUI buttons, undo/redo systems, job queues.
-
Template Method ๐
Defines the skeleton of an algorithm, deferring some steps to subclasses.abstract class DataParser { void parseData() { readData(); processData(); writeData(); } abstract void readData(); abstract void processData(); abstract void writeData(); }Examples:
HttpServlet.doGet()anddoPost()in the Servlet API. -
Iterator ๐
Provides a way to access elements of a collection sequentially without exposing the underlying structure.Iteratorit = list.iterator(); while (it.hasNext()) { System.out.println(it.next()); } Examples: Java Collections API, Streams API.
๐ Conclusion
Understanding Java design patterns helps you write clean, maintainable, and reusable code. Start small — implement one pattern at a time and observe how your architecture improves. ๐ก✨
Related Reads:
Applying SOLID Principles in Java
Key Microservices Design Patterns
Java OOP Fundamentals
Labels: Java, Design Patterns, Creational Patterns, Structural Patterns, Behavioral Patterns
Comments
Post a Comment