zoukankan      html  css  js  c++  java
  • 如何管理庞大的Ajax请求?

    随着Ajax的流行,现在的项目使用Ajax已经很普遍了,可以说是大量的使用Ajax了。由于存在大量的Ajax请求,变得难于管理。于是我们经常统一请求,唯一页面或者唯一的入口。于是便有以下类似的代码:

    public partial class Ajax : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                string comm = Request["comm"];
                if ("editUserInfo".Equals(comm))
                {
                    EditUserInfo();
                }
                else if ("addUserInfo".Equals(comm))
                {
                    AddUserInfo();
                }
                ……
                  else if ("deleteUserInfo".Equals(comm))
                {
                   DeleteUserInfo();
                }
               Response.End();
            }
    
            public void EditUserInfo()
            {
               ……
             }
            ……
    }
     
    随着客户需求的变化、二期开发,也许有着更多的Ajax请求,这将制造了庞大的if…else if… … else if…,同时每个请求都有
    相应的操作,这将造成这个类过于庞大,不利于管理、理解。
     
    Factory模式正适用于这种场合。它使用一个类封装创建逻辑和客户代码的实例化/配置选项。UML图如下(用word画,不是很标准):
    QQ截图未命名 
     

    示例:

    一、创建抽象类AjaxComm。

    public abstract class AjaxComm
    {
        public HttpRequest Request =HttpContext.Current.Request;
        public HttpResponse Response = HttpContext.Current.Response;
        public abstract void Execute();
    }

    二、Factory类,根据用户请求的Comm命令,创建相应的类,并执行相应的操作。

    public partial class Ajax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string comm = Request["comm"];
            //根据comm创建相应的类。类的命令规则:比如comm为“AddUser“,则相应的类名为“AddUserComm”
            AjaxComm ajaxComm = (AjaxComm)Assembly.Load("Web").CreateInstance("Web.Ajax." + comm+"Comm");
            if (ajaxComm != null)
            {
                ajaxComm.Execute();   
            }
            Response.End();
         }
    }

    三、实现相应的命令类。

    public class EditImgComm:AjaxComm
    {
        public override void Execute()
        {
            ……
            Response.Write("Success");
         }
    }

      # 优点与缺点#                                                                      

      +合并创建逻辑和实例化/配置选项。                                                                                       

      +将客户代码与创建逻辑解耦。                                                                                              

      -如果可以直接实例化,会使设计复杂化。                                                                                 

  • 相关阅读:
    Effective C++笔记:继承与面向对象设计
    华为手机连不上adb解决方法
    android手机出现sqlite3 not found的解决方法
    adb permission denied
    Apache2.4为什么启动报错Cannot load php5apache2_4.dll into server
    界面排版-TableLayout的stretchColumns方法
    java中静态代码块的用法 static用法详解(转)
    Activity not started, its current task has been brought to the front
    (转)在低版本的SDK里使用高版本函数@SuppressLint("NewApi") or @TargetApi?
    Android笔记:解决 Your project contains error(s),please fix them before running your application问题
  • 原文地址:https://www.cnblogs.com/chingho/p/1818373.html
Copyright © 2011-2022 走看看