码迷,mamicode.com
首页 > 其他好文 > 详细

简单工厂

时间:2014-05-31 21:07:59      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

1、消息产品接口

bubuko.com,布布扣
bubuko.com,布布扣
namespace IBLL
{
    public interface IMsg
    {
        List<Model.Msg> GetMsgList();
    }
}
View Code
bubuko.com,布布扣

2、消息A实现

bubuko.com,布布扣
bubuko.com,布布扣
   public class Msg:IBLL.IMsg
    {
        public List<Model.Msg> GetMsgList()
        {
            //DALFactory.SimpleDALFactory DAL = new DALFactory.SimpleDALFactory();
            //return DAL.GetBLLMsg().GetMsgList();
            List<Model.Msg> userList = new List<Model.Msg>() 
            {
                new Model.Msg(){MsgId=Guid.NewGuid().ToString()},
                new Model.Msg(){MsgId=Guid.NewGuid().ToString()}
            };
            return userList;
        }
    }
View Code
bubuko.com,布布扣

3、消息B实现

bubuko.com,布布扣
bubuko.com,布布扣
   public class Msg:IBLL.IMsg
    {
        public List<Model.Msg> GetMsgList()
        {
            //DALFactory.SimpleDALFactory DAL = new DALFactory.SimpleDALFactory();
            //return DAL.GetBLLMsg().GetMsgList();
            List<Model.Msg> userList = new List<Model.Msg>() 
            {
                new Model.Msg(){MsgId=Guid.NewGuid().ToString()},
                new Model.Msg(){MsgId=Guid.NewGuid().ToString()}
            };
            return userList;
        }
    }
View Code
bubuko.com,布布扣

4、简单工厂类

bubuko.com,布布扣
bubuko.com,布布扣
namespace BLLFactory
{
    /// <summary>
    /// 简单工厂
    /// </summary>
    public class SimpleBLLFactory
    {
        //public IBLL.IUsers GetUserBLL()
        //{
        //    string bll = ConfigurationManager.AppSettings["BLL"].ToString();
        //    switch (bll)
        //    {
        //        case "BLLA":
        //            return new BLLA.Simple.Users();
        //        case "BLLB":
        //            return new BLLB.Simple.Users();

        //    }
        //    return null;
        //}

        public IBLL.IMsg GetMsgBLL()
        {
            string bll = ConfigurationManager.AppSettings["BLL"].ToString();
            switch (bll)
            {
                case "BLLA":
                    return new BLLA.Simple.Msg();
                case "BLLB":
                    return new BLLB.Simple.Msg();

            }
            return null;
        }
    }
}
View Code
bubuko.com,布布扣

5、客户端调用

bubuko.com,布布扣
bubuko.com,布布扣
namespace Web
{
    /// 和抽象工厂的区别:
    ///     1、简单工厂的不用业务类返回的时候,必须写单独的业务方法进行返回
    ///     2、客户端调用的时候,必须定义不用的工厂实现方法
    ///     3、如。new人员工厂实例   new消息工厂实例
    public partial class SimpleFactory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ////调用工厂接口
            ////人员信息
            //BLLFactory.SimpleBLLFactory DAL = new BLLFactory.SimpleBLLFactory();
            //List<Model.Users> listUsers = DAL.GetUserBLL().GetUserList();

            //foreach (Model.Users user in listUsers)
            //{
            //    Response.Write("UserId=[" + user.UserId + "]====UserName=[" + user.UserName + "]====AccountName=[" + user.AccountName + "]</br>");
            //}
            /*
             *调用工厂接口
             *消息信息
             */
            BLLFactory.SimpleBLLFactory Msg = new BLLFactory.SimpleBLLFactory();
            List<Model.Msg> listMsg = Msg.GetMsgBLL().GetMsgList();

            foreach (Model.Msg msg in listMsg)
            {
                Response.Write("MsgId=[" + msg.MsgId + "]</br>");
            }

        }
    }
}
View Code
bubuko.com,布布扣

 具体项目结构

bubuko.com,布布扣

简单工厂,布布扣,bubuko.com

简单工厂

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/happygx/p/3762240.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!