State Design Pattern in Visual Basic .NET
Definition
The State Design Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change
its class.
State Design Pattern is one of the Behavioral Design Patterns. Some objects may have several states in its life cycle, and also it has different behavior depending on the state where the object lives. In such cases you can use State Design Patter
First of all we will see the UML diagram for State Design Pattern
Participants
Context
- Provide interface to client
- Keep track of the current state which is an instance of the
ConcreteSate
State
This is the interface which defines all the states of the Context class
Concrete State
You can have sub class for each state. Each state has different behaviors
Example
We will see an example to understand State Design Pattern and implement it in VB.NET
A file can have three different states Open,Close,Delete. If file is opened you can not delete it but you can close it. If file is closed you can open it or you can delete it. If file is deleted you can not open it or you can not close it
To implement above example we can use the following UML diagram
Here File is the Context .
Now we will see how we can code this State Design Pattern in VB.NET
File (context) Class
Public Class File Private m_state As State Public Property State() As State Get Return m_state End Get Set(ByVal value As State) m_state = value End Set End Property Public Sub New() m_state = New CloseState(Me) End Sub Public Sub Open() m_state.Open() End Sub Public Sub Delete() m_state.Delete() End Sub Public Sub Close() m_state.Colse() End Sub End Class
State Interface
Public Interface State Sub Open() Sub Delete() Sub Colse() End Interface
DeleteState Concrete Class
Public Class DeleteState Implements State Dim f As File Public Sub New(ByVal obj As File) f = obj End Sub Public Sub Colse() Implements State.Colse Console.WriteLine("No file exists to close") End Sub Public Sub Delete() Implements State.Delete If (f.State.ToString() = "Delete State") Then Console.WriteLine("File already deleted") Else f.State = New DeleteState(f) Console.WriteLine("File deleted") End If End Sub Public Sub Open() Implements State.Open Console.WriteLine("No file exists to open") End Sub Public Overrides Function ToString() As String Return "Delete State" End Function End Class
OpenState Concrete Class
Public Class OpenState Implements State Dim f As File Public Sub New(ByVal obj As File) f = obj End Sub Public Sub Colse() Implements State.Colse f.State = New CloseState(f) Console.WriteLine("File is on Closed State now") End Sub Public Sub Delete() Implements State.Delete Console.WriteLine("You can not delete file because file is on " + f.State.ToString()) End Sub Public Sub Open() Implements State.Open End Sub Public Overrides Function ToString() As String Return "Open State" End Function End Class
CloseState Concrete Class
Public Class CloseState Implements State Dim f As File Public Sub New(ByVal obj As File) f = obj End Sub Public Sub Colse() Implements State.Colse Console.WriteLine("File is already closed") End Sub Public Sub Delete() Implements State.Delete Console.WriteLine("File Deleted") f.State = New DeleteState(f) End Sub Public Sub Open() Implements State.Open f.State = New OpenState(f) End Sub Public Overrides Function ToString() As String Return "Close State" End Function End Class
Now you can test the above code with following code
Module Module1 Sub Main() Dim f As New File() f.Open() f.Delete() f.Close() f.Delete() f.Delete() Console.ReadLine() End Sub End Module
Finally, you will see the following output
You can not delete file because file is on Open State File is on Closed State now File Deleted File already deleted