0% found this document useful (0 votes)
3 views1 page

Vending Machine State Pattern Implementation

Uploaded by

iheb.zenkri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Vending Machine State Pattern Implementation

Uploaded by

iheb.zenkri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

State Pattern

public class VendingMachineContext {


private VendingMachineState state; class ReadyState implements VendingMachineState {
@Override
constructor() { public void handleRequest() {
[Link] = null; [Link]("Ready state: Please select a product.");
} }
}
setState(state) {
[Link] = state; class ProductSelectedState implements VendingMachineState {
} @Override
public void handleRequest() {
request() { [Link]("Product selected state: Processing
[Link](); payment.");
} }
} }

public interface VendingMachineState { class PaymentPendingState implements VendingMachineState {


handleRequest() ; @Override
} public void handleRequest() {
[Link]("Payment pending state: Dispensing
public class VendingMachine { product.");
private VendingMachineState currentState; }
}
public VendingMachine() {
[Link] = new ReadyState(); class OutOfStockState implements VendingMachineState {
} @Override
public void handleRequest() {
public void setState(VendingMachineState state) { [Link]("Out of stock state: Product unavailable.
[Link] = state; Please select another product.");
} }
}
public void handleRequest() {
[Link]();
}
}

You might also like