标签:设计模式
话说机房收费系统项目中,有三个窗体的顶层逻辑是一样的。这些逻辑就是:先查询卡号是不是存在,如果存在就把数据显示出来。
定义一个操作中算法的骨架,而将一些步骤延迟到子类中。模版方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定的步骤。
Imports System.Data.SqlClient Public Class DAL1 Public Function SelectUserByID(UserID As String) As DataTable Dim helper As New SQLHelper Dim str = "select * from Table1 where id=@id" Dim sqlparam() As SqlParameter = {New SqlParameter("@id", UserID)} Return helper.ExecuteQuery(str, CommandType.Text, sqlparam) End Function End Class Public Class DAL2 Public Function SelectUserByID(UserID As String) As DataTable Dim helper As New SQLHelper Dim str = "select * from Table2 where id=@id" Dim sqlparam() As SqlParameter = {New SqlParameter("@id", UserID)} Return helper.ExecuteQuery(str, CommandType.Text, sqlparam) End Function End Class Public Class DAL3 Public Function SelectUserByID(UserID As String) As DataTable Dim helper As New SQLHelper Dim str = "select * from Table3 where id=@id" Dim sqlparam() As SqlParameter = {New SqlParameter("@id", UserID)} Return helper.ExecuteQuery(str, CommandType.Text, sqlparam) End Function End Class
Public MustInherit Class AbstractClass Public MustOverride Sub CheckCardID(CardID As String) Public MustOverride Function GetCardInfo(CardID As String) As DataTable Public Function ExecuteCommand(CardID As String) As DataTable CheckCardID(CardID) Return GetCardInfo(CardID) End Function End Class Public Class ConcreteClass1 : Inherits AbstractClass Public Overrides Sub CheckCardID(CardID As String) Dim DAL As New DAL1 Dim mydatatable As DataTable mydatatable = DAL.SelectUserByID(CardID) If mydatatable.Rows.Count = 0 Then Throw New Exception("没有数据!") End If End Sub Public Overrides Function GetCardInfo(CardID As String) As DataTable Dim DAL As New DAL1 Return DAL.SelectUserByID(CardID) End Function End Class Public Class ConcreteClass2 : Inherits AbstractClass Public Overrides Sub CheckCardID(CardID As String) Dim DAL As New DAL2 Dim mydatatable As DataTable mydatatable = DAL.SelectUserByID(CardID) If mydatatable.Rows.Count = 0 Then Throw New Exception("没有数据!") End If End Sub Public Overrides Function GetCardInfo(CardID As String) As DataTable Dim DAL As New DAL2 Return DAL.SelectUserByID(CardID) End Function End Class Public Class ConcreteClass3 : Inherits AbstractClass Public Overrides Sub CheckCardID(CardID As String) Dim DAL As New DAL3 Dim mydatatable As DataTable mydatatable = DAL.SelectUserByID(CardID) If mydatatable.Rows.Count = 0 Then Throw New Exception("没有数据!") End If End Sub Public Overrides Function GetCardInfo(CardID As String) As DataTable Dim DAL As New DAL3 Return DAL.SelectUserByID(CardID) End Function End Class
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim myBll As New ConcreteClass1 Try DataGridView1.DataSource = myBll.ExecuteCommand(TextBox1.Text) Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Form2.Show() Form3.Show() End Sub End Class
Public Class Form2 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim myBll As New ConcreteClass2 Try DataGridView1.DataSource = myBll.ExecuteCommand(TextBox1.Text) Catch ex As Exception MsgBox(ex.Message) End Try End Sub End Class
Public Class Form3 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim myBll As New ConcreteClass3 Try DataGridView1.DataSource = myBll.ExecuteCommand(TextBox1.Text) Catch ex As Exception MsgBox(ex.Message) End Try End Sub End Class
标签:设计模式
原文地址:http://blog.csdn.net/cxl0921/article/details/45841373