解析Petshop 3.0 简单工厂(Simple Factory)模式
2006-05-01 13:34
597 查看
近日在拜读阎宏博士所著的<<Java与模式>>一书,获益非浅。微软的Petshop 3.0 相信大家研究过,现作个简单的分析,不妥之处,请指正.
一)数据库工厂层,有六个工厂类,分别是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
二) 数据库访问接口层,对应有六个接口,分别是IAccount,IInventory,IItem,IOrder,IProduct和IProfile
其中IOrder接口(抽象产品角色)部分代码如下:
public interface IOrder
{
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图如下
一)数据库工厂层,有六个工厂类,分别是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
二) 数据库访问接口层,对应有六个接口,分别是IAccount,IInventory,IItem,IOrder,IProduct和IProfile
其中IOrder接口(抽象产品角色)部分代码如下:
public interface IOrder
{
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图如下
相关文章推荐
- 解析Petshop 3.0 简单工厂(Simple Factory)模式
- 简单工厂( Simple Factory )模式
- 简单工厂(Simple Factory)模式
- Java设计模式——简单工厂(Simple Factory)
- Simple Factory 简单工厂模式
- 简单工厂模式 Simple Factory【转】
- 简单工厂(Simple Factory)模式
- 简单工厂模式-Simple Factory Pattern
- 设计模式之简单工厂(Simple Factory)
- 简单工厂(Simple Factory)模式
- 【设计模式】学习笔记4:简单工厂模式(Simple Factory)
- 设计模式学习笔记三:简单工厂(Simple Factory)
- 简单工厂模式(SimpleFactory)
- 简单工厂模式-Simple Factory Pattern 工厂三兄弟之简单工厂模式(二):简单工厂模式概述
- 简单工厂模式 Simple Factory Pattern
- 设计模式 一 简单工厂Simple Factory(工厂模式)
- 设计模式之SimpleFactory(简单工厂)
- 简单工厂模式(Simple Factory Pattern)
- 工厂模式之一 - 简单工厂(Simple Factory)
- 设计模式案例--工厂模式--简单工厂(静态工厂)(simplefactory)