工厂模式的实例解读:

工厂模式的解读:       

#region  店铺详情

 

        /// <summary>

        /// Supporters the detail.

        /// </summary>

        /// <returns></returns>

        public ActionResult ShopDedail()

        {

            return View();

        }

        public ActionResult ShopDedailHelper()

        {

            ShopBussiness shop = new ShopBussiness();

            IDictionary<string, string> pageParmas = PageParams();

            Carpa.Web.Script.IHashObject Page = new Carpa.Web.Script.HashObject();

            Carpa.Web.Script.IHashObjectList list = shop.GetShopList();

            Page["rows"] = list;

            return Json(Page, "text/html", JsonRequestBehavior.AllowGet);

        }

 

#endregion

第一步:

  //public IDictionary<string, string> PageParams = null;

        public virtual IDictionary<string, string> PageParams()

        {

            //取得页面查询参数(总是以key:value方式存取,可以看做两条链表存储)

            IDictionary<string, string> PageParams = new Dictionary<string, string>();

            foreach (string sky in Request.QueryString.AllKeys)

            {

                PageParams.Add(sky,Request.QueryString[sky]);

            }

            return PageParams;

        }

 

//取得页面查询参数(总是以key:value方式存取,可以看做两条链表存储)

            //页面即是对象(数据+控件)

 

第二步:

public IHashObjectList GetShopList()

        {

            //返回值为HashObjectList是为了得到json数据源

            using (DbHelper db = IProgram.Utils.AppUtils.CreateDbHelper())

            {

                //连接数据库(任意形式)

                IProgram.Base.BaseService iserver = null;

                //晚期实例化子类为超类或者接口对象,那么ContextListDataFactory应该返回IBaseData 或其子类型的对象

                //而且看起来像是一座工厂对应一个类,因此ListDataFactoryType是个标志的集合(枚举集合,字符串集合,位集合等等)

第三步:

  IListData list = IProgram.DAL.DataFactory.ContextListDataFactory(DAL.ListDataFactoryType.ShelvesGoodsListData);

                //以上只是便于扩展,对于解决具体的问题,并不是必须的,

                //then,在创建的实例中,可以进行各种各样的操作

第四步:

                list._DbHelper = db;

  iserver = new IProgram.BLL.ShopService(list);

                int totalRecord = -1;

                IHashObjectList ilist = iserver.FindHashList(new HashObject(), 0, 10, ref totalRecord);

                if (ilist == null) return ilist;

                foreach (IHashObject objRowData in ilist)

                {

                    //objRowData["goodsname"] = "<td thid='th4' align='left' style='width: 150px;'><div class='tableText' style='width: 150px;'><a href='javascript:;' onmouseout='hideDiv()' onmouseover='showDiv(4)'>" + objRowData.GetValue<string>("goodsname") + "</a></div></td>";

                }

                return ilist;

            }

第三步:

public static IBaseData ContextBaseFactory(string className)

        {

            if (string.IsNullOrEmpty(className))

            {

                throw new ArgumentNullException("参数className 数据工厂为空。");

            }

            className = string.Format("IProgram.DAL.{0}", className);

            //取得全名,准备动态创建类

            Type type = SysUtils.GetAssembly("IProgram.DAL").GetType(className);

            if (type == null)

            {

                throw new Exception(string.Format("【{0}】数据工厂对象未定义或命名空间不正确!", className));

            }

            return (IBaseData) Activator.CreateInstance(type);

            //动态创建,格式化成基础类型+工作类型

        }

第四步:

namespace IProgram.DAL

{

    public enum ListDataFactoryType

    {

        CtgListData = 0,

        DeptListData = 1,

        ShelvesGoodsListData = 2,

    }

}

以上就是对工厂模式的理解.

 

 

 

 

 

 

posted @ 2015-04-08 13:44  ip8888  阅读(200)  评论(0编辑  收藏  举报