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 BoxStatusoperation. If the state of the BoxStatus 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 BoxStatus will be off. So in short depending on the state the behavior changes.
Create a Class file in the Vb.net Project
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 BoxStatusoperation. If the state of the BoxStatus 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 BoxStatus 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
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 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
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())
Clas.Pressswitch()
MessageBox.Show(Clas.displayMessage())