
可以把抽象工厂(套餐工厂)做成一个接口,这个接口只有一个方法,就是创建抽象 产品(计费类)的工厂方法。工厂方法模式然后所有的要生产具体类(动感地带,神州行等)的工厂去实现这个接口,由具体的套餐工厂去创建具体的套餐类。于是我们再增加新 的套餐的时候,就不需要更改原有的工厂类,只需要增加此功能的计费类和相应的工厂类就可以了。 * 优点 封装了创建具体对象的工作 使得客户代码“针对接口编程”,保持对变化的“关闭” * 具体实现代码: 1、产品类 sing System.Configuration; using System.Data; using System.Data.SqlClient; using System.Data.OleDb; namespace FactoryLibrary /// /// 抽象产品基类 /// public abstract class Product abstract public DataTable GetData ; /// /// 具体产品SQL Server2005,继承自Product类 /// public class SqlServer2005 : Product string connectionString string.Empty; SqlConnection connection null; SqlCommand cmd null; SqlDataAdapter dap null; DataTable table null; /// /// 读取SQL Server /// /// public override DataTable GetData try connectionString ConfigurationManager.ConnectionStrings["sqlConn"].ConnectionString.ToString ; connection new SqlConnection connectionString ; cmd new SqlCommand "SELECT EmployeeID,LastName,FirstName,Title FROM Employees", connection ; dap new SqlDataAdapter cmd ; table new DataTable ; dap.Fill table ; return table; catch return null; finally table.Dispose ; /// /// 具体产品Access,继承自Product类 /// public class Access : Product string connectionString string.Empty; OleDbConnection connection null; OleDbCommand cmd null; OleDbDataAdapter dap null; DataTable table null; /// /// 读取Access /// /// public override DataTable GetData try connectionString ConfigurationManager.ConnectionStrings["accessConn"].ConnectionString.ToString ; connection new OleDbConnection connectionString ; cmd new OleDbCommand "SELECT ID,姓氏,名字,电子邮件地址 FROM 员工", connection ; dap new OleDbDataAdapter cmd ; table new DataTable ; dap.Fill table ; return table; catch return null; finally table.Dispose ; /// /// 具体产品Oracle,继承自Product类 /// public class Oracle : Product /// /// 读取Oracle /// /// public override DataTable GetData return null; 2、工厂类 namespace FactoryLibrary /// /// 抽象工厂类 /// public abstract class Factory /// /// 工厂方法 /// abstract public Product createDataBase ; /// /// SQL Server工厂,继承自Factory类 /// public class SqlServerFactory : Factory /// /// 返回SQL Server产品 /// /// public override Product createDataBase return new SqlServer2005 ; /// /// Access工厂,继承自Factory类 /// public class AccessFactory : Factory /// /// 返回Access产品 /// /// public override Product createDataBase return new Access ; /// /// Oracle工厂,继承自Factory类 /// public class OracleFactory : Factory /// /// 返回Oracle产品 /// /// public override Product createDataBase return new Oracle ; 3、客户端 using System; using System.Windows.Forms; using FactoryLibrary; namespace FactoryMethod public partial class mainForm : Form public mainForm InitializeComponent ; this.rbtnSQLServer.CheckedChanged + new EventHandler rbtn_CheckedChanged ; this.rbtnOracle.CheckedChanged + new EventHandler rbtn_CheckedChanged ; this.rbtnAccess.CheckedChanged + new EventHandler rbtn_CheckedChanged ; /// /// 事件调用 /// /// param name "sender" /// param name "e" void rbtn_CheckedChanged object sender, EventArgs e selectedDB RadioButton sender .Text; this.lblSelectedInfo.Text "您选择了" + selectedDB + "!"; this.gboxSelect.Enabled false; this.btnShowData.Enabled true; /// /// 切换 /// /// param name "sender" /// param name "e" private void btnSelectNewDB_Click object sender, EventArgs e if this.gboxSelect.Enabled true MessageBox.Show "您还没有选择,请选择!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk ; this.rbtnSQLServer.Checked false; this.rbtnOracle.Checked false; this.rbtnAccess.Checked false; this.gboxSelect.Enabled true; this.lblSelectedInfo.Text null; this.btnShowData.Enabled false; this.dgvShowData.DataSource null; /// /// 存放客户端所选择的 /// private string selectedDB null; /// /// 访问显示数据 /// /// param name "sender" /// param name "e" private void btnShowData_Click object sender, EventArgs e switch selectedDB case "SQL Server2005": this.dgvShowData.DataSource new SqlServerFactory .createDataBase .GetData ; break; case "Oracle": break; case "Access": this.dgvShowData.DataSource new AccessFactory .createDataBase .GetData ; break; default: break; * 有一系列的自动车产品Bus(公交车),Truck(卡车);它们需要由不同的工厂生产,但具有相同的生产流程。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-27237-1.html
最新法规发布了