24 April 2013

State Pattern in VB.net

Today we will discuss about the State Pattern in Vb.net.
Description:
    State pattern allows an object to change its behavior depending on the current values of the object. Consider the figure 'State pattern example'. It's an example of a

Box Status operation. If the state of the Box Status is off and you press the switch the BoxStatus will turn off. If the state of BoxStatus is on and you press the switch the Box Status will be off. So

in short depending on the state the behavior changes.
Create a Class file in the Vb.net Project


Enum BoxStatus
BoxOn = 1
BoxOff = 2
End Enum
Public Shared defaultBoxstatus As BoxStatus = BoxStatus.BoxOff
Public Function displayMessage() As String
If BoxStatus.BoxOn = defaultBoxstatus Then
Return "Box is on"
Else
Return "Box is off"
End If
End Function
Public Sub Pressswitch()
If defaultBoxstatus = BoxStatus.BoxOn Then
defaultBoxstatus = BoxStatus.BoxOff
Else
defaultBoxstatus = BoxStatus.BoxOn
End If
End Sub
Dim Clas As New Class1()
Clas.Pressswitch()
MessageBox.Show(Clas.displayMessage())

No comments: