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();
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();
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();
Also used in Lombok: @Builder
Prototype Pattern
Create objects by cloning.
// Example: Object clone()
Employee cloned = (Employee) employee.clone();
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);
Used when integrating legacy systems.
Decorator Pattern
Adds functionality at runtime.
// Example: I/O Streams
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("file.txt"));
Also in Spring: HandlerInterceptor, Filter
Facade Pattern
Provides a simplified interface.
// Example: JDBC
Connection conn = DriverManager.getConnection(url);
// JDBC hides complex driver logic!
Also: HibernateTemplate, RestTemplate
Proxy Pattern
Controls access to an object.
// Example: Java Dynamic Proxy (used in Spring AOP)
Proxy.newProxyInstance(...)
Used for authentication, logging, lazy loading.
Behavioral Patterns
Observer Pattern
Notifies when state changes.
// Example: PropertyChangeListener (Java Beans)
button.addActionListener(...);
Spring Example: ApplicationEventPublisher
Strategy Pattern
Different algorithms, same interface.
// Example: Comparator
Collections.sort(list, (a, b) -> a.compareTo(b));
Spring: AuthenticationStrategy, RetryStrategy
Template Method Pattern
Defines skeleton → subclasses override steps.
// Example: HttpServlet.doGet() / doPost()
abstract class HttpServlet {
protected void doGet() {}
}
Spring JDBC Template follows this pattern.
Command Pattern
Encapsulates a request as an object.
// Example: Runnable
ExecutorService.exec(new MyTask());
Used in scheduling, undo operations.
Iterator Pattern
Sequential access without exposing structure.
// Example:
Iterator<String> itr = list.iterator();
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)