工厂模式

这里写自定义目录标题

代码

GetList类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
namespace Ce
{
    public class GetList
    {
        public DataSet Query(string ss)
        {
            string connectionString = "Data Source=.;Initial Catalog=ce1;User ID=sa;Password=123456";

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    string SQLString = "select * from "+ss;
                    SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }

                return ds;
            }
        }

        public virtual DataSet Doit()
        {
            DataSet ds = new DataSet();
            return ds;
        }
    }
}

Users类

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Ce
{
    public class Users:GetList
    {
        public override DataSet Doit()
        {
            return Query("Uers");
        }
    }
}

Customer类

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Ce
{
    public class Customer:GetList
    {
        public override DataSet Doit()
        {
            return Query("Customer");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Ce
{
    public interface IFactory
    {

        GetList Create();


    }

    public class UsersFactory : IFactory
    {
        public GetList Create()
        {
            return new DataOperate();
        }
    }
    public class CustomerFactory : IFactory
    {
        public GetList Create()
        {
            return new DataOperateCustomer();
        }
    }
}

web页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_201719164205_牛园.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>
        <asp:GridView ID="GridView2" runat="server">
        </asp:GridView>
    </form>
</body>
</html>

web页面后端代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace Ce
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               IFactory factory=new CustomerFactory();
               GetList getlist = factory.Create();
                DataSet ds = getlist.Doit();
                GridView1.DataSource = ds;
                GridView1.DataBind();

                IFactory factory1=new UsersFactory();
                GetList getlist1 = factory1.Create();
                DataSet ds1 = getlist1.Doit();
                GridView2.DataSource = ds1;
                GridView2.DataBind();
            }
        }
    }
}

Customer表
Customer表
Users表
Users表

大概的思路

GetList类是一个公共的类,可以新建一个类,这个类继承GetList类,并重写Doit方法,实现自己的查询。

IFactory是一个接口,在接口里限制条件,让凡是继承他的子工厂都要满足这个条件。
而子工厂,通过调用子类来进行查询所有的信息。

在使用的时候只需要实例化子工厂就可以了。


语言表达能力不是太好,请见谅。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值