首页 > 代码库 > 简单工厂

简单工厂

1、消息产品接口

namespace IBLL
{
    public interface IMsg
    {
        List<Model.Msg> GetMsgList();
    }
}
View Code

2、消息A实现

   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

3、消息B实现

   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

4、简单工厂类

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

5、客户端调用

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

 具体项目结构