Dot Net Design Model-simple factory

Source: Internet
Author: User
Tags dot net

1. Preface
1.1 Summary
The role of a simple factory is to instantiate an object, without the need for the customer to know which specific subclass the object belongs.
There is no simple factory in the GOF design model, but it is explained as a special case of the factory method. It can be understood that a simple factory is a parametric factory method.

1.2 usage
Classes instantiated in a simple factory have the same interface. When class classes are limited and do not need to be extended, you can use a simple factory. For example, if the database connection object and common database classes are predictable, a simple factory is used.

1.3 Results
The advantage of using a simple factory is that users can obtain corresponding class instances based on parameters, avoiding direct instantiation of classes and reducing coupling; the disadvantage is that the instantiated type has been determined during compilation. If a new type is added, You need to modify the factory. A simple factory needs to know all types to be generated. It is not suitable for use when there are too many child classes or too many child classes.

2.. NET implementation
2.1 Implementation highlights
Generally, a simple factory does not need to be instantiated, but uses a static method. In VB. NET, it is called the "sharing method", because a simple factory is generally only responsible for instantiation. In C #, the static method uses the statics keyword and the Shared keyword in VB. NET.
Another implementation point is not in the factory, but in the class to be instantiated, abstract classes and interface methods can be implemented to abstract specific classes. For a simple factory, both methods can achieve the same effect. However, considering the maintainability and scalability of the system, the advantages and disadvantages of the two solutions must be discussed.
If the subclass to be instantiated has a large number of identical code, you can consider using abstract classes to reduce code duplication. For example, subclass has a large number of common attributes. However, the disadvantage of doing so is poor scalability and potential coupling between sub-classes. Therefore, you can usually consider using interfaces instead of abstract classes. A specific class can achieve the same effect by implementing this interface.

2.2 Structure
Suppose we want to generate the subclass and structure of Vehicle.

From the structure of a simple factory, we can see that a simple factory depends on all product subclasses. The benefit is that the customer only needs to know the product's parent class and factory.
2.3 code framework
The following is the code framework:
'. NET sample code

'Transportation is a base class that must be inherited.
Public MustInherit Class VehicleClass Vehicle
Protected m-TypeName As String
Public Property TypeName () As String
Get
Return m-TypeName
End Get
Set (ByVal Value As String)
M-TypeName = Value
End Set
End Property
Public MustOverride Function Go () As String
End Class
'Car
Public Class CarClass Car
Inherits Vehicle
Public Sub New ()
M-TypeName = "car"
End Sub

Public Overrides Function Go () As String
Return "driving on the road"
End Function
End Class
'Train
Public Class TrainClass Train
Inherits Vehicle

Public Sub New ()
M-TypeName = "train"
End Sub

Public Overrides Function Go () As String
Return "Train on Rails"
End Function
End Class
'Boat
Public Class BoatClass Boat
Inherits Vehicle

Public Sub New ()
M-TypeName = "boat"
End Sub
Public Overrides Function Go () As String
Return "boat on water"
End Function
End Class

Public Class CreateAVehicleClass CreateAVehicle
'Define the creation method as sharing
Public Shared Function CreateAVehicle () Function CreateAVehicle (ByVal typeid As String) As Vehicle
Dim v As Vehicle
'Determine the object to be created based on the parameter
Select Case typeid. ToLower
Case "car"
V = New Car
Case "train"
V = New Train
Case "Boat"
V = New Boat
End Select

Return v
End Function
End Class

This section describes the traditional implementation methods. In the. NET development environment, you can use the reflection method described earlier to simplify the implementation.

3. Simple factory Application
Simple factory applications have a wide range of applications. In one application, we can apply it repeatedly to make the program easy to maintain. For example, in Web applications developed using ASP. NET, you can use the method described earlier to enable applications to support multiple types of databases. In this case, you can save the database type and connection string as parameters in the Web. Config file. When the database type used changes, you only need to change these parameters.
When you need to obtain a database connection, first obtain the database type and connection string from Web. Config, and then call the corresponding class instantiation.
Another example is that many e-payment platforms use this mode. If you develop your own interface with various banks, you should also consider the simple factory model.
The implementation code of these two solutions is very simple, so we will not detail them here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.