Java Design Patterns

repository·master·Indexed 13 days ago

https://github.com/iluwatar/java-design-patterns

A comprehensive collection of Java design patterns implemented with real-world, open-source technologies. It serves as a reference for architects and a practical tutorial for developers, featuring implementations of patterns such as Abstract Document, Abstract Factory, and Active Object.

Tokens
284.5K
Snippets
418
Records
926
Agent score
98%

What's inside Java Design Patterns

  1. Explore Java Design Patterns

    master

    This repository provides a collection of Java design patterns implemented using popular, battle-proven open-source technologies. You can browse patterns to learn how to implement them using the following methods:

    • Search by name: Find a specific pattern directly.
    • Browse by tags: Filter patterns using tags like Performance, Gang of Four, or Data access.
    • Browse by category: Explore patterns by their functional type, such as Creational or Behavioral.

    The source code examples are heavily commented and serve as programming tutorials for implementing each specific pattern. It is recommended to be familiar with Software Design Principles before diving into the implementations. The project advocates for simplicity, suggesting that patterns should only be introduced when necessary for practical extensibility, following principles like KISS (Keep It Simple, Stupid) and YAGNI (You Ain't Gonna Need It).

  2. What is the Monostate pattern?

    master

    The Monostate pattern (also known as Borg) is a creational design pattern that provides singleton-like behavior by ensuring all instances of a class share the same state.

    Unlike the Singleton pattern, which restricts a class to a single instance, Monostate allows you to create any number of instances. However, because the state is managed through class-level (static) fields, every instance acts upon the same data. This provides a 'conceptual singleton' where clients interact with regular objects, unaware that the underlying state is shared.

  3. What is the Naked Objects design pattern?

    master

    The Naked Objects pattern (also known as Transparent Objects) is an architectural pattern used to enable rapid development by automatically generating a user interface directly from domain object definitions.

    It is based on three core principles:

    1. Encapsulated Business Logic: All business logic is encapsulated within the domain objects.
    2. Object-Oriented UI: The user interface is a direct representation of the domain objects, where user actions consist of creating, retrieving, or invoking methods on those objects.
    3. Automatic UI Generation: The UI is entirely created automatically from the domain object definitions, typically using reflection or source code generation.

    This ensures that any changes to the domain model (e.g., adding a new field) are immediately reflected in the UI without manual interface updates.

  4. What is the Balking Design Pattern?

    master

    The Balking pattern is a concurrency design pattern used to prevent an object from executing certain code if it is in an incomplete or inappropriate state. Instead of making the caller wait (as in the Guarded Suspension pattern), the object 'balks'—it simply returns or ignores the request if the required state conditions are not met.

    When to use it:

    • When you want to invoke an action on an object only when it is in a specific state.
    • In multithreaded applications where certain actions should only proceed when specific conditions are met, and those conditions change over time due to concurrent operations.
    • When objects are temporarily in a state that prevents certain actions for an unknown duration.

    Real-world analogies:

    • A washing machine that refuses to start if the door is open.
    • A ZIP file reader that refuses to read data if the file is not currently open.
  5. What is the Command pattern and when to use it

    master

    The Command pattern is a behavioral design pattern that encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

    When to use it:

    • Encapsulate actions: Represent actions as objects (or callbacks) to standardize how operations are triggered.
    • Deferred execution: Specify, queue, and execute requests at different times. Command objects can have a lifecycle independent of the original request.
    • Undo/Redo functionality: Store the state required to reverse an operation within the command itself. By maintaining a history of executed commands, you can implement unlimited undo and redo capabilities.
    • Logging and recovery: Log changes to a persistent store so they can be re-applied in case of a system crash.
    • Transaction modeling: Build high-level operations (transactions) composed of multiple primitive operations using a common interface.
    • Request history: Maintain a history of all requests made to the system.
    • Callback implementation: Use commands as object-oriented alternatives to procedural callback functions.
  6. What is the Specification Pattern and when to use it

    master

    The Specification pattern encapsulates business rules and criteria that an object must satisfy. This allows you to check these rules in various parts of an application without coupling the rules to the domain objects themselves.

    When to use it:

    • When you need to filter objects based on different, varying criteria.
    • When filtering criteria can change dynamically at runtime.
    • When you have complex business rules that must be reused across different parts of an application.

    Real-world applications:

    • Validating user inputs in enterprise applications.
    • Filtering search results in e-commerce applications.
    • Business rule validation in Domain-Driven Design (DDD).
  7. What is the Actor Model pattern?

    master

    The Actor Model is a pattern for building highly concurrent, distributed, and fault-tolerant systems. It treats 'actors' as the universal primitives of concurrent computation.

    Key characteristics include:

    • Isolation: Actors are independent workers that do not share memory.
    • Asynchronous Communication: Actors interact exclusively through asynchronous message passing.
    • No Shared Mutable State: This eliminates common concurrency issues like race conditions.
    • Loose Coupling: Components are isolated and communicate only via messages.
  8. What is the Microservices Distributed Tracing pattern?

    master

    The Microservices Distributed Tracing pattern provides a mechanism to trace and correlate requests as they traverse multiple microservices in a distributed system. It enables end-to-end visibility, allowing developers to follow a single request's journey through all interacting services, which is essential for troubleshooting, identifying performance bottlenecks, and diagnosing failures in complex multi-service environments.

    Also known as:

    • Distributed Request Tracing
    • End-to-End Microservice Tracing
  9. What is the MapReduce design pattern?

    master

    The MapReduce pattern is a programming model used to efficiently process large-scale datasets by dividing computation into two main phases: Map and Reduce. This approach allows for parallel and distributed execution across multiple nodes, making it a fundamental technique in big data analytics.

    The Two Main Phases:

    1. Map Step: The input is divided into smaller sub-problems and distributed to worker nodes. Each worker processes its sub-problem and passes the result back to a master node.
    2. Reduce Step: The master node collects the answers from all sub-problems and combines them to form the final output.

    This pattern is also known as Map-Reduce or Divide and Conquer for Data Processing.

  10. What is the Notification design pattern?

    master
    The Notification design pattern (also known as Event Listener) facilitates asynchronous communication between different parts of a system. It allows an object to automatically notify a list of interested observers about changes or events without needing to know the specific details or identities of those subscribers. This pattern is primarily used to decouple the producer of an event from its consumers, enhancing system flexibility and reusability.
  11. What is the Active Object pattern and when to use it

    master

    The Active Object pattern decouples method execution from method invocation to improve concurrency and responsiveness in multithreaded applications. It encapsulates tasks within objects that possess their own dedicated thread and a message queue (pending requests).

    Use the Active Object pattern when:

    • You need to handle asynchronous tasks without blocking the main thread.
    • You need to interact with external resources asynchronously.
    • You want to improve application responsiveness.
    • You need to manage concurrent tasks in a modular and maintainable way.

    Core Components:

    • Proxy: Provides an interface towards clients with publicly accessible methods.
    • Interface: Defines the method request on an active object.
    • Pending Requests: A list (queue) of requests from clients.
    • Scheduler: Decides which request to execute next.
    • Implementation: The actual logic of the active object method.
    • Callback/Variable: A mechanism for the client to receive the result.