Certainly! Here's a list of design patterns commonly used in Java applications, along with brief examples of each:
-
Singleton Pattern: - Example: Java's
java.lang.Runtime
class, which represents the runtime environment of the Java application, follows the Singleton pattern. -
Factory Pattern: - Example:
java.util.Calendar
, which provides methods to create instances ofjava.util.Date
based on different parameters, is an implementation of the Factory pattern. -
Builder Pattern: - Example:
StringBuilder
in Java provides a builder-like approach to create and manipulate strings efficiently. -
Prototype Pattern: - Example:
java.lang.Object#clone()
method, which creates a copy of an object, is an implementation of the Prototype pattern. -
Strategy Pattern:- Example:
java.util.Comparator
interface in Java is an example of the Strategy pattern. It allows multiple algorithms for comparing objects to be implemented. -
Iterator Pattern: Example: The
java.util.Iterator
interface in Java's standard library is an implementation of the Iterator pattern, allowing traversal of collections. -
DAO (Data Access Object) Pattern: - Example: In Java's Spring Framework, the
org.springframework.jdbc.core.JdbcTemplate
class provides a data access object for interacting with databases. -
Composite Pattern: - Example: The
java.awt.Container
class, which represents a component that can contain other components, follows the Composite pattern. • Spring provides composite annotations like @RestController and @RequestMapping, which simplify the creation of RESTful APIs by composing smaller handler methods. -
Abstract Factory Pattern: Example:
javax.xml.parsers.DocumentBuilderFactory
provides an abstract factory for creating different types of XML parsers. -
Adapter Pattern: Example:
java.util.Arrays#asList()
method, which allows an array to be viewed as a List, is an example of the Adapter pattern. -
Decorator Pattern: Example:
java.io.BufferedInputStream
andjava.io.BufferedOutputStream
classes in Java's I/O library use the Decorator pattern to add buffering functionality. -
Observer Pattern:- Example: The
java.util.Observable
class andjava.util.Observer
interface in Java's standard library provide an implementation of the Observer pattern. -
Template Method Pattern: Example: The
javax.servlet.http.HttpServlet
class in Java's Servlet API uses the Template Method pattern to define the lifecycle of a servlet. -
Proxy Pattern: Example: The
java.lang.reflect.Proxy
class in Java allows the creation of dynamic proxy objects, which can intercept and delegate method calls. -
MVC (Model-View-Controller) Pattern: Example: Java's Swing GUI framework follows the MVC pattern. For example, the
javax.swing.JTable
class represents the view, while thejavax.swing.table.TableModel
interface represents the model.
These examples illustrate the application of various design patterns in real-world Java libraries and frameworks. Understanding and applying these patterns can enhance code organization, maintainability, and extensibility in Java applications.