友情回顾
学习是一个反复的过程……
当我们要完成在某一细节层次一致的一个过程或一系列步骤,但其个别步骤在更详细的层次上的实现可能不同时,模板方法登场了。个人通俗的理解,模板方法把共同的不变行为抽出(拿出)到一个抽象类,那么子类中的重复代码大大减少,很好的体现了代码复用性。看看模板方法模式的UML图
机房收费系统中的模板模式
主要思路是建立一个空的窗体作为父窗体,而其余有具体执行内容的窗体作为子窗体,这样我们在编码过程中只需要对子窗体中不同执行过程的方法进行重写就可以了。
小编就本人就自己的习惯,展示一下我对机房收费系统的组合查询部分的理解和代码的诠释。(红框中的父类模板窗体)
'//******************************************************* '** 作者: 徐露 '** 创始时间:2014-9-1 '** 小组: '** 完成时间:2014-10-31 '** 描述:个人机房重构 '** 项目说明: 组合查询父类 '********************************************************** Imports ChargeSystem.Entity Imports ChargeSystem.BLL Public Class from1 Public eGroupQuery As New Entity.EGroupQuery Private Sub btnQuery_Click(sender As Object,e As EventArgs) Handles btnQuery.Click DGV.DataSource = Nothing '组合关系A 为空时 If cboRelationA.Text = "" Then Dim arrayControl() As Windows.Forms.Control ReDim Preserve arrayControl(2) arrayControl(0) = cboFieldA arrayControl(1) = cboOperatorA arrayControl(2) = txtContentA If PublicInfo.IsSomeEmptyText(arrayControl) Then Exit Sub End If End If '组合关系A 不为空时 If cboRelationA.Text <> "" Then Dim arrayControl() As Windows.Forms.Control ReDim Preserve arrayControl(5) arrayControl(0) = cboFieldA arrayControl(1) = cboOperatorA arrayControl(2) = txtContentA arrayControl(3) = cboFieldB arrayControl(4) = cboOperatorB arrayControl(5) = txtContentB If PublicInfo.IsSomeEmptyText(arrayControl) Then Exit Sub End If End If '组合关系B 不为空时 If cboRelationB.Text <> "" Then Dim arrayControl() As Windows.Forms.Control ReDim Preserve arrayControl(8) arrayControl(0) = cboFieldA arrayControl(1) = cboOperatorA arrayControl(2) = txtContentA arrayControl(3) = cboFieldB arrayControl(4) = cboOperatorB arrayControl(5) = txtContentB arrayControl(6) = cboFieldC arrayControl(7) = cboOperatorC arrayControl(8) = txtContentC If PublicInfo.IsSomeEmptyText(arrayControl) Then Exit Sub End If End If '定义一个实体 Dim eGroupQuery As New EGroupQuery eGroupQuery.cboFieldA = GetDBName(cboFieldA.Text.Trim) eGroupQuery.cboFieldB = GetDBName(cboFieldB.Text.Trim) eGroupQuery.cboFieldC = GetDBName(cboFieldC.Text.Trim) eGroupQuery.cboOperatorA = cboOperatorA.Text.Trim eGroupQuery.cboOperatorB = cboOperatorB.Text.Trim eGroupQuery.cboOperatorC = cboOperatorC.Text.Trim eGroupQuery.cboRelationA = GetDBName(cboRelationA.Text.Trim) eGroupQuery.cboRelationB = GetDBName(cboRelationB.Text.Trim) eGroupQuery.txtContentA = txtContentA.Text.Trim eGroupQuery.txtContentB = txtContentB.Text.Trim eGroupQuery.txtContentC = txtContentC.Text.Trim eGroupQuery.GetTable = tablename() Dim BGroupQuery As New GroupQueryBLL Dim dt As DataTable Call DView(eGroupQuery) End Sub '定义虚函数,获取不同数据库表中的字段名 Protected Overridable Function GetDBName(ByVal Str As String) As String Return "" End Function '定义虚函数,获取不同数据库表名 Protected Overridable Function tablename() As String Return "" End Function Protected Overridable Sub DView(ByVal eGroupQuery As EGroupQuery) End Sub Private Sub btnExit_Click(sender As Object,e As EventArgs) Handles btnExit.Click Me.Close() End Sub Private Sub cboRelationA_SelectedIndexChanged(sender As Object,e As EventArgs) Handles cboRelationA.SelectedIndexChanged If cboRelationA.Text <> "" Then cboFieldB.Enabled = True cboOperatorB.Enabled = True txtContentB.Enabled = True cboRelationB.Enabled = True End If End Sub Private Sub cboRelationB_SelectedIndexChanged(sender As Object,e As EventArgs) Handles cboRelationB.SelectedIndexChanged If cboRelationB.Text <> "" Then cboFieldC.Enabled = True cboOperatorC.Enabled = True txtContentC.Enabled = True End If End Sub Protected Sub from1_Load(sender As Object,e As EventArgs) Handles MyBase.Load '加载窗体控件的名称 eGroupQuery.cboFieldA = "" eGroupQuery.cboFieldB = "" eGroupQuery.cboFieldC = "" cboOperatorA.Items.Add("<") cboOperatorA.Items.Add(">") cboOperatorB.Items.Add("<") cboOperatorB.Items.Add(">") cboOperatorC.Items.Add("<") cboOperatorC.Items.Add(">") cboRelationA.Items.Add("与") cboRelationA.Items.Add("或") cboRelationB.Items.Add("与") cboRelationB.Items.Add("或") cboFieldB.Enabled = False cboOperatorB.Enabled = False txtContentB.Enabled = False cboRelationB.Enabled = False cboFieldC.Enabled = False cboOperatorC.Enabled = False txtContentC.Enabled = False End Sub Private Sub btnCancel_Click(sender As Object,e As EventArgs) Handles btnCancel.Click Call Rdim() If AllEmpty(arrayControl) Then Exit Sub End If DGV.DataSource = Nothing End Sub Private Sub Rdim() 'Dim arrayControl() As Windows.Forms.Control ReDim Preserve arrayControl(9) arrayControl(0) = New Term(txtContentA,"要查询的内容") arrayControl(1) = New Term(txtContentB,"要查询的内容") arrayControl(2) = New Term(txtContentC,"要查询的内容") arrayControl(3) = New Term(cboFieldA,"选择字段") arrayControl(4) = New Term(cboFieldB,"选择字段") arrayControl(5) = New Term(cboFieldC,"选择字段") arrayControl(6) = New Term(cboOperatorA,"选择操作符") arrayControl(7) = New Term(cboOperatorB,"选择操作符") arrayControl(8) = New Term(cboOperatorC,"选择操作符") End Sub End Class这仅仅是UI层中的代码,其他层的代码就不一一展示了,实在还有不理解者大可参考其他人士的代码。与此同理,我们的子类窗体的代码就大大减少了
多看你一眼___建造者模式
建造者模式(将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示)
题外话:在我校食堂和大神吃饭的时候,大神问:看人吃热干面,我也想吃,不知道好不好?我来了一句:那决定与拌热干面的师傅了,他心情好估计就好吃,心情不好的话…咳咳,我就吃了几次,味道不一样的。然后大神说:你说,这不是咱学过的那个设计模式来着?“是……命令?”“是……什么来这……
就这样小编再次翻开《大话设计模式》,越看越有意思,越看越和模板方法有的一拼
建造者模式同样体现着模板方法,因为Builder类中定义好了ConcreteBuilder必须要重写或要有的方法也就是说Builder中已定义好了要有的方法,这些方法可以在Builder类中实现,也可以把它推迟到子类中实现。模板方法中Abstract2类的重要函数TemplateMethod与建造者模式中的Director类中的函数Build相似,都是定义了方法的执行的骨架。(本内容最好对照《大话设计模式》课本)
它们究竟还有什么不同呢?小编认为,在实现方法的骨架层次上,建造者模式中使用的是组合的方式,而模板方法模式采用的是继承的方式,我们都知道,组合优于继承,所以我认为建造者更灵活,而且也可以避免由继承而引发的各种问题,如代码膨涨,责任过大,难以维护等。
学习是……相互关联的