DEV Community

shantanu mahakale
shantanu mahakale

Posted on

Quick Recap: Design Patterns in Java (Real Examples)

Design patterns provide reusable solutions to common software problems. Here’s a concise recap with actual Java / Spring framework examples — not generic ones.


Creational Patterns

Singleton

Ensures only one instance exists.

/ Real Example:
Calendar calendar = Calendar.getInstance();
Runtime runtime = Runtime.getRuntime();
Enter fullscreen mode Exit fullscreen mode

Used in logging, DB connections, cache managers.


Factory Method

Creates objects without exposing logic.

// Example: Java Collections
List<String> list = List.of("A", "B"); // Java 9 Factory
NumberFormat format = NumberFormat.getInstance();
Enter fullscreen mode Exit fullscreen mode

Spring Example: BeanFactory.getBean("beanName")


Builder Pattern

Best for creating complex objects with many fields.

// Example: StringBuilder
String result = new StringBuilder()
  .append("Hello ")
  .append("World")
  .toString();
Enter fullscreen mode Exit fullscreen mode

Also used in Lombok: @Builder


Prototype Pattern

Create objects by cloning.

// Example: Object clone()
Employee cloned = (Employee) employee.clone();
Enter fullscreen mode Exit fullscreen mode

Useful when object creation is costly.


Structural Patterns

Adapter Pattern

Converts one interface to another.

// Example: InputStreamReader adapts InputStream to Reader.
Reader reader = new InputStreamReader(inputStream);
Enter fullscreen mode Exit fullscreen mode

Used when integrating legacy systems.


Decorator Pattern

Adds functionality at runtime.

// Example: I/O Streams
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file.txt"));
Enter fullscreen mode Exit fullscreen mode

Also in Spring: HandlerInterceptor, Filter


Facade Pattern

Provides a simplified interface.

// Example: JDBC
Connection conn = DriverManager.getConnection(url);
// JDBC hides complex driver logic!
Enter fullscreen mode Exit fullscreen mode

Also: HibernateTemplate, RestTemplate


Proxy Pattern

Controls access to an object.

// Example: Java Dynamic Proxy (used in Spring AOP)
Proxy.newProxyInstance(...)
Enter fullscreen mode Exit fullscreen mode

Used for authentication, logging, lazy loading.


Behavioral Patterns

Observer Pattern

Notifies when state changes.

// Example: PropertyChangeListener (Java Beans)
button.addActionListener(...);
Enter fullscreen mode Exit fullscreen mode

Spring Example: ApplicationEventPublisher


Strategy Pattern

Different algorithms, same interface.

// Example: Comparator
Collections.sort(list, (a, b) -> a.compareTo(b));
Enter fullscreen mode Exit fullscreen mode

Spring: AuthenticationStrategy, RetryStrategy


Template Method Pattern

Defines skeleton → subclasses override steps.

// Example: HttpServlet.doGet() / doPost()
abstract class HttpServlet {
  protected void doGet() {}
}
Enter fullscreen mode Exit fullscreen mode

Spring JDBC Template follows this pattern.


Command Pattern

Encapsulates a request as an object.

// Example: Runnable
ExecutorService.exec(new MyTask());
Enter fullscreen mode Exit fullscreen mode

Used in scheduling, undo operations.


Iterator Pattern

Sequential access without exposing structure.

// Example:
Iterator<String> itr = list.iterator();
Enter fullscreen mode Exit fullscreen mode

Summary Table

Pattern Real Java Example
Singleton Runtime.getRuntime()
Factory NumberFormat.getInstance()
Builder StringBuilder
Adapter InputStreamReader
Decorator BufferedInputStream
Facade JDBC (DriverManager)
Proxy Spring AOP / Dynamic Proxy
Observer ActionListener
Strategy Comparator
Template Method HttpServlet
Command Runnable

Top comments (0)