刚刚简单的实现了抽象工厂模式,只是有个模样先来晒晒算是对自己的鼓励吧。
其效果是:
对每个类写的代码并不多,主要注意点是vb.net语法与c#语法不同。如关键字Overrides,Inherits等的使用,以及在函数返回值方面的不同,vb.net直接用“=”完成:
CreateUser=New AccessUser。而C#实现中使用Return关键字。
这样做在客户端代码里就不必出现具体的数据库访问类,只需要使用这些类的抽象。将具体的实现数据访问的过程封装起来了。客户端代码如下:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim AccessFactoryTest As New AccessFactory
Dim UserTest As User
UserTest = AccessFactoryTest.CreateUser
UserTest.ConnectDataBase()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button2.Click
Dim sqlserverFactoryTest As New sqlserverFactory
Dim sqlserverUserTest As User
sqlserverUserTest = sqlserverFactoryTest.CreateUser
sqlserverUserTest.ConnectDataBase()
End Sub
End Class
原文链接:https://www.f2er.com/vb/262457.html