日期:2014-05-18  浏览次数:20520 次

麻烦高手解释一段代码,谢谢!~~`
C# code

namespace SamTest.DALFactory
{
    /// <summary>
    /// This class is implemented following the Abstract Factory pattern to create the DAL implementation
    /// specified from the configuration file
    /// </summary>
    public sealed class DataAccess
    {
        private static readonly string path = ConfigurationManager.AppSettings["WebDAL"];
        public DataAccess() { }

        public static SamTest.IDAL.IBuildingSell CreateBuildingSell()
        {
            
            string CacheKey = path + ".BuildingSell";    //此处返回的就是SamTest.BLL.BuildingSell
            System.Type type = Type.GetType(CacheKey);
            return (SamTest.IDAL.IBuildingSell)Activator.CreateInstance(type);     //此处标记为 A1
        }
     }
}


namespace SamTest.BLL
{
    /// <summary>
    /// 业务逻辑类BuildingSell 的摘要说明。
    /// </summary>
    public class BuildingSell
    {
        private static readonly IBuildingSell dal = DataAccess.CreateBuildingSell();     //此处标记为 A2
        public BuildingSell()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int BU_ID)
        {
            return dal.Exists(BU_ID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(SamTest.Model.BuildingSell model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(SamTest.Model.BuildingSell model)
        {
            dal.Update(model);
        }
}



namespace SamTest.IDAL
{
    /// <summary>
    /// 接口层IBuildingSell 的摘要说明。
    /// </summary>
    public interface IBuildingSell
    {
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        bool Exists(int BU_ID);
        /// <summary>
        /// 增加一条数据
        /// </summary>
        void Add(SamTest.Model.BuildingSell model);
        /// <summary>
        /// 更新一条数据
        /// </summary>
        void Update(SamTest.Model.BuildingSell model);
        /// <summary>
        /// 删除一条数据
        /// </summary>
        void Delete(int BU_ID);
        #endregion  成员方法
    }
}



public partial class Member_Member_SellFlats_UpdateBuild : System.Web.UI.Page
{
    private void UpdateInfo()
    {
       SamTest.BLL.BuildingSell BLLobj = new SamTest.BLL.BuildingSell();
       BLLobj.Update(Modelobj);
    }
}




A1返回的是一个接口类型,然后A2处的代码让感觉像是实例化了一个接口对象,不知道这里怎么理解,接口是不能实例化的,不知道这里这么写是为什么,麻烦知道的朋友帮忙解释一下,谢谢!不胜感激!~

------解决方案--------------------
是返回一个对象,该对象实现了这个接口.
------解决方案--------------------
IBuildingSell dal = DataAccess.CreateBuildingSell

并不是表示实例化IBuildingSell 接口,而是实例化一个实现了IBuildingSell 接口的对象.