“Handler” pattern?

前端 未结 6 1750
臣服心动
臣服心动 2020-12-31 07:11

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

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-31 07:42

    I think the goal of avoiding recompiles is much better served by a COM influenced design. What extra flexibility do you get from this:

    IHandler UserHandler = ...;
    
    Dictionary result = UserHandler.Handle(
        new Dictionary{
            { "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" });
    }
    

提交回复
热议问题