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

简单工厂模式

ioriliao 发布于 2008-02-17 13:22, 1416 次点击
程序代码:

'简单工厂模式
Module Module1

    Sub Main()
        Dim MyCar As ICar = CarFactory.GetCarInterface("Benz")
        MyCar.CarRun()
        MyCar.CarStop()
        MyCar = CarFactory.GetCarInterface("Ford")
        MyCar.CarRun()
        MyCar.CarStop()
        System.Console.ReadLine()
    End Sub

End Module

Public Interface ICar
    Sub CarRun()
    Sub CarStop()
End Interface

Public Class Benz : Implements ICar  '奔驰汽车


    Public Sub CarRun() Implements ICar.CarRun
        System.Console.WriteLine("奔驰汽车启动了.......")
    End Sub

    Public Sub CarStop() Implements ICar.CarStop
        System.Console.WriteLine("奔驰汽车停车了.......")
    End Sub
End Class

Public Class Ford : Implements ICar  '福特汽车

    Public Sub CarRun() Implements ICar.CarRun
        System.Console.WriteLine("福特汽车启动了.......")
    End Sub

    Public Sub CarStop() Implements ICar.CarStop
        System.Console.WriteLine("福特汽车停车了.......")
    End Sub

End Class

Public Class CarFactory '汽车工厂
    Public Shared Function GetCarInterface(ByVal CarType As String) As ICar
        Select Case CarType
            Case "Benz"
                Return New Benz
            Case "Ford"
                Return New Ford
        End Select
        Return System.DBNull.Value
    End Function
End Class
0 回复
1