码迷,mamicode.com
首页 > 其他好文 > 详细

模版方法

时间:2015-05-19 13:15:27      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:设计模式

问题背景

话说机房收费系统项目中,有三个窗体的顶层逻辑是一样的。这些逻辑就是:先查询卡号是不是存在,如果存在就把数据显示出来。

模版方法

定义一个操作中算法的骨架,而将一些步骤延迟到子类中。模版方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定的步骤。

Demo

数据库结构

技术分享

技术分享技术分享技术分享

程序结构

技术分享

技术分享

技术分享

技术分享

代码

DAL

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

BLL

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

Form1

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

Form2

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

Form3

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!