1.前提
1.模板方法模式
定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
2.组合方法
之前写过一篇博客,讲述实现任意组合查询。中心思想:将所有的查询都看做一种情况来处理(http://blog.csdn.net/lypf19900912/article/details/8959245)
3.三层架构
用户界面表示层(UL);业务逻辑层(BLL);数据访问层(DAL)
2.目的
利用模板方法来实现组合查询
3.分析
组合查询在机房收费系统中出现了很多次。他们的界面如图:
实现组合查询的过程也很类似,他们对条件进行组合然后进行查询。细分一下他们实现的相同点和不同点
相同点:选择的条件 拼接成的字符串;不同点:字段的具体内容
那么对于这个组合查询来说实现的步骤是不变的(选择条件然后拼接字符串),但是实现某些步骤的具体实现是不同的(字段的具体内容不同)。
4.实现
在BLL层建立一个抽象类和他的子类。
抽象类中有一个虚方法:选择的字段(不同点)
四个实方法:选择的操作符,选择的内容,选择的组合关系,拼接字符串
Imports CooperateJF.Entity Imports CooperateJF.Factory Imports CooperateJF.IDAL Namespace BLL ''' <summary> ''' 建立组合查询的抽象类 ''' </summary> Public MustInherit Class CombinCheckBLL ''' <summary> ''' 组合方法 ''' 完成字段的拼接 ''' </summary> ''' <param name="enGroupOne">第一条组合 ''' 传递三条字段信息</param> Public Function Combin(ByVal enGroupOne As GroupEntity, ByVal enGrouptwo As GroupEntity, ByVal enGroupthree As GroupEntity, ByVal enGroupfour As GroupEntity) As String '拼接字符串 Dim strCombin As String strCombin = enGroupOne.FileNameOne + enGrouptwo.OperatorOne + enGroupthree.ContextOne + " " + enGroupfour.RelationOne + " " + enGroupOne.FileNameTwo + enGrouptwo.OperatorTwo + enGroupthree.ContextTwo + " " + enGroupfour.RelationTwo + " " + enGroupOne.FileNameThree + enGrouptwo.OperatorThree + enGroupthree.ContextThree Return strCombin End Function ''' <summary> ''' 将三个操作符都选择出来 ''' 这里需要注意的是:不是每次都选择三个,所以需要在方法中为操作符选择默认值(字段如果没有被选择则默认是=) ''' </summary> ''' <param name="filethree">第三个选择的操作符</param> ''' <param name="filetwo">第二个选择的操作符</param> ''' <param name="fileone">第一个选择的操作符</param> Public MustOverride Function SelectFile(ByVal filethree As String, ByVal filetwo As String, ByVal fileone As String) As GroupEntity Public Function SelectOeprator(ByVal Oepratorthree As String, ByVal Oepratortwo As String, ByVal Oepratorone As String) As GroupEntity '定义实体类 Dim enGroup As New Entity.GroupEntity '第一个选择操作符 Select Case Oepratorone '如果没有选择那么设置默认值是“=” Case "" enGroup.OperatorOne = "=" Case Else enGroup.OperatorOne = Oepratorone End Select '第二个选择的操作符 Select Case Oepratortwo Case "" enGroup.OperatorTwo = "=" Case Else enGroup.OperatorTwo = Oepratortwo End Select '第三个选择的操作符 Select Case Oepratorthree Case "" enGroup.OperatorThree = "=" Case Else enGroup.OperatorThree = Oepratorthree End Select Return enGroup End Function ''' <summary> ''' 将三个操作符都选择出来 ''' 这里需要注意的是:不是每次都选择三个,所以需要在方法中为内容选择默认值(内容如果没有被选择则默认是1) ''' </summary> ''' <param name="Contextthree">第三个选择的内容</param> ''' <param name="Contexttwo">第二个选择的内容</param> ''' <param name="Contextone">第一个选择的内容</param> Public Function SelectContext(ByVal Contextthree As String, ByVal Contexttwo As String, ByVal Contextone As String) As GroupEntity '定义实体类 Dim enGroup As New Entity.GroupEntity '第二个选择的内容 Select Case Contextone Case "" enGroup.ContextOne = " 1 " Case Else enGroup.ContextOne = Contextone End Select '第二个选择的内容 Select Case Contexttwo Case "" enGroup.ContextTwo = "1 " Case Else enGroup.ContextTwo = Contexttwo End Select '第三个选择的内容 Select Case Contextthree Case "" enGroup.ContextThree = "1" Case Else enGroup.ContextThree = Contextthree End Select Return enGroup End Function ''' <summary> ''' 选择组合;如果没有选择默认是and ''' </summary> ''' <param name="relationtwo">选择的第二种组合</param> ''' <param name="relationone">选择的第一种组合</param> Public Function SelectRelation(ByVal relationtwo As String, ByVal relationone As String) As GroupEntity '定义实体类 Dim enGroup As New Entity.GroupEntity '选择的第二个组合 Select Case relationtwo Case "" enGroup.RelationTwo = "AND" Case "与" enGroup.RelationTwo = "AND" Case "或" enGroup.RelationTwo = "or" End Select '选择的第一个组合 Select Case relationone Case "" enGroup.RelationOne = "AND" Case "与" enGroup.RelationOne = "AND" Case "或" enGroup.RelationOne = "OR" End Select Return enGroup End Function End Class End Namespace
子类来实现两个功能的选择字段方法
Imports CooperateJF.Entity Imports CooperateJF.Factory Imports CooperateJF.IDAL Namespace BLL ''' <summary> ''' 字段子类 ''' </summary> Public Class FileBLL Inherits BLL.CombinCheckBLL ''' <summary> ''' 字段选择(默认为1) ''' </summary> ''' <param name="filethree">选择第三个字段</param> ''' <param name="filetwo">选择第二个字段</param> ''' <param name="fileone">选择第一个字段</param> Public Overrides Function SelectFile(ByVal filethree As String, ByVal filetwo As String, ByVal fileone As String) As GroupEntity '定义实体类 Dim enGroup As New Entity.GroupEntity '选择第一个字段 Select Case fileone Case "" enGroup.FileNameOne = 1 Case "学号" enGroup.FileNameOne = "StuID" Case "性别" enGroup.FileNameOne = "StuSex" Case "卡号" enGroup.FileNameOne = "CardID" Case "余额" enGroup.FileNameOne = "Balance" Case "系别" enGroup.FileNameOne = "StuDepartment" Case "专业" enGroup.FileNameOne = "StuMajor" Case "班级" enGroup.FileNameOne = "StuClass" End Select '选择第二个字段 Select Case filetwo Case "" enGroup.FileNameTwo = " 1" Case "学号" enGroup.FileNameTwo = " StuID" Case "性别" enGroup.FileNameTwo = " StuSex" Case "卡号" enGroup.FileNameTwo = " CardID" Case "余额" enGroup.FileNameTwo = " Balance" Case "系别" enGroup.FileNameTwo = " StuDepartment" Case "专业" enGroup.FileNameTwo = " StuMajor" Case "班级" enGroup.FileNameTwo = " StuClass" End Select '选择第三个字段 Select Case filethree Case "" enGroup.FileNameThree = " 1" Case "学号" enGroup.FileNameThree = " StuID" Case "性别" enGroup.FileNameThree = " StuSex" Case "卡号" enGroup.FileNameThree = " CardID" Case "余额" enGroup.FileNameThree = " Balance" Case "系别" enGroup.FileNameThree = " StuDepartment" Case "专业" enGroup.FileNameThree = " StuMajor" Case "班级" enGroup.FileNameThree = " StuClass" End Select Return enGroup End Function End Class End Namespace
在UI层的调用(主要的部分)
''' <summary> ''' 确定查看学生基本信息 ''' </summary> Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click '设置选择的内容 Dim enGroupOne As New GroupEntity Dim enGroupTwo As New GroupEntity Dim enGroupThree As New GroupEntity Dim enGroupFour As New GroupEntity Dim file As New FileBLL '选择字段 enGroupOne = file.SelectFile(cmbFileName3.Text, cmbFileName2.Text, cmbFileName1.Text) '选择操作符 enGroupTwo = file.SelectOeprator(cmbOperate3.Text, cmbOperate2.Text, cmbOperate1.Text) '添加的内容 enGroupThree = file.SelectContext(txtFileName3.Text, txtFileName2.Text, txtFileName1.Text) '选择的组合关系 enGroupFour = file.SelectRelation(cmbRelation1.Text, cmbRelation2.Text) '拼接字段 Dim strCombin As String strCombin = file.Combin(enGroupOne, enGroupTwo, enGroupThree, enGroupFour) End Sub
模板方法模式是通过把不变的行为搬移到超类,去除子类中的重复代码来体现它的优势的。也就提供了一个很好的代码复用平台。