简单工厂模式
程序代码:
'''''ICar.cls Public Function CarRun() As String '抽象汽车方法 End Function Public Function CarStop() As String End Function
程序代码:
'Bus.cls '具体汽车方法 Implements ICar Private Function ICar_CarRun() As String ICar_CarRun = "Bus开了..." End Function Private Function ICar_CarStop() As String ICar_CarStop = "Bus停了..." End Function
程序代码:
'''MiniCar.cls '具体汽车方法 Implements ICar Private Function ICar_CarRun() As String ICar_CarRun = "小汽车开了..." End Function Private Function ICar_CarStop() As String ICar_CarStop = "小汽车停了..." End Function
程序代码:
'''CarFactory.cls Friend Function MakeCar(ByVal CarType As String) As Object '抽象汽车工厂 Select Case CarType Case "MiniCar" Set MakeCar = New MiniCar Case "Bus" Set MakeCar = New Bus Case Else MakeCar = Null End Select End Function
程序代码:
Private Sub Form_Click() Dim CarFactory As New CarFactory Dim MyCar As New ICar Set MyCar = CarFactory.MakeCar("Bus") Print MyCar.CarRun Print MyCar.CarStop Set MyCar = CarFactory.MakeCar("MiniCar") Print MyCar.CarRun Print MyCar.CarStop End Sub