avalokkumar / java-design-patterns-guide

Commonly used Design patterns with real world examples

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Design Patterns in Java

This repository provides code examples for various design patterns in Java.

Chain of Responsibility

The Chain of Responsibility design pattern is a behavioral pattern that allows a chain of objects to handle a request. It is often used when there are multiple objects that can handle a request and the specific object to handle the request is unknown.

Use Cases

Some common use cases for the Chain of Responsibility pattern include:

  • Request filtering or validation
  • Authentication and authorization
  • Logging and error handling

Example

An example of using the Chain of Responsibility pattern could be a technical support system. The system could have multiple levels of support handlers, each responsible for handling a specific type of ticket. If a support handler cannot handle a ticket, it passes the ticket to the next handler in the chain until the ticket is either resolved or there are no more handlers.

Command

The Command design pattern is a behavioral pattern that allows an object to encapsulate a request. It separates the object that issues the command from the object that receives and executes the command. This decoupling allows for greater flexibility and extensibility in a system.

Use Cases

Some common use cases for the Command pattern include:

  • Implementing undo and redo functionality
  • Implementing a queue of requests
  • Implementing a macro recording and playback system

Example

An example of using the Command pattern could be a remote control system for a home entertainment system. Each button on the remote control could be associated with a command object, which encapsulates a specific action to be taken (e.g. turning the TV on/off, changing the channel, etc.). When a button is pressed, the associated command object is executed.

Interpreter

The Interpreter design pattern is a behavioral pattern that provides a way to evaluate language grammar or expressions. It is often used to implement DSLs (Domain Specific Languages) or to interpret user input.

Use Cases

Some common use cases for the Interpreter pattern include:

  • Implementing a calculator or math expression evaluator
  • Implementing a search query language
  • Implementing a configuration language for a system

Example

An example of using the Interpreter pattern could be a simple calculator. The calculator could take a string of arithmetic expressions as input, parse the expressions into a syntax tree, and then evaluate the expressions based on the syntax tree. The Interpreter pattern provides a flexible and extensible way to handle various expressions and operators.

About

Commonly used Design patterns with real world examples


Languages

Language:Java 100.0%