State Pattern

When to Use State Pattern?

Lets Assume a Case where we design a CardReader System.The specification of  the CardReader would be something like at a given point of time the machine can either insert or discard or hold the card for processing.so we find the Usage of State in CardReader .

State of CardReader:

Hold,Discard,Insert

Normal Approach:

A Normal Approach would be to have an Enum with specific state and traditional switch case performing various task of the CardReader.

Code:

public enum CardReaderState
{
Insert,
Discard,
Hold

}
class CardReader
{
private CardReaderState state=CardReaderState.Insert;
public void PerformCardOperation()
{
switch (state)
{
case CardReaderState.Discard:
IdleState();

break;
case CardReaderState.Insert:
state = CardReaderState.Hold;
InsertCard();

break;
case CardReaderState.Hold:
state = CardReaderState.Discard;
CardTasks();
break;
}

}

private void CardTasks()
{
Console.WriteLine(“CardReader Holding the Card For Processing”);
PerformCardOperation();
}

private void IdleState()
{
Console.WriteLine(“CardReader is Idle”);
}

private void InsertCard()
{
Console.WriteLine(“Card Inserted”);
PerformCardOperation();

}
}

Problem with the Above Code:It Solves the Problem,but it clearly fails against Open/Closed Principles and lacks oo principles.what if we need another state introduced,we must modify the enum and alter the switch cases.An addition might introduce new bug and break the functionality.

Solution:

The State Pattern Comes to the rescue.

Definition:Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

My Definition:

1.Create a abstract Class which every state must perform.

2.Override the State behavior with corresponding states.

3.Chain the Next State in the Respective State.

Step 1:

1,Abstract Class :

public abstract class CardState
{
protected CardReader _reader; 

public virtual void PerformCardOperation()
{

}
}

2.State Class for every states

public class CardInsert : CardState
{

public CardInsert(CardReader Reader)
{
_reader = Reader;
}

public override void PerformCardOperation()
{
Console.WriteLine(“Card Inserted”);
_reader.CurrenState=new CardHold(_reader);//Chaining the next States
}

}

public class CardHold : CardState

{
public CardHold(CardReader reader)
{
_reader = reader;
}
public override void PerformCardOperation()
{
Console.WriteLine(“Card Holded For Performing Operation”);
_reader.CurrenState=new CardDiscard(_reader);//Chaining the next state

}

}

public class CardDiscard : CardState
{
public CardDiscard(CardReader reader)
{
_reader = reader;
}
public override void PerformCardOperation()
{
Console.WriteLine(“Card discarded”);
}
}

3.Chain the Respective State : 

In the above code this is the line

_reader.CurrenState=new CardHold(_reader);

which does the chaining the next state.i.e from insert to hold and then to Discard.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s