Saturday, 1 February 2014

State Design pattern - Implementation [Vending Machine]



Click here to watch in Youtube : https://www.youtube.com/watch?v=VIzWKPRG9lo

Click the below Image to Enlarge
State Design pattern - Implementation [Vending Machine]
State Design pattern - Implementation [Vending Machine] - Class Diagram










































VendingMachineState.java


public interface VendingMachineState
{

public void selectProductAndInsertMoney(int amount,String productName);

public void dispenseProduct();

}

HasMoneyState.java

public class HasMoneyState implements VendingMachineState
{

@Override
public void selectProductAndInsertMoney( int amount, String productName )
{
System.out.println("Already Vending machine has money and product selected,So wait till it finish the current dispensing process ... ");

}

@Override
public void dispenseProduct()
{
System.out.println("Vending Machine  dispensed the product ...");

}

}

NoMoneyState.java

public class NoMoneyState implements VendingMachineState
{

@Override
public void selectProductAndInsertMoney( int amount, String productName )
{
System.out.println(amount + "Rs has been inserted and " + productName + " has been selected...");

}

@Override
public void dispenseProduct()
{
System.out.println("Vending Machine cannot dispense product because money is not inserted and product is not selected...");

}

}

VendingMachine.java

// This is like Context
public class VendingMachine implements VendingMachineState
{

private VendingMachineState vendingMachineState;

public VendingMachine()
{
vendingMachineState = new NoMoneyState();
}

public VendingMachineState getVendingMachineState()
{
return vendingMachineState;
}

public void setVendingMachineState( VendingMachineState vendingMachineState )
{
this.vendingMachineState = vendingMachineState;
}

@Override
public void selectProductAndInsertMoney( int amount, String productName )
{
vendingMachineState.selectProductAndInsertMoney(amount, productName);
VendingMachineState hasMoneyState = new HasMoneyState();
/*
* Money has been inserted so vending Machine changed the
* internal state to 'hasMoneyState'
*/

if( vendingMachineState instanceof NoMoneyState )
{
setVendingMachineState(hasMoneyState);
System.out.println("VendingMachine internal state has been moved to : "
               + vendingMachineState.getClass().getName());
}

}

@Override
public void dispenseProduct()
{
VendingMachineState noMoenyState = new NoMoneyState();
vendingMachineState.dispenseProduct();

/*
* Product has been dispensed so vending Machine changed the
* internal state to 'NoMoneyState'
*/
if( vendingMachineState instanceof HasMoneyState )
{
setVendingMachineState(noMoenyState);
System.out.println("VendingMachine internal state has been moved to : "
               + vendingMachineState.getClass().getName());
}

}

}

Client.java

public class Client
{

public static void main( String[] args )
{

/*
* Initially Vending Machine will be 'noMoneyState'
*/
VendingMachine vendingMachine = new VendingMachine();

System.out.println("Current VendingMachine State : "
               + vendingMachine.getVendingMachineState().getClass().getName()+"\n");

vendingMachine.dispenseProduct();
vendingMachine.selectProductAndInsertMoney(100, "Pepsi");

/*
* Money has been inserted so vending Machine changed the
* internal state to 'hasMoneyState'
*/

System.out.println("\nCurrent VendingMachine State : "
               + vendingMachine.getVendingMachineState().getClass().getName()+"\n");

vendingMachine.selectProductAndInsertMoney(100, "Fanta");
vendingMachine.dispenseProduct();

/*
* Product has been dispensed so vending Machine changed the
* internal state to 'NoMoneyState'
*/

System.out.println("\nCurrent VendingMachine State : "
               + vendingMachine.getVendingMachineState().getClass().getName());

}

}

Output

Current VendingMachine State : NoMoneyState

Vending Machine cannot dispense product because money is not inserted and product is not selected...
100Rs has been inserted and Pepsi has been selected...
VendingMachine internal state has been moved to : HasMoneyState

Current VendingMachine State : HasMoneyState

Already Vending machine has money and product selected,So wait till it finish the current dispensing process ...
Vending Machine  dispensed the product ...
VendingMachine internal state has been moved to : NoMoneyState

Current VendingMachine State : NoMoneyState

See also:

  • State Design pattern - Introduction
  • State Design pattern - Real time example [ATM]
  • State Design pattern - Real time example [TV Remote]
  • State Design pattern - Real time example [Vending Machine]
  • State Design pattern - Real time example [Project]
  • State Design pattern - Class Diagram
  • State Design pattern - Implementation [ATM]
  • State Design pattern - Implementation [Project]
  • State Design pattern - Implementation [LED TV]
  • State Design pattern - Keypoints
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge