State Pattern: Allow an object to alter its behaviour when it’s internal state changes.
The object will appear to change its class..
Example
State.java
package com.statepattern.example; public interface State { public void insertCard(); public void ejectCard(); public void enterAmount(); public void dispense(); }
NoCardState.java
package com.statepattern.example; public class NoCardState implements State { CashMachine cashMachine; public NoCardState(CashMachine cashMachine) { this.cashMachine = cashMachine; } @Override public void insertCard() { System.out.println("Please insert your card now"); cashMachine.setState(cashMachine.getHasCardState()); } @Override public void ejectCard() { System.out.println("Can't return anything as there is no Card"); } @Override public void enterAmount() { System.out.println("Can't enter amount now as there is no card"); } @Override public void dispense() { System.out.println("Can't dispense amount now as there is no card"); } public String toString(){ return "No Card State"; } }
HasCardState.java
package com.statepattern.example; public class HasCardState implements State{ CashMachine cashMachine; public HasCardState(CashMachine cashMachine) { this.cashMachine = cashMachine; } @Override public void insertCard() { System.out.println("you can't insert another Card"); } @Override public void ejectCard() { System.out.println("Your card is returned now"); cashMachine.setState(cashMachine.getNoCardState()); } @Override public void enterAmount() { cashMachine.setState(cashMachine.getWorkingState()); } @Override public void dispense() { System.out.println("cash Machine is already dispensing"); } public String toString(){ return "Has Card State"; } }
WorkingState.java
package com.statepattern.example; public class WorkingState implements State { CashMachine cashMachine; public WorkingState(CashMachine cashMachine) { this.cashMachine = cashMachine; } @Override public void insertCard() { System.out.println("You can't insert card Now as processing is in progress"); } @Override public void ejectCard() { System.out.println("You can't eject card Now as processing is in progress"); } @Override public void enterAmount() { System.out.println("Already Processing your Transcation Please wait"); } @Override public void dispense() { if(cashMachine.getBalance()>cashMachine.getWithdrawAmt()){ cashMachine.setBalance(cashMachine.getBalance()-cashMachine.getWithdrawAmt()); System.out.println("Dispensing amount " + cashMachine.getWithdrawAmt()); System.out.println("Now Balance is " + cashMachine.getBalance()); cashMachine.setState(cashMachine.getNoCardState()); }else{ System.out.println("Sorry!.. Sufficient balance is not available"); cashMachine.setState(cashMachine.getOutofServiceState()); } } public String toString(){ return "Working State"; } }
OutofServiceState.java
package com.statepattern.example; public class OutofServiceState implements State { CashMachine cashMachine; public OutofServiceState(CashMachine cashMachine) { this.cashMachine = cashMachine; } @Override public void insertCard() { System.out.println("Can't insert card now as cash Machine is out of Service"); } @Override public void ejectCard() { System.out.println("Can't eject Card now as cash Machine is out of Service"); } @Override public void enterAmount() { System.out.println("Can't enter amount now as cash Machine is out of Service"); } @Override public void dispense() { System.out.println("Can't dispense amount now as cash Machine is out of Service"); } public String toString(){ return "OutofService State"; } }
CashMachine.java
package com.statepattern.example; public class CashMachine { private State hasCardState; private State noCardState; private State workingState; private State outofServiceState; //setting the default state to Out of Service private State state = outofServiceState; private int balance; private int withdrawAmt; public CashMachine(int balance) { hasCardState = new HasCardState(this); noCardState = new NoCardState(this); workingState = new WorkingState(this); outofServiceState = new OutofServiceState(this); this.balance = balance; if (balance > 0) { //if the balance is greather than zero then set the cashMachine to No Card State state = noCardState; } } public void setState(State state) { this.state = state; } public void insertCard() { state.insertCard(); } public void ejectCard() { state.ejectCard(); } public void enterAmount(int amount) { this.withdrawAmt = amount; state.enterAmount(); } public void dispense() { state.dispense(); } public State getHasCardState() { return hasCardState; } public void setHasCardState(State hasCardState) { this.hasCardState = hasCardState; } public State getNoCardState() { return noCardState; } public void setNoCardState(State noCardState) { this.noCardState = noCardState; } public State getWorkingState() { return workingState; } public void setWorkingState(State workingState) { this.workingState = workingState; } public State getOutofServiceState() { return outofServiceState; } public void setOutofServiceState(State outofServiceState) { this.outofServiceState = outofServiceState; } public State getState() { return state; } public void setBalance(int balance) { this.balance = balance; } public int getBalance() { return balance; } public int getWithdrawAmt() { return withdrawAmt; } public void printState() { System.out.println("Current State : " + this.state); } }
CashMachineTestDrive.java
package com.statepattern.example; public class CashMachineTestDrive { public static void main(String[] args) { CashMachine cashMachine = new CashMachine(100000); //printing current state using method printState cashMachine.printState(); cashMachine.insertCard(); cashMachine.printState(); cashMachine.enterAmount(50000); cashMachine.printState(); cashMachine.dispense(); cashMachine.printState(); } }Home
No comments:
Post a Comment