C# “处理程序”模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/641426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Handler" pattern?
提问by Andy White
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:
我遇到过一种被称为“处理程序模式”的设计模式,但我在任何地方都找不到对这种模式的任何真正引用。它基本上只是一个单一方法接口,允许您轻松扩展后端的功能,而无需让客户端重新编译。对于必须处理许多不同类型请求的 Web 服务可能很有用。下面是一个例子:
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.
args 通常包括一个键,如“Action”,其值告诉实现要做什么。可以传入额外的参数来为 impl 提供更多信息。然后 impl 传回客户端“应该”理解的任意参数列表。
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.
编辑:更多信息:按照我所看到的实现方式,“根”处理程序将充当其他具体处理程序的调度程序(也许?)。根处理程序有一个“HandlerResolver”,它根据消息的内容决定哪个具体处理程序应该获取消息。也许它实际上就像一个“调度员”模式,虽然我也不知道这是否真的是一个模式。我想它的根也可能有一个责任链模式,它允许您将一堆具体的处理程序链接在一起,然后让他们决定由哪个处理程序来处理它。
采纳答案by Javier
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.
这是对没有它们的语言进行闭包的 OOP 方式。它没有“模式”名称,因为在函数式语言中这是显而易见的工作方式。在 OOP 语言上,OTOH,您必须做一些工作,因此它似乎是一个可命名的习语。“处理程序”听起来不错。
(it's not a singleton, BTW)
(这不是单身人士,顺便说一句)
回答by gnovice
I don't know if it's really recommended, but I've actually had to use that kind of pattern in some MATLAB applications I've written to mimic reference-like behavior for objects (which is needless now with newer versions).
我不知道它是否真的被推荐,但我实际上不得不在我编写的一些 MATLAB 应用程序中使用这种模式来模拟对象的类似引用的行为(现在新版本不需要了)。
Ironically, I actually called the function "handler". My object simply stored one field containing a function handle reference (@handler) and methods were just wrappers that called this function. For example, an overloaded GET function for the object would just call:
具有讽刺意味的是,我实际上称该函数为“处理程序”。我的对象只是存储了一个包含函数句柄引用 (@handler) 的字段,而方法只是调用此函数的包装器。例如,对象的重载 GET 函数只会调用:
object.handler('get',...input argument list...)
I'm not sure if this is considered a "good" design choice in other languages. I chose it out of necessity, because it was the only way I came across to create reference-like behavior in MATLAB (the handler function had access to a workspace of initialized data that I wouldn't have to pass in and out of the various method calls). The newest versions of MATLAB now have a HANDLE class that can do this stuff in a much cleaner way.
我不确定这在其他语言中是否被认为是“好的”设计选择。我选择它是出于必要,因为这是我在 MATLAB 中创建类似引用行为的唯一方法(处理程序函数可以访问已初始化数据的工作区,我不必传入和传出各种方法调用)。最新版本的 MATLAB 现在有一个 HANDLE 类,可以以更简洁的方式完成这些工作。
回答by OscarRyz
I use it under the name of "SingletonRegistry"
我以“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 alsosearching the web for that name.
我发现这个类也在网上搜索这个名字。
Looks like the same isn't?
好像不是一样?
回答by C???
Because you had the word "Action" in your post, I am led to believe that this might be a part of the Command pattern. Check out the Wiki and search for "Handler"... maybe this will give a little more insight.
因为您在帖子中使用了“操作”一词,所以我相信这可能是命令模式的一部分。查看 Wiki 并搜索“处理程序”...也许这会提供更多见解。
回答by Simon Buchan
I think the goal of avoiding recompiles is much better served by a COM influenced design. What extra flexibility do you get from this:
我认为受 COM 影响的设计可以更好地实现避免重新编译的目标。您从中获得了什么额外的灵活性:
IHandler UserHandler = ...;
Dictionary<string,string> result = UserHandler.Handle(
new Dictionary<string, string>{
{ "Action", "AddUser" },
{ "UserName", "Joe Bloggs" },
{ "Age", "23" } });
NewUserId = Int.Parse(result["UserId"]);
over:
超过:
IUserHandler UserHandler = ...;
AddUserResult result = UserHandler.AddUser(new AddUserArgs {
UserName = "Joe Bloggs",
Age = 23 });
NewUserId = result.UserId;
when you can extend the actions, results, and arguments:
何时可以扩展操作、结果和参数:
IUserHandler UserHandler = ...;
AddUserResult2 result = UserHandler.AddUser(new AddUserArgs2 {
UserName = "Joe Bloggs",
Age = 23,
Password = "xyzzy" });
NewUserId = result.UserId;
SessionId = result.SessionId;
IUserHandler2 UserHandler2 = UserHandler as IUserHandler2;
if (UserHandler2 != null)
{
LoginUserResult loginResult = UserHandler2.LoginUser(new LoginUserArgs {
UserId = NewUserId,
SessionId = SessionId,
Password = "xyzzy" });
}
回答by Hazok
I've seen the "Handler" classes around in legacy code bases, especially for web services. From what I've seen though, these typically turn out to be an Adapter, Facade, or some similar pattern and ends up getting called a Handler since it is handling a request.
我在遗留代码库中看到了“处理程序”类,尤其是对于 Web 服务。但据我所见,这些通常是适配器、外观或一些类似的模式,最终被称为处理程序,因为它正在处理请求。