Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Handler" pattern?

I've come across a design pattern that's been referred to as a "Handler Pattern," but I can't find any real references to this pattern anywhere. It's basically just a one-method interface that allows you to easily extend the functionality on the back-end without making clients recompile. Might be useful for a web-service that has to handle many different types of requests. Here's an example:

public interface IHandler
{
    IDictionary<string, string> Handle(IDictionary<string, string> args);
}

The args would typically include one key like "Action" with a value that tells the implmentation what to do. Additional args can be passed in to give the impl more information. The impl then passes back an arbitrary list of args that the client "should" understand.

Is this an anti-pattern, or maybe another pattern in disguise? Is this type of design recommended?

EDIT: A little more info: The way I've seen this implemented, the "root" Handler would act as a dispatcher to other concrete handlers (maybe?). The root handler has a "HandlerResolver," which decides which concrete handler should get the message based on it's contents. Maybe it's actually like a "dispatcher" pattern, although I don't know if that's really a pattern either. I guess it could also have a chain-of-responsibility pattern in the root, that allows you to chain together a bunch of concrete handlers, then let them decide which one will handle it.

like image 540
Andy White Avatar asked Mar 13 '09 04:03

Andy White


People also ask

What is Handler design pattern?

The handler pattern abstracts a business process into some action being carried out in response to a request. This abstraction works well when the client should not have to know about the specific implementation of the handler and the action cannot be better represented by a more explicit interface.

What is chain design pattern?

Chain of responsibility pattern is used to achieve loose coupling in software design where a request from client is passed to a chain of objects to process them.

What is Handler pattern in Java?

The pattern allows multiple objects to handle the request without coupling sender class to the concrete classes of the receivers. The chain can be composed dynamically at runtime with any handler that follows a standard handler interface.

Which is considered an example of Chain of Responsibility pattern?

A Chain of Responsibility Pattern says that just "avoid coupling the sender of a request to its receiver by giving multiple objects a chance to handle the request". For example, an ATM uses the Chain of Responsibility design pattern in money giving process.


2 Answers

it's the OOP way to do closures on languages that doesn't have them. it didn't have a 'pattern' name because on functional languages it's the obvious way to work. on OOP languages, OTOH, you have to do some work, so it seems a nameable idiom. 'Handler' sounds right.

(it's not a singleton, BTW)

like image 50
Javier Avatar answered Oct 05 '22 00:10

Javier


I use it under the name of "SingletonRegistry"

See this thread

I've use it a couple of times. Specially when the actions to take are unknown upfront ( in the first phases of the design ) or the app should support extreme flexibility.

I load the dictionary either from a file or a database, and create a single instance of the class that will handle the request under certain "key".

I've found this class also searching the web for that name.

Looks like the same isn't?

like image 24
OscarRyz Avatar answered Oct 05 '22 01:10

OscarRyz