Design patterns with examples
Design patterns are standardized solutions to common software design problems. Here's a list of some commonly recognized design patterns categorized by their purpose, along with brief descriptions and examples:
Creational Patterns
-
Singleton Pattern
- Purpose: Ensures a class has only one instance and provides a global point of access to it.
- Example: Logging classes, configuration settings.
-
Factory Pattern
- Purpose: Creates objects without specifying the exact class of object that will be created.
- Example:
ConnectionFactory
creates different database connections based on input.
-
Builder Pattern
- Purpose: Separates the construction of a complex object from its representation, allowing the same construction process to create different representations.
- Example:
StringBuilder
in Java.
-
Prototype Pattern
- Purpose: Creates new objects by copying a prototype object, thus avoiding the overhead of creating objects using the
new
keyword. - Example: Cloning objects in Java using
clone()
method.
- Purpose: Creates new objects by copying a prototype object, thus avoiding the overhead of creating objects using the
Structural Patterns
-
Adapter Pattern
- Purpose: Allows objects with incompatible interfaces to collaborate.
- Example:
ArrayAdapter
in Android converts data for a list view.
-
Decorator Pattern
- Purpose: Attaches additional responsibilities to an object dynamically, providing a flexible alternative to subclassing for extending functionality.
- Example:
BufferedReader
andBufferedWriter
in Java IO.
-
Facade Pattern
- Purpose: Provides a unified interface to a set of interfaces in a subsystem.
- Example:
javax.faces.context.FacesContext
in JavaServer Faces (JSF).
-
Bridge Pattern
- Purpose: Decouples an abstraction from its implementation so that the two can vary independently.
- Example: GUI frameworks that separate windowing systems (e.g., Windows, macOS) from the application.
Behavioral Patterns
-
Observer Pattern
- Purpose: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
- Example: Event listeners in GUI frameworks.
-
Strategy Pattern
- Purpose: Defines a family of algorithms, encapsulates each one, and makes them interchangeable. It lets the algorithm vary independently from clients that use it.
- Example: Sorting algorithms where different strategies (e.g., quicksort, mergesort) are interchangeable.
-
Command Pattern
- Purpose: Encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
- Example: GUI command invokers like menus, toolbars.
-
Iterator Pattern
- Purpose: Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
- Example: Iterators in Java Collections Framework.
Concurrency Patterns
-
Producer-Consumer Pattern
- Purpose: Coordinates the actions of threads that produce data and threads that consume it.
- Example: BlockingQueue in Java for thread-safe producer-consumer communication.
-
Read-Write Lock Pattern
- Purpose: Allows multiple readers but only one writer to access a shared resource concurrently.
- Example:
ReadWriteLock
interface in Java.
-
Thread Pool Pattern
- Purpose: Manages a group of threads to achieve better performance and reduce the overhead of creating new threads.
- Example:
ExecutorService
in Java.
Architectural Patterns
-
MVC (Model-View-Controller) Pattern
- Purpose: Separates an application into three main components: Model (data), View (UI), and Controller (logic), promoting separation of concerns and modularity.
- Example: Web applications built using frameworks like Spring MVC.
-
Layered Architecture Pattern
- Purpose: Organizes software into distinct layers (e.g., presentation, business logic, data access), each responsible for a specific aspect of functionality.
- Example: Enterprise applications following layers such as UI, service, and data access.
Published on: Jul 04, 2024, 11:50 AM