010Visual Basic二谈建造者模式
程序员文章站
2024-01-19 08:13:40
...
下面的示例是一个控制台程序
1.建造者
‘父类
Public MustInherit Class Builder
Public MustOverride Sub BuildPartA()
Public MustOverride Sub BuildPartB()
Public MustOverride Function GetResult() As product
End Class
’子一
Public Class ConcreteBuilder1
Inherits Builder
Private myProduct As Product = New Product
Public Overrides Sub BuildPartA()
myProduct.add("部件A")
End Sub
Public Overrides Sub BuildPartB()
myProduct.add("部件B")
End Sub
Public Overrides Function GetResult() As Product
Return myProduct
End Function
End Class
‘子二
Public Class ConcreteBuilder2
Inherits Builder
Private myProduct As Product = New Product
Public Overrides Sub BuildPartA()
myProduct.add("部件X")
End Sub
Public Overrides Sub BuildPartB()
myProduct.add("部件Y")
End Sub
Public Overrides Function GetResult() As Product
Return myProduct
End Function
End Class
2.产品类
Public Class Product
Private parts As IList(Of String) = New List(Of String)
Public Sub add(ByVal part As String)
parts.Add(part)
End Sub
Public Sub show()
Console.WriteLine("产品创建------------")
For Each part In parts
Console.WriteLine(part)
Next
End Sub
End Class
3.指挥者
Public Class Director
Public Sub New(ByVal b As Builder)
b.BuildPartA()
b.BuildPartB()
End Sub
End Class
4.客户端
Module Module1
Sub Main()
Dim b1 As Builder = New ConcreteBuilder1
Dim b2 As Builder = New ConcreteBuilder2
Dim myDirector As Director = New Director(b1)
Dim p1 As Product = b1.GetResult
p1.show()
myDirector = New Director(b2)
Dim p2 As Product = b2.GetResult
p2.show()
Console.Read()
End Sub
End Module
上一篇: 010.day010