Sunday, January 31, 2016

State design pattern with java


State pattern, which allows objects to behave in different ways depending on internal state. State is used when you need a class to behave differently, such as performing slightly different computations, based on some arguments passed through to the class.

Problem: Vehicle different status details

1. VehicleNotificationStatus – having different status details about vehicle.
package com.siva;

public enum VehicleNotificationStatus {
 
 VEHICLE_ORDERED("Vehicle-Ordered"),
 VEHICLE_ORDER_SUBMITTED_FOR_BODY_BUILD("Vehicle-Order-Submitted-For-Body-Build"),
 VEHICLE_BODY_SHOP_COMPLETED("Vehicle-Body-shop_completed"),
 VEHICLE_DELIVERED("Vehicle-Delivered"),
 INVALID("Invalid");
 
 VehicleNotificationStatus(String _type){
  this.type=_type;
 }
 private String type;
 

 public String getType() {
  return type;
 }
 public void setType(String type) {
  this.type = type;
 }

}

2. VehicleStatus interface to evaluate the different statues.
package com.siva;

  public interface VehicleStatus {
 
 public VehicleNotificationStatus evaluateVehicleStatus(String status);

  }

3. Different statues classes to implement the VehicleStatus interface.

VehicleOrdered is one type of status class is to implement VehicleStatus
package com.siva.status;

import com.siva.VehicleNotificationStatus;
import com.siva.VehicleStatus;

public class VehicleOrdered implements VehicleStatus{

 @Override
 public VehicleNotificationStatus evaluateVehicleStatus(String status) {
  System.out.println("VehicleOrdered state start");
    if(VehicleNotificationStatus.VEHICLE_ORDERED.getType().equalsIgnoreCase(status)){
    return VehicleNotificationStatus.VEHICLE_ORDERED;
  }
  return VehicleNotificationStatus.INVALID;
    
 }
 

}
2. VehicleForBodyBuild is one type of status class is to implement VehicleStatus
package com.siva.status;

import com.siva.VehicleNotificationStatus;
import com.siva.VehicleStatus;

public class VehicleForBodyBuild implements VehicleStatus{

 @Override
 public VehicleNotificationStatus evaluateVehicleStatus(String status) {
  System.out.println("VehicleForBodyBuild state start");
    if(VehicleNotificationStatus.VEHICLE_ORDER_SUBMITTED_FOR_BODY_BUILD.getType().equalsIgnoreCase(status)){
    return VehicleNotificationStatus.VEHICLE_ORDER_SUBMITTED_FOR_BODY_BUILD;
  }
  return VehicleNotificationStatus.INVALID;
    
 }

}
3. VehicleDelivered is one type of status class is to implement VehicleStatus
package com.siva.status;

import com.siva.VehicleNotificationStatus;
import com.siva.VehicleStatus;

public class VehicleDelivered  implements VehicleStatus{

 @Override
 public VehicleNotificationStatus evaluateVehicleStatus(String status) {
  System.out.println("VehicleDelivered state start");
    if(VehicleNotificationStatus.VEHICLE_DELIVERED.getType().equalsIgnoreCase(status)){
     System.out.println();
    return VehicleNotificationStatus.VEHICLE_DELIVERED;
  }
  return VehicleNotificationStatus.INVALID;
    
 }
}

4. Now we need to write Factory class which can be implement all this status details.
package com.siva;

import com.siva.status.VehicleDelivered;
import com.siva.status.VehicleForBodyBuild;
import com.siva.status.VehicleOrdered;

public class VehicleStatusFactory {
 
 public static VehicleStatus createStatus(VehicleNotificationStatus status){
  if(status.equals(VehicleNotificationStatus.VEHICLE_ORDERED)){
   return new VehicleOrdered();
  }
  else if(status.equals(VehicleNotificationStatus.VEHICLE_ORDER_SUBMITTED_FOR_BODY_BUILD)){
   return new VehicleForBodyBuild();
  }
  else if(status.equals(VehicleNotificationStatus.VEHICLE_DELIVERED)){
   return new VehicleDelivered();
  }
  else
   return null;
 }

}

5. We have completed the all the classes related to State pattern, now we need to write test class to see the results.
package com.siva;

public class TestStateDesignPattern {
 
 public static void main(String[] args) {
  
  VehicleStatus vehicleStatus =VehicleStatusFactory.createStatus(VehicleNotificationStatus.VEHICLE_ORDERED);
  VehicleNotificationStatus notificationStatus = vehicleStatus.evaluateVehicleStatus(VehicleNotificationStatus.VEHICLE_ORDERED.getType());
  System.out.println(notificationStatus);
  vehicleStatus =VehicleStatusFactory.createStatus(VehicleNotificationStatus.VEHICLE_ORDER_SUBMITTED_FOR_BODY_BUILD);
  VehicleNotificationStatus notificationStatus1 = vehicleStatus.evaluateVehicleStatus(VehicleNotificationStatus.VEHICLE_ORDER_SUBMITTED_FOR_BODY_BUILD.getType());
  System.out.println(notificationStatus1);
  vehicleStatus =VehicleStatusFactory.createStatus(VehicleNotificationStatus.VEHICLE_DELIVERED);
  VehicleNotificationStatus notificationStatus2 = vehicleStatus.evaluateVehicleStatus(VehicleNotificationStatus.VEHICLE_DELIVERED.getType());
  System.out.println(notificationStatus2);
 }

}


Output:
VehicleOrdered state start
VEHICLE_ORDERED
VehicleForBodyBuild state start
VEHICLE_ORDER_SUBMITTED_FOR_BODY_BUILD
VehicleDelivered state start

VEHICLE_DELIVERED




This is how state pattern will work.
Thanks for viewing this post.

2 comments:

AddToAny

Contact Form

Name

Email *

Message *