|
| 1 | +abstract class OrderState { |
| 2 | + protected context: TaxiOrderContext; |
| 3 | + |
| 4 | + constructor(context: TaxiOrderContext) { |
| 5 | + this.context = context; |
| 6 | + } |
| 7 | + |
| 8 | + cancel(reason: string): void { |
| 9 | + console.log('Action is not allowed in the current state.'); |
| 10 | + } |
| 11 | + |
| 12 | + pay(amount: number): void { |
| 13 | + console.log('Action is not allowed in the current state.'); |
| 14 | + } |
| 15 | + |
| 16 | + message(text: string): void { |
| 17 | + console.log('Action is not allowed in the current state.'); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +class OrderCanceledState extends OrderState { |
| 22 | + cancel(reason: string): void { |
| 23 | + console.log('The order is already canceled.'); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +class OrderPaidState extends OrderState { |
| 28 | + cancel(reason: string): void { |
| 29 | + console.log('Order canceled. The money will be returned.'); |
| 30 | + } |
| 31 | + |
| 32 | + message(text: string): void { |
| 33 | + console.log('Your message has been sent.'); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class SearchingDriverState extends OrderState { |
| 38 | + cancel(reason: string): void { |
| 39 | + console.log('Order canceled. No driver was assigned.'); |
| 40 | + this.context.setState(OrderCanceledState); |
| 41 | + } |
| 42 | + |
| 43 | + pay(amount: number): void { |
| 44 | + console.log('You cannot pay before the car is found.'); |
| 45 | + } |
| 46 | + |
| 47 | + message(text: string): void { |
| 48 | + console.log('You cannot message before the car is found.'); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +class DriverAssignedState extends OrderState { |
| 53 | + cancel(reason: string): void { |
| 54 | + console.log('Order canceled. Informing the driver.'); |
| 55 | + this.context.setState(OrderCanceledState); |
| 56 | + } |
| 57 | + |
| 58 | + pay(amount: number): void { |
| 59 | + console.log(`Payment ${amount} processed. Thank you!`); |
| 60 | + this.context.setState(OrderPaidState); |
| 61 | + } |
| 62 | + |
| 63 | + message(text: string): void { |
| 64 | + console.log('Driver assigned. Your message has been sent.'); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +class RideInProgressState extends OrderState { |
| 69 | + cancel(reason: string): void { |
| 70 | + console.log('You cannot cancel the ride while in progress.'); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +class RideCompletedState extends OrderState { |
| 75 | + cancel(reason: string): void { |
| 76 | + console.log('You cannot cancel the ride after completion.'); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +type StateConstructor = new (context: TaxiOrderContext) => OrderState; |
| 81 | + |
| 82 | +class TaxiOrderContext { |
| 83 | + private state: OrderState; |
| 84 | + |
| 85 | + constructor() { |
| 86 | + this.state = new SearchingDriverState(this); |
| 87 | + } |
| 88 | + |
| 89 | + setState(StateClass: StateConstructor): void { |
| 90 | + this.state = new StateClass(this); |
| 91 | + } |
| 92 | + |
| 93 | + cancel(reason: string): void { |
| 94 | + const state = Object.getPrototypeOf(this.state).constructor.name; |
| 95 | + console.log(`State: ${state}, Cancel: ${reason}`); |
| 96 | + this.state.cancel(reason); |
| 97 | + } |
| 98 | + |
| 99 | + pay(amount: number): void { |
| 100 | + const state = Object.getPrototypeOf(this.state).constructor.name; |
| 101 | + console.log(`State: ${state}, Pay: ${amount}`); |
| 102 | + this.state.pay(amount); |
| 103 | + } |
| 104 | + |
| 105 | + message(text: string): void { |
| 106 | + const state = Object.getPrototypeOf(this.state).constructor.name; |
| 107 | + console.log(`State: ${state}, Message: ${text}`); |
| 108 | + this.state.message(text); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// Usage |
| 113 | + |
| 114 | +const order = new TaxiOrderContext(); |
| 115 | +order.message('Hello'); |
| 116 | +order.cancel('I changed plans'); |
| 117 | + |
| 118 | +const newOrder = new TaxiOrderContext(); |
| 119 | +newOrder.message('Hello'); |
| 120 | +newOrder.setState(DriverAssignedState); |
| 121 | +newOrder.message('Waiting...'); |
| 122 | +newOrder.cancel('No longer needed'); |
| 123 | + |
| 124 | +const ride = new TaxiOrderContext(); |
| 125 | +ride.setState(RideInProgressState); |
| 126 | +ride.message('Hello'); |
| 127 | +ride.pay(50); |
| 128 | +ride.cancel('Return my money'); |
| 129 | +ride.setState(RideCompletedState); |
| 130 | +ride.pay(50); |
| 131 | +ride.cancel('Return my money'); |
0 commit comments