Introduction to State Design Pattern(Java, C# Example)

In this article we are going to learn about State Design Pattern. What is State pattern? What are the basic principles? How to implement and where we can use this.
I have used Java with Eclipse and C# with VS2010.

What is State Pattern?

In OO design, when we need to change behavior of an object depending on its state we use State pattern. Lets think about payment transaction domain. The main object responsible for a payment transaction used to notify a set a set of other objects depend on his state. Suppose, there is a successful transaction, it will notify customer, balance object. If, there were communication problem, it will notify client, then notify bank manager type objects or it there is any insecure activity then it also notify bank's security notifying object.

State pattern allows an object to change its behavior depend on the internal state change. So, now, what is state. A state of an object refers to a.values of its properties b.effects of functions(may be void or any valuated output). So, if the state change, it will work differently.

Let's consider a sales order , a sales order can be new order, registered in system, granted order, shipped order, invoicing or even cancelled. These are the state of the same order state and its inner states will be change along with the functionality of the whole sales order object.

Typically in state design pattern contains
1. Context :Context is the main object. It is the container of different states.
2. States : It should be derived among same grouped classes
 
Basic Principle :
- Allowing changing behavior
- Let an object decide it behavior based on its state

UML :


How to implement?

As we know, we will have context , states and client who actually calls the context. So, lets think about how to implement. It is for sure, a context will contain all of its state. And, a context will have the functions which will be changed as soon as its state has been changed. So, we can understand this context is actually a composition of all state.
And, an individual state should have function defining for each variety of state. That means either a method taking parameter to vary from state to state or, methods for each state behavior.
This are the simple ideas, let see more detail with example.

Example : Let's consider an android mobile connecting to PC. We will represent an Android mobile as Context object(we may apply interface also that will be called from client). We have mainly three states( three way to connect a device to PC)
1. Connect to PC for charge only : An android device will be connected. Device will be charging only in this state.
2. Connect to PC with ADB Enabled : An android device can be connected to PC with debug mode. In this mode ADB(Android Debug Bridge) will be activated and we can control the android mobile from PC
3. Connect to PC with USB Storage Enabled : An Android device can be connected to PC with USB storage mode. We can see the mobile's storage as drive in this mode.

And, every time the android mobile is connected , depending on state its behavior will be changed. So, that means , each state will represent different behavior. That means, we have to define at least one method to represent the function of a particular behavior. We may use more than one method or even describe in a single method, but I guess it is better to use separate methods to represent separate state behaviors(easy to understand).
And, as I told in the beginning, Android will be our context. We may use a context Interface IMobile which should be implemented by the context object Android so that client can initiate Android using IMobile. This context will have facility to change its own state and providing method to access its state.
And, lastly a Client. A client calls the context and may change behavior to get the context see functions are changing. So,

Step -1 :Defining States : So, Start with IState interface that contains all state related changing method. 
   1:  public interface IState {    
   2:      public void charge();
   3:      public void usbMount();
   4:      public void adbConnect();
   5:  }

Note: We could make a common method and use parameter to define functionality. But, I use separate methods to understand properly as well as I have use those methods in side separate states to change the states.

And, implementing states ChargingState, AdbState ,USBMountingState

   1:  public class ChargingState implements IState{
   2:      private IMobile myMobile;
   3:      public ChargingState(IMobile aMobile){
   4:          myMobile = aMobile;
   5:      }
   6:      public void charge() {
   7:          System.out.println("Mobile is connected to PC for charging only");
   8:      }
   9:      public void usbMount() {
  10:          System.out.println("While Charging Only mode device can not mount as USB storage, please select USB mount from Device");
  11:          myMobile.setMobileState(myMobile.getUsb());
  12:      }
  13:      public void adbConnect() {
  14:          System.out.println("While Charging Only mode we can not access ADB , please select Android Debugging from Device settings");
  15:          myMobile.setMobileState(myMobile.getAdb());
  16:      }
  17:  } 

   1:  public class AdbState implements IState{
   2:      private IMobile myMobile;
   3:      public AdbState(IMobile aMobile){
   4:          myMobile = aMobile;
   5:      }
   6:      public void charge() {
   7:          System.out.println("While ADB mode, your device is by default charging");
   8:          myMobile.setMobileState(myMobile.getCharging());
   9:      }
  10:      public void usbMount() {
  11:          System.out.println("While ADB mode , device can not mount as USB storage, please select USB mount from Device");
  12:          myMobile.setMobileState(myMobile.getUsb());
  13:      }
  14:      public void adbConnect() {
  15:          System.out.println("Your device is connected in Android Debugging Mode, to cancel ADB mode please dis-select Android Debugging from Device settings");
  16:      }
  17:  }

   1:  public class USBMountingState implements IState{
   2:      private IMobile myMobile;
   3:      public USBMountingState(IMobile aMobile){
   4:          myMobile = aMobile;
   5:      }
   6:      public void charge() {
   7:          System.out.println("While USB Only mode, your device is by default charging");
   8:          myMobile.setMobileState(myMobile.getCharging());
   9:      }
  10:      public void usbMount() {
  11:          System.out.println("Deviec is in USB mode, access mobile storage from file explorer");
  12:      }
  13:      public void adbConnect() {
  14:          System.out.println("While USB Only mode we can not access ADB , please select Android Debugging from Device settings");
  15:          myMobile.setMobileState(myMobile.getAdb());
  16:      }
  17:  }


Step -2 : Creating Context : Now, Lets make a context interface IMobile that will contain all state changing behavior as well as behavior calling functions.

   1:  //Context Interface
   2:  public interface IMobile {
   3:      public void setMobileState(IState aState);
   4:      public void ChargeMobile();
   5:      public void USBConnect();
   6:      public void AdbAccess();
   7:      public IState getCharging() ;
   8:      public IState getUsb() ;
   9:      public IState getAdb() ;
  10:  }

And, implementing class Android

   1:  //Context Class
   2:  public class Android implements IMobile{
   3:      private IState charging;
   4:      private IState usb;
   5:      private IState adb;
   6:      
   7:      private IState mobileState;    
   8:      public void setMobileState(IState aState) {
   9:          mobileState= aState;
  10:      }
  11:      public Android(){
  12:          charging = new ChargingState(this);
  13:          usb = new USBMountingState(this);
  14:          adb = new AdbState(this);
  15:          mobileState = charging;
  16:      }
  17:      //next three are for accessing functions in one state
  18:      public void ChargeMobile(){
  19:          mobileState.charge();
  20:      }
  21:      public void USBConnect(){
  22:          mobileState.usbMount();
  23:      }
  24:      public void  AdbAccess() {
  25:          mobileState.adbConnect();
  26:      }
  27:      // next three are getting states
  28:      public IState getCharging() {
  29:          return charging;
  30:      }    
  31:      public IState getUsb() {
  32:          return usb;
  33:      }    
  34:      public IState getAdb() {
  35:          return adb;
  36:      }
  37:  }


Step -3 : Calling From Client : So, now we call context from client and we will see when the context changed, the same method of a same context provides different results.

   1:  public class Program {
   2:      public static void main(String[] args) {
   3:          IMobile samsungGalaxy = new Android();//initial state will be charging 
   4:          samsungGalaxy.ChargeMobile();
   5:          samsungGalaxy.AdbAccess();//mode will be change to ADB
   6:          samsungGalaxy.AdbAccess();
   7:          samsungGalaxy.USBConnect();//mode will change to USB
   8:          samsungGalaxy.USBConnect();
   9:      }
  10:  }

Java Example :
1. To keep simple, I have used only command print to define behaviors.

C# Example :
1. Some method names are changed due to different implementation.

Uses :
1. When we need an individual object's behavior specified by its state.(not decided by other objects)
2. When are design stand alone system object( like as device representing objects).
3. Very good to represent a data table or database state or record state with this pattern.
And sometimes it is handy to use with other patterns.

Thanks...:)