zoukankan      html  css  js  c++  java
  • 工厂方法模式切换数据库 echo

    工厂方法(FactoryMethod),我也就一句话了:具体的子工厂生产具体的产品,解决了OO的“开放-封闭”原则。

      一、先看图:

      二、具体实现代码:

            1、产品类

    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Data.OleDb;
    
    namespace FactoryLibrary
    {
        /// <summary>
        /// 抽象产品基类
        /// </summary>
        public abstract class Product
        {
            abstract public DataTable GetData();
        }
        /// <summary>
        /// 具体产品SQL Server2005,继承自Product类
        /// </summary>
        public class SqlServer2005 : Product
        {
            string connectionString = string.Empty;
            SqlConnection connection = null;
            SqlCommand cmd = null;
            SqlDataAdapter dap = null;
            DataTable table = null;
            /// <summary>
            /// 读取SQL Server数据库
            /// </summary>
            /// <returns></returns>
            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();
                }
            }
        }
        /// <summary>
        /// 具体产品Access,继承自Product类
        /// </summary>
        public class Access : Product
        {
            string connectionString = string.Empty;
            OleDbConnection connection = null;
            OleDbCommand cmd = null;
            OleDbDataAdapter dap = null;
            DataTable table = null;
            /// <summary>
            /// 读取Access数据库
            /// </summary>
            /// <returns></returns>
            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();
                }
            }
        }
        /// <summary>
        /// 具体产品Oracle,继承自Product类
        /// </summary>
        public class Oracle : Product
        {
            /// <summary>
            /// 读取Oracle数据库
            /// </summary>
            /// <returns></returns>
            public override DataTable GetData() { return null; }
        }
    }

      2、工厂类

     

    namespace FactoryLibrary
    {
        /// <summary>
        /// 抽象工厂类
        /// </summary>
        public abstract class Factory
        {
            /// <summary>
            /// 工厂方法
            /// </summary>
            abstract public Product createDataBase();
        }
        /// <summary>
        /// SQL Server工厂,继承自Factory类
        /// </summary>
        public class SqlServerFactory : Factory
        {
            /// <summary>
            /// 返回SQL Server产品
            /// </summary>
            /// <returns></returns>
            public override Product createDataBase()
            {
                return new SqlServer2005();
            }
        }
        /// <summary>
        /// Access工厂,继承自Factory类
        /// </summary>
        public class AccessFactory : Factory
        {
            /// <summary>
            /// 返回Access产品
            /// </summary>
            /// <returns></returns>
            public override Product createDataBase()
            {
                return new Access();
            }
        }
        /// <summary>
        /// Oracle工厂,继承自Factory类
        /// </summary>
        public class OracleFactory : Factory
        {
            /// <summary>
            /// 返回Oracle产品
            /// </summary>
            /// <returns></returns>
            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);
            }
            /// <summary>
            /// 事件调用
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void rbtn_CheckedChanged(object sender, EventArgs e)
            {
                selectedDB = ((RadioButton)sender).Text;
                this.lblSelectedInfo.Text = "您选择了" + selectedDB + "数据库!";
                this.gboxSelect.Enabled = false;
                this.btnShowData.Enabled = true;
            }
    
            /// <summary>
            /// 切换数据库
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            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;
            }
            /// <summary>
            /// 存放客户端所选择的数据库
            /// </summary>
            private string selectedDB = null;
            /// <summary>
            /// 访问数据库显示数据
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            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;
                }
            }
        }
    }

     三、没了。

  • 相关阅读:
    HTTP断点续传 规格严格
    Java Shutdown 规格严格
    linux 命令源码 规格严格
    JTable调整列宽 规格严格
    linux 多CPU 规格严格
    Hello can not find git path 规格严格
    Kill 规格严格
    拜拜牛人 规格严格
    Swing 规格严格
    Debugging hangs in JVM (on AIX but methodology applicable to other platforms) 规格严格
  • 原文地址:https://www.cnblogs.com/mangonic/p/1784014.html
Copyright © 2011-2022 走看看