Let's take a look at this example. We need to create a vehicle, which can be a car, train, or ship. The structure is as follows:
We can use a simple factory to tell the object type we need to create a factory through parameters. If we add sub-classes, such as trucks and cars, we must add parameters and correspondingCodeIf there are many sub-classesProgramBecomes difficult to maintain. It is obviously cumbersome to use a simple factory to implement the above structure.
Of course, we can use the factory method to implement it. We can define an interface to generate transportation tools, and then create a specific subclass in the subclass:
'Using interfaces to define abstract factory methods
Public InterfaceCreatevehicle
FunctionCreateavehicle ()AsVehicle'Create a vehicle
End Interface
'The specific creation is determined by the subclass.
Public ClassCreatecar
ImplementsCreatevehicle
Public FunctionCreateavehicle ()AsVehicleImplementsCreatevehicle. createavehicle
Return NewCar
End Function
End Class
This is the factory method. But if we want to add a new transport, we need not only to implement the transport interface, but also to implement the factory method to generate the transport. The specific factory method of the ship is as follows:
Public ClassCreateboat
ImplementsCreatevehicle
Public FunctionCreateavehicle ()AsVehicleImplementsCreatevehicle. createavehicle
Return NewBoat
End Function
End Class
Obviously, if we need to generate dozens of transportation tools, we need dozens of specific factory classes. The difference between these factory classes is only to return the corresponding instance of the class. This makes maintenance troublesome. If you need to add a parameter creation method to the interface, all subclasses must be modified.
How about using abstract factories? In this case, there is no difference between the abstract factory and the factory method, because there is no product line involved here, and the abstract factory cannot solve the problem here. Of course, if each type of transportation has a corresponding station, the abstract factory will be used, but that will be more complicated.
Is it possible to pass the type of the class to be created to the factory method, and the factory method returns the corresponding instance according to the type?
The key to solving the above problems is to dynamically determine the classes to be created. This is not a problem that can be solved by the design pattern and belongs to the functional category of the software platform. Good news. NetIt can provide corresponding functions, which is reflection technology.
Let's take a look at the example of simplifying reflection technology:
ImportsSystem. Reflection
Public ClassCreatevehiclebytype
ImplementsCreatevehicle
PrivateVeicletypeAsType
Public Sub New(ByvalTAsType)
Veicletype = T
End Sub
Public FunctionCreateavehicle ()AsVehicleImplementsCreatevehicle. createavehicle
DimObjconstructorAsConstructorinfo = veicletype. getconstructor (system. type. emptytypes)
DimCAsVehicle =Ctype(Objconstructor. Invoke (Nothing), Vehicle)
ReturnC
End Function
End Class
in use, you only need to include the type of the class to be created during creation.