注册 登录
编程论坛 VB.NET论坛

Builder 建造模式

ioriliao 发布于 2008-02-18 16:10, 958 次点击
程序代码:

'Builder模式
Module Module1

    Sub Main()
        System.Console.WriteLine("中国式房子:")
        HouseModule.CreateHouse(New ChinaHouseBuiler)
        System.Console.WriteLine()
        System.Console.WriteLine()
        System.Console.WriteLine("美国式房子:")
        HouseModule.CreateHouse(New USAHouseBuiler)
        System.Console.ReadLine()

    End Sub

End Module



Public Interface Builder
    Sub BuilDoor()   '门
    Sub BuilWall()   '墙
    Sub BuilWindow() '窗口
    Sub BuilFloor()  '地板
    Sub BuilHouseCeiling() '屋顶
End Interface

'不稳定部分
Public Class ChinaHouseBuiler : Implements Builder  '中国风格的房子

    Public Sub BuilDoor() Implements Builder.BuilDoor
        System.Console.WriteLine("中国门")
    End Sub

    Public Sub BuilFloor() Implements Builder.BuilFloor
        System.Console.WriteLine("中国地板")
    End Sub

    Public Sub BuilHouseCeiling() Implements Builder.BuilHouseCeiling
        System.Console.WriteLine("中国屋顶")
    End Sub

    Public Sub BuilWall() Implements Builder.BuilWall
        System.Console.WriteLine("中国墙")
    End Sub

    Public Sub BuilWindow() Implements Builder.BuilWindow
        System.Console.WriteLine("中国窗")
    End Sub

   
End Class

Public Class USAHouseBuiler : Implements Builder  '美国风格的房子

    Public Sub BuilDoor() Implements Builder.BuilDoor
        System.Console.WriteLine("美国门")
    End Sub

    Public Sub BuilFloor() Implements Builder.BuilFloor
        System.Console.WriteLine("美国地板")
    End Sub

    Public Sub BuilHouseCeiling() Implements Builder.BuilHouseCeiling
        System.Console.WriteLine("美国屋顶")
    End Sub

    Public Sub BuilWall() Implements Builder.BuilWall
        System.Console.WriteLine("美国墙")
    End Sub

    Public Sub BuilWindow() Implements Builder.BuilWindow
        System.Console.WriteLine("美国窗")
    End Sub


End Class
''''还可以继续添加其它风格的房子
'''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''



'稳定部分
Public Class HouseModule
    '假设一间房子有四个墙,一个地板,两扇窗,一个屋顶,一个门
    Public Shared Sub CreateHouse(ByVal HouseBuilder As Builder)
        HouseBuilder.BuilWall()
        HouseBuilder.BuilWall()
        HouseBuilder.BuilWall()
        HouseBuilder.BuilWall()

        HouseBuilder.BuilFloor()

        HouseBuilder.BuilWindow()
        HouseBuilder.BuilWindow()

        HouseBuilder.BuilHouseCeiling()

        HouseBuilder.BuilDoor()



    End Sub
End Class
0 回复
1