码迷,mamicode.com
首页 > 其他好文 > 详细

装饰模式小试

时间:2015-05-14 12:10:17      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:设计

背景

用装饰模式来实现机房收费系统登录之后的权限控制

总体思路

每个用户都是一个人,每种权限可以理解成人的不同服饰。

程序结构

技术分享

技术分享

技术分享

代码

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

form1的代码:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!