Doc1
Doc1
AND TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE
COURSE: SDA
LAB:3
Question
Draw a chain of responsibility pattern for logging system/Handlers.
The Chain of Responsibility design pattern, is a behavioral design pattern used to pass a request
along a chain of handlers. Each handler in the chain has the opportunity to process the request or
pass it on to the next handler in the chain.
WORKING PATTERN
1. Handler: Defines an interface for handling requests and optionally maintains a reference to
the next handler in the chain.
2. ConcreteHandler: Implements the handling logic and decides whether to process the
request or pass it to the next handler.
3. Client: Constructs the chain of handlers and sends requests to the first handler in the chain.
EXAMPLE:
LOGGING SYSTEM
The goal is to create a logging system where different types of loggers (Info, Warning, Error)
handle log messages based on their severity levels.
Each logger in the chain either handles the log message or passes it to the next logger in the
chain.
IMPLEMENTATION
Step 1: Define the Handler Interface: Create an interface with methods for setting the next
handler and processing requests.
Step 2: Create Concrete Handlers: Implement the handler interface in multiple classes,
each handling specific requests and passing unhandled requests to the next handler.
Step 3: Set Up the Chain: Create instances of your handlers and link them together by
setting the next handler for each one.
Step 4: Send Requests: Use the first handler in the chain to send requests, allowing each
handler to decide whether to process it or pass it along.
STRUCTURE