标签:设计
背景
用装饰模式来实现机房收费系统登录之后的权限控制
总体思路
每个用户都是一个人,每种权限可以理解成人的不同服饰。
程序结构
代码
Class1的代码:
Public Class UserLogin Public Overridable Sub Login() Main.Show() End Sub End Class Public Class Finer : Inherits UserLogin Protected component As UserLogin Public Sub Decorate(component As UserLogin) Me.component = component End Sub Public Overrides Sub Login() If Not (component Is Nothing) Then component.Login() End If End Sub End Class Public Class CommonUserLogin : Inherits Finer Public Overrides Sub Login() Main.一般用户ToolStripMenuItem.Visible = True MyBase.Login() End Sub End Class Public Class OperateUserLogin : Inherits Finer Public Overrides Sub Login() Main.操作员ToolStripMenuItem.Visible = True MyBase.Login() End Sub End Class Public Class MgrUserLogin : Inherits Finer Public Overrides Sub Login() Main.管理员ToolStripMenuItem.Visible = True MyBase.Login() End Sub End Class Public Class CommonUser Public Sub LoginSystem() Dim CommonUser As UserLogin = New UserLogin() Dim CommonUserLogin As New CommonUserLogin() CommonUserLogin.Decorate(CommonUser) CommonUserLogin.Login() End Sub End Class Public Class OperateUser Public Sub LoginSystem() Dim OperateUser As UserLogin = New UserLogin() Dim CommonUserLogin As New CommonUserLogin() Dim OperateUserLogin As New OperateUserLogin() CommonUserLogin.Decorate(OperateUser) OperateUserLogin.Decorate(CommonUserLogin) OperateUserLogin.Login() End Sub End Class Public Class ManagerUser Public Sub LoginSystem() Dim ManagerUser As UserLogin = New UserLogin() Dim CommonUserLogin As New CommonUserLogin() Dim OperateUserLogin As New OperateUserLogin() Dim ManagerUserLogin As New MgrUserLogin() CommonUserLogin.Decorate(ManagerUser) OperateUserLogin.Decorate(CommonUserLogin) ManagerUserLogin.Decorate(OperateUserLogin) ManagerUserLogin.Login() End Sub End Class
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If RadioButton1.Checked Then Dim CommonUser As New CommonUser CommonUser.LoginSystem() Exit Sub End If If RadioButton2.Checked Then Dim OperateUser As New OperateUser OperateUser.LoginSystem() Exit Sub End If If RadioButton3.Checked Then Dim MgrUser As New ManagerUser MgrUser.LoginSystem() Exit Sub End If End Sub End Class
标签:设计
原文地址:http://blog.csdn.net/cxl0921/article/details/45717653