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