近日在拜读阎宏博士所著的<<Java与模式>>一书,获益非浅。微软的Petshop 3.0 相信大家研究过,现作个简单的分析,不妥之处,请指正.
一)数据库工厂层,有六个工厂类,分别是Account类,Inventory类,Item类,Order类,Product类和Profile类
其中Order类(工厂类)的部分代码如下:
![]() public class Order {
public class Order {
![]()
![]() //public static PetShop.IDAL.IOrder GetOrder() {
        //public static PetShop.IDAL.IOrder GetOrder() {
![]() public static PetShop.IDAL.IOrder Create() {
        public static PetShop.IDAL.IOrder Create() {
![]()
![]() /// Look up the DAL implementation we should be using
            /// Look up the DAL implementation we should be using
![]() string path = System.Configuration.ConfigurationSettings.AppSettings["OrdersDAL"];
            string path = System.Configuration.ConfigurationSettings.AppSettings["OrdersDAL"];
![]() string className = path + ".Order";
            string className = path + ".Order";
![]()
![]() // Using the evidence given in the config file load the appropriate assembly and class
            // Using the evidence given in the config file load the appropriate assembly and class
![]() return (PetShop.IDAL.IOrder)Assembly.Load(path).CreateInstance(className);
            return (PetShop.IDAL.IOrder)Assembly.Load(path).CreateInstance(className);    
![]() }
        }
![]() }
工厂类Order的静态工厂方法Create(),返回Petshop.IDAL下的接口IOrder
    }
工厂类Order的静态工厂方法Create(),返回Petshop.IDAL下的接口IOrder
二) 数据库访问接口层,对应有六个接口,分别是IAccount,IInventory,IItem,IOrder,IProduct和IProfile
其中IOrder接口(抽象产品角色)部分代码如下:
三)SqlServerDAL和OracleDAL数据访问层下,均有六个充当具体产品角色的类,他们实现抽象产品角色,即(相应的接口)
其中SqlServerDAL下的Order类(具体产品角色)部分代码如下:
  
![]() namespace PetShop.SQLServerDAL {
namespace PetShop.SQLServerDAL {
![]()
![]() public class Order : IOrder{
public class Order : IOrder{
![]() public int Insert(OrderInfo order) {
public int Insert(OrderInfo order) {
![]()
![]() int orderId = 0;
            int orderId = 0;
![]() String strSQL = null;
            String strSQL = null;
![]() try{
            try{
![]() 
            
![]() SqlParameter[] orderParms = GetOrderParameters();
                SqlParameter[] orderParms = GetOrderParameters();
![]() SqlParameter statusParm = new SqlParameter(PARM_ORDER_ID, SqlDbType.Int);
                SqlParameter statusParm = new SqlParameter(PARM_ORDER_ID, SqlDbType.Int);
![]() SqlCommand cmd = new SqlCommand();
                SqlCommand cmd = new SqlCommand();
![]() orderParms[0].Value = order.UserId;
                                orderParms[0].Value = order.UserId;
![]() orderParms[1].Value = order.Date;
                orderParms[1].Value = order.Date;
![]() 
                
![]() 
                ![]()
![]()
![]()
![]() ..
..
![]() 
                
![]() cmd.Parameters.Clear();
                    cmd.Parameters.Clear();
![]() }
                }
![]()
![]() }catch(Exception e){
            }catch(Exception e){
![]() throw e;
                throw e;
![]() }finally{
            }finally{                
![]() }
            }    
![]() return orderId;
            return orderId;
![]() }
        }
![]()
![]() 
    
![]() public OrderInfo GetOrder(int orderId) {
        public OrderInfo GetOrder(int orderId) {
![]() 
            
![]() 
            
![]() SqlParameter parm = new SqlParameter(PARM_ORDER_ID, SqlDbType.Int);
            SqlParameter parm = new SqlParameter(PARM_ORDER_ID, SqlDbType.Int);
![]() parm.Value = orderId;
            parm.Value = orderId;
![]()
![]() using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_DTC_ORDERS, CommandType.Text, SQL_SELECT_ORDER, parm)) {
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_DTC_ORDERS, CommandType.Text, SQL_SELECT_ORDER, parm)) {
![]() 
                
![]() 
                ![]()
![]()
![]()
![]()
![]()
![]() return null;
            return null;
![]() }
        }
![]() }
}
![]() }
}
![]() 
        
![]()
![]() 四) 相应的UML图如下
四) 相应的UML图如下
![]()
一)数据库工厂层,有六个工厂类,分别是Account类,Inventory类,Item类,Order类,Product类和Profile类
其中Order类(工厂类)的部分代码如下:
 public class Order {
public class Order {
 //public static PetShop.IDAL.IOrder GetOrder() {
        //public static PetShop.IDAL.IOrder GetOrder() { public static PetShop.IDAL.IOrder Create() {
        public static PetShop.IDAL.IOrder Create() {
 /// Look up the DAL implementation we should be using
            /// Look up the DAL implementation we should be using string path = System.Configuration.ConfigurationSettings.AppSettings["OrdersDAL"];
            string path = System.Configuration.ConfigurationSettings.AppSettings["OrdersDAL"]; string className = path + ".Order";
            string className = path + ".Order";
 // Using the evidence given in the config file load the appropriate assembly and class
            // Using the evidence given in the config file load the appropriate assembly and class return (PetShop.IDAL.IOrder)Assembly.Load(path).CreateInstance(className);
            return (PetShop.IDAL.IOrder)Assembly.Load(path).CreateInstance(className);     }
        } }
    }二) 数据库访问接口层,对应有六个接口,分别是IAccount,IInventory,IItem,IOrder,IProduct和IProfile
其中IOrder接口(抽象产品角色)部分代码如下:
public interface IOrder 
{
int Insert(OrderInfo order);
OrderInfo GetOrder(int orderId);
}
{
int Insert(OrderInfo order);
OrderInfo GetOrder(int orderId);
}
三)SqlServerDAL和OracleDAL数据访问层下,均有六个充当具体产品角色的类,他们实现抽象产品角色,即(相应的接口)
其中SqlServerDAL下的Order类(具体产品角色)部分代码如下:
 namespace PetShop.SQLServerDAL {
namespace PetShop.SQLServerDAL {
 public class Order : IOrder{
public class Order : IOrder{ public int Insert(OrderInfo order) {
public int Insert(OrderInfo order) {
 int orderId = 0;
            int orderId = 0; String strSQL = null;
            String strSQL = null; try{
            try{ 
             SqlParameter[] orderParms = GetOrderParameters();
                SqlParameter[] orderParms = GetOrderParameters(); SqlParameter statusParm = new SqlParameter(PARM_ORDER_ID, SqlDbType.Int);
                SqlParameter statusParm = new SqlParameter(PARM_ORDER_ID, SqlDbType.Int); SqlCommand cmd = new SqlCommand();
                SqlCommand cmd = new SqlCommand(); orderParms[0].Value = order.UserId;
                                orderParms[0].Value = order.UserId; orderParms[1].Value = order.Date;
                orderParms[1].Value = order.Date; 
                 
                


 ..
.. 
                 cmd.Parameters.Clear();
                    cmd.Parameters.Clear(); }
                }
 }catch(Exception e){
            }catch(Exception e){ throw e;
                throw e; }finally{
            }finally{                 }
            }     return orderId;
            return orderId; }
        }
 
     public OrderInfo GetOrder(int orderId) {
        public OrderInfo GetOrder(int orderId) { 
             
             SqlParameter parm = new SqlParameter(PARM_ORDER_ID, SqlDbType.Int);
            SqlParameter parm = new SqlParameter(PARM_ORDER_ID, SqlDbType.Int); parm.Value = orderId;
            parm.Value = orderId;
 using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_DTC_ORDERS, CommandType.Text, SQL_SELECT_ORDER, parm)) {
            using (SqlDataReader rdr = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_DTC_ORDERS, CommandType.Text, SQL_SELECT_ORDER, parm)) { 
                 
                




 return null;
            return null; }
        } }
} }
} 
        


 
                    
                     
                    
                 
                    
                 
 
        

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号