Home   programming  

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

  1. Singleton Pattern

    • Purpose: Ensures a class has only one instance and provides a global point of access to it.
    • Example: Logging classes, configuration settings.
  2. 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.
  3. 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.
  4. 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.

Structural Patterns

  1. Adapter Pattern

    • Purpose: Allows objects with incompatible interfaces to collaborate.
    • Example: ArrayAdapter in Android converts data for a list view.
  2. Decorator Pattern

    • Purpose: Attaches additional responsibilities to an object dynamically, providing a flexible alternative to subclassing for extending functionality.
    • Example: BufferedReader and BufferedWriter in Java IO.
  3. Facade Pattern

    • Purpose: Provides a unified interface to a set of interfaces in a subsystem.
    • Example: javax.faces.context.FacesContext in JavaServer Faces (JSF).
  4. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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

  1. 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.
  2. Read-Write Lock Pattern

    • Purpose: Allows multiple readers but only one writer to access a shared resource concurrently.
    • Example: ReadWriteLock interface in Java.
  3. 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

  1. 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.
  2. 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  


Add your comment