EPower2002 Example: Using the factory method to design classes

Source: Internet
Author: User
Tags date define datetime integer variables
EPower2002 Example: Using the "Factory method" design class

1. A new type of library project, named "Vbnetdesignmode.vb"
2. Delete the Class1.vb
3. Add class in this project, named "Factorymethod.vb"
4. Add the Product class "Product" to "Factorymethod.vb".
(1) Add three private member variables: mid,mname,mcreadate
(2) Add three public properties: Id,name,creadate
***********************************************
The Factorymethod.vb code is as follows:
***********************************************
Public Class product ' products
' Define Private member variables
Private MID as Integer
Private Mname as String
Private Mcreadate as DateTime
' Define Properties
Public Property ID () as Integer
Get
Return MID
End Get
Set (ByVal Value as Integer)
MID = Value
End Set
End Property
Public Property Name () as String
Get
Return Mname
End Get
Set (ByVal Value as String)
Mname = Value
End Set
End Property
Public Property Creadate () as DateTime
Get
Return mcreadate
End Get
Set (ByVal Value as DateTime)
Mcreadate = Value
End Set
End Property

' The following can also define other properties
' The following can also define more methods
' The following can also define more events
End Class

5. Add another class, named "Factory.vb"
6. In its class, the Createproduct method is defined with the Overridable keyword, and the method returns an instance of the product class.
7. Initialize the product class in the Createproduct function body. Here, for demonstration convenience, we will this product number ID to 99999, product name named "Dreamfactoryno.1", the production date is Date.now

**********************************
The Factory.vb code is as follows:
**********************************
Public Class Factory ' factory
' Define the Createproduct method with the Overridable keyword, which returns an instance of the Product class
' Because it has a overridable keyword, its subclasses can be overwritten with overrides keywords.
Public Overridable Function createproduct () as Product
Dim newproduct as New Product ()
' Initialization of the product class
Newproduct.id = 99999
Newproduct.name = "Dreamfactoryno.1"
Newproduct.creadate = Date.now
Return newproduct
End Function
End Class

8. Next, we will add a "window Application" project to the solution and name the project "Test"
In general, it is a good idea to keep the test project in a folder of that class.

9. Change the VS default generated Form1.vb to "Frmfactory.vb", preferably also "public class Form1 ()" In Code view to "public class frmfactory ()"
The code is as follows:
Public Class Frmfactory
Inherits System.Windows.Forms.Form
#Region "code generated by the Windows forms Designer"
Note: Where the code has been omitted
#End Region
End Class

10. Add a reference to the "Vbnetdesignmode" Item and import the Vbnetdesignmode namespace.
Imports Vbnetdesignmode

11. Add a new class called "Newfactory", and we'll let this class inherit the factory class. Inheritance is implemented through inherits keywords.
Inherits vbnetdesignmode.factory

12. We will overwrite the Createproduct method because we are going to perform a new initialization operation on the product.
As we said earlier, the method can be overridden because it is declared by the Overridable keyword.
Public Class Newfactory
Inherits vbnetdesignmode.factory
Public Overrides Function createproduct () as Product
Dim newproduct as New Product ()
' Initialization of the product class
Newproduct.id = 88888
Newproduct.name = "Dreamfactoryno.2"
Newproduct.creadate = Date.now
Return newproduct
End Function
End Class

13. Finally, in the form load, we test each:

Private Sub frmfactory_load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
Dim Oldpro as New Product ()
Dim Oldfac as New Factory ()
Oldpro = Oldfac.createproduct
Debug.WriteLine ("******** Old product Information **********")
Debug.WriteLine ("ID:" & Oldpro.id)
Debug.WriteLine ("Name:" & Oldpro.name)
Debug.WriteLine ("Creadate:" & Oldpro.creadate)

Dim Newpro as New Product ()
Dim Newfac as New newfactory ()
Newpro = Newfac.createproduct
Debug.WriteLine ("******** New product Information **********")
Debug.WriteLine ("ID:" & Newpro.id)
Debug.WriteLine ("Name:" & Newpro.name)
Debug.WriteLine ("Creadate:" & Newpro.creadate)
End Sub

Summary:
The above code has no practical value, but the design of the implementation of these code is very useful, when you really understand the "factory method" design mode, you can develop more efficient and professional things to.







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.