简单工厂模式

in Develop



项目AccessDAL 在ACCESS数据库下的数据访问层,由DALInterface接口派生

public class AccessDALDemo : DALInterface
    {
       ………………..
    }

项目SdlDAL 在MS Sqlserver下的数据访问层(笔误中把sqlDAL写成了SdlDAL,我太搓了!-_#)

public class SqlDALDemo : DALInterface
    {
…………….
    }

项目IDAL 接口项目
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace IDAL
{
    public interface DALInterface
    {
        DataView GetDataList();
    }
}

项目 DALFactory 工厂项目,工厂模式的核心
 public static IDAL.DALInterface Create(int i)
        {
            if (i == 1)
                return new SqlDAL.SqlDALDemo();
            else
                return new AccessDAL.AccessDALDemo();
        }
Create方法将返回一个DALInterface接口
Site表示层
.cs
 void bindGV(int i)
    {
        IDAL.DALInterface dal = DALFactory.Factory.Create(i);
        DataView dv = dal.GetDataList();
        this.GridView1.DataSource = dv;
        this.GridView1.DataBind();
    }
根据变量i的值创建合适的类,从而实现简单工厂模式

2 Comments

2 Comments

  1. 一头雾水.看不懂.

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>