典型的分层、接口和工厂的写法
微软宠物店的分析
---------页面表现层Web----------------------
Product product = new Product();
IList productsByCategory = product.GetProductsByCategory
((string)ViewState[KEY_CATEGORY]);
----------商业逻辑层---------------------
namespace PetShop.BLL
public class Product {
IProduct dal = PetShop.DALFactory.Product.Create();
return dal.GetProductsBySearch(keywords);
}
---------接口层--------------------------
namespace PetShop.IDAL{
public interface IProduct{
IList GetProductsByCategory(string category);
}
}
------------数据访问层--------------------
public class Product : IProduct{
public IList GetProductsByCategory(string category) {
......}
}
----------静态工厂方法--------------------
namespace PetShop.DALFactory {
public class Product {
public static PetShop.IDAL.IProduct Create() {
string path =
System.Configuration.ConfigurationSettings.AppSettings
["WebDAL"];
string className = path + ".Product";
return (PetShop.IDAL.IProduct)Assembly.Load
(path).CreateInstance(className);
}
}
}