static void Main(string[] args) { // Initially ATM Machine in DebitCardNotInsertedState ATMMachine atmMachine = new ATMMachine(); Console.WriteLine("ATM Machine Current state : " + atmMachine.atmMachineState.GetType().Name); Console.WriteLine(); atmMachine.EnterPin(); atmMachine.WithdrawMoney(); atmMachine.EjectDebitCard(); atmMachine.InsertDebitCard(); Console.WriteLine(); // Debit Card has been inserted so internal state of ATM Machine // has been changed to DebitCardInsertedState Console.WriteLine("ATM Machine Current state : " + atmMachine.atmMachineState.GetType().Name); Console.WriteLine(); atmMachine.EnterPin(); atmMachine.WithdrawMoney(); atmMachine.InsertDebitCard(); atmMachine.EjectDebitCard(); Console.WriteLine(""); // Debit Card has been ejected so internal state of ATM Machine // has been changed to DebitCardNotInsertedState Console.WriteLine("ATM Machine Current state : " + atmMachine.atmMachineState.GetType().Name); Console.Read(); }
/* * Reference: YouTube/Derek Banas * Allows an object to alter it's behavior when it's internal state changes * Here, we're trying to achieve a situation where an action could be called in any state of the object and it should be well equiped to handle it. * For e.g. States like HasCard, NoCard, HasCash, NoCash etc. the ATM Machine could be either of the states, say HasCard - the next logical step would be add PIN which should be handled appropriately. However, if the requests cash directly, then this too should be handled properly. * Hence, we define an interface IState, all identified States are defined it and equal implementation class are instantiated. * In the ATM Machine class, we hold instances of these stated and vice versa. * We'll getter setter methos on ATM Machine for all properties and states. * This is a fool proof plan if all the states are identified and defined well. */ static void Main(string[] args) { var atmMachine = new ATMMachine(); atmMachine.insertCard(); atmMachine.ejectCard(); atmMachine.insertCard(); atmMachine.insertPin(1234); atmMachine.requestCash(1500); atmMachine.insertCard(); atmMachine.insertPin(1234); }
public NoCash(ATMMachine newATMMachine) { atmMachine = newATMMachine; }
public NoCash(ATMMachine atm) { }
public HasCard(ATMMachine atm) { _atm = atm; }
public HasCard(ATMMachine newATMMachine) { atmMachine = newATMMachine; }
public HasPin(ATMMachine atm) { _atm = atm; }
public HasPin(ATMMachine newATMMachine) { atmMachine = newATMMachine; }
public NoCard(ATMMachine atm) { _atm = atm; }
public HasNoCash(ATMMachine _aTMMachine) { aTMMachine = _aTMMachine; }
public HasPin(ATMMachine _aTMMachine) { atmMachine = _aTMMachine; }
public NoCard(ATMMachine _aTMMachine) { aTMMachine = _aTMMachine; }
public hasCard(ATMMachine _atmMachine) { atmMachine = _atmMachine; }