工厂方法模式
程序代码:
'工厂方法模式
Module Module1
Sub Main()
'小汽车
Dim MyCar As ICar = CarFactory.GetCarInterface("Benz")
MyCar.CarRun()
MyCar.CarStop()
MyCar = CarFactory.GetCarInterface("Ford")
MyCar.CarRun()
MyCar.CarStop()
'巴士
Dim MyBus As ICar = BusFactory.GetCarInterface("BigBus")
MyBus.CarRun()
MyBus.CarStop()
MyBus = BusFactory.GetCarInterface("MiniBus")
MyBus.CarRun()
MyBus.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 BigBus : 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 MiniBus : 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 Interface AbstructCarFactory '抽象汽车工厂
End Interface
Public Class CarFactory : Implements AbstructCarFactory '小汽车工厂车间
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
Public Class BusFactory : Implements AbstructCarFactory '巴士工厂车间
Public Shared Function GetCarInterface(ByVal CarType As String) As ICar
Select Case CarType
Case "BigBus"
Return New BigBus
Case "MiniBus"
Return New MiniBus
End Select
Return System.DBNull.Value
End Function
End Class
为了简单起见,没有用到反射机制.



