最近在做.net机房收费系统,和简单的三层来一个对比,现在贴出来和大家分享!
包图:
应用了设计模式(抽象工厂),这样使得B层和D层之间无需再有关系(应用接口),应用了抽象工厂+反射+配置文件,使得工厂和D层的关系线消失,这样B层和工厂只需知道D层的接口,和D层解耦。
类图在这里就省略了!
时序图:
下面则是实现的代码,当然只是简单的用上图的架构实现了登录的功能,时序图中的一些逻辑处理还没有应用.
U层:
[vb]
Public Class Form
Private Sub butExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butExit.Click
End
End Sub
Private Sub butLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butLogin.Click
'定义一个实体类对象,将文本框中的内容赋给实体类对象
Dim user As Model.Teacher = New Model.Teacher
user.user_ID = txtUserID.Text.ToString()
user.user_PWD = txtUserPWD.Text.ToString()
'定义一个业务逻辑层对象,根据业务逻辑层返回的结果,通知用户信息
Dim fd As Facade.UserF = New Facade.UserF
If fd.Query(user) Then
MsgBox(True)
Else
MsgBox(False)
End If
End Sub
End Class
Facede层:
[vb]
Public Class UserF
Dim uB As BLL.UserBLL = New BLL.UserBLL
Function Query(ByVal user As Model.Teacher) As Boolean
If uB.QueryUser(user) Then
Return (True)
Else
Return (False)
End If
End Function
End Class
B层
[vb]
Public Class UserBLL
Dim factory As Factory.UserFactory = New Factory.UserFactory
Dim IUserDAL As IDAL.IUserIDAL
Public Function QueryUser(ByVal user As Model.Teacher) As Boolean
IUserDAL = factory.CreateUserDAL()
If IUserDAL.Query_user(user) Then
Return (True)
Else
Return (False)
End If
End Function
End Class
Factory层
[vb]
Imports System.Reflection
Imports System.Configuration
Public Class UserFactory
Public Function CreateUserDAL() As IDAL.IUserIDAL
Dim AssemblyName = "DAL"
'Dim create = "User"
Dim create As String = ConfigurationManager.AppSettings("create")
Dim className As String
'Dim user As DAL.UserDAL = New DAL.UserDAL
'Return user
className = AssemblyName + "." + create + "DAL"
Return CType(Assembly.Load(AssemblyName).CreateInstance(className), IDAL.IUserIDAL)
End Function
End Class
IDAL层
[vb]
Imports Model
Public Inte易做图ce IUserIDAL
Function Query_user(ByVal user As Teacher) As Boolean
End Inte易做图ce
学习的过程就是不断反复,不断对比,我们也是在不断的跌跌撞撞中成长,正是这样的成长,让我们逐步的去认识新的事物,了解它,以致掌握它!