Skip to content

Instantly share code, notes, and snippets.

@PanJarda
Last active December 8, 2020 06:18
Show Gist options
  • Save PanJarda/a6e68049296901c279f05892eb43f7c3 to your computer and use it in GitHub Desktop.
Save PanJarda/a6e68049296901c279f05892eb43f7c3 to your computer and use it in GitHub Desktop.
Design patterns: State
"use strict"
var DesignPatterns = {};
DesignPatterns.Utils = (function() {
function BadMethodCallException(message) {
this.message = message;
this.name = "BadMethodCallException";
}
function Iterable() {}
Iterable.prototype.rewind = function() {};
Iterable.prototype.key = function() {};
Iterable.prototype.next = function() {};
Iterable.prototype.valid = function() {};
Iterable.prototype.getCurrent = function() {};
function ListItem(item) {
this.item = item;
this.next = null;
}
function List() {
Iterable.call(this);
this.first = new ListItem(null);
this.last = this.first;
this.current = this.first;
}
List.prototype = new Iterable();
List.prototype.add = function(item) {
this.last.next = new ListItem(item);
this.last = this.last.next;
return this;
};
List.prototype.rewind = function() {
this.current = this.first;
return this;
}
List.prototype.getCurrent = function() {
return this.current.item;
};
List.prototype.next = function() {
if (this.current.next === null) {
return null;
}
this.current = this.current.next;
return this.getCurrent();
}
return {
List: List
};
})();
DesignPatterns.State = (function()
{
var Printers = (function()
{
function PrinterState(printer) {
this.printer = printer;
}
function OfflinePrinterState(printer) {
PrinterState.call(this, printer);
}
OfflinePrinterState.prototype.plugIn = function() {
this.printer.setState(this.printer.states.PLUGED_IN);
console.log("Tiskárna zapojena do sítě.");
};
OfflinePrinterState.prototype.turnOn = function() {
throw new BadMethodCallException("Tiskárna nebyla zapojena!");
};
OfflinePrinterState.prototype.warmUp = function() {
throw new BadMethodCallException("Tiskárna nebyla zapnuta!");
};
OfflinePrinterState.prototype.turnOff = function() {
throw new BadMethodCallException("Tiskárna nebyla zapnuta!");
};
OfflinePrinterState.prototype.unplug = function() {
throw new BadMethodCallException("Tiskárna nebyla zapojena!");
};
function PlugedInPrinterState(printer) {
PrinterState.call(this, printer);
}
PlugedInPrinterState.prototype.plugIn = function() {
throw new BadMethodCallException("Tiskárna již byla zapojena!");
};
PlugedInPrinterState.prototype.turnOn = function() {
this.printer.setState(this.printer.states.TURNED_ON);
console.log("Tiskárna zapnuta.");
};
PlugedInPrinterState.prototype.warmUp = function() {
throw new BadMethodCallException("Tiskárna nebyla zapnuta!");
};
PlugedInPrinterState.prototype.turnOff = function() {
throw new BadMethodCallException("Tiskárna nebyla zapnuta!");
};
PlugedInPrinterState.prototype.unplug = function() {
this.printer.setState(this.printer.states.OFFLINE);
console.log("Tiskárna odpojena.");
};
function TurnedOnPrinterState(printer) {
PrinterState.call(this, printer);
}
TurnedOnPrinterState.prototype.plugIn = function() {
throw new BadMethodCallException("Tiskárna již byla zapojena!");
};
TurnedOnPrinterState.prototype.turnOn = function() {
throw new BadMethodCallException("Tiskárna již byla zapnuta!");
};
TurnedOnPrinterState.prototype.warmUp = function() {
this.printer.setState(this.printer.states.WARMED_UP);
console.log("Tiskárna rozehřáta.");
};
TurnedOnPrinterState.prototype.turnOff = function() {
this.printer.setState(this.printer.states.PLUGED_IN);
console.log("Tiskárna vypnuta.");
};
TurnedOnPrinterState.prototype.unplug = function() {
throw new BadMethodCallException("Tiskárna je stále zapnutá!");
};
function WarmedUpPrinterState(printer) {
PrinterState.call(this, printer);
}
WarmedUpPrinterState.prototype.plugIn = function() {
throw new BadMethodCallException("Tiskárna již byla zapojena!");
};
WarmedUpPrinterState.prototype.turnOn = function() {
throw new BadMethodCallException("Tiskárna již byla zapnuta!");
};
WarmedUpPrinterState.prototype.warmUp = function() {
throw new BadMethodCallException("Tiskárna již byla rozehřáta!");
};
WarmedUpPrinterState.prototype.turnOff = function() {
this.printer.setState(this.printer.states.PLUGED_IN);
console.log("Tiskárna vypnuta.");
};
WarmedUpPrinterState.prototype.unplug = function() {
throw new BadMethodCallException("Tiskárna je stále zapnutá!");
};
function Printer(type) {
this.states = {
OFFLINE: new OfflinePrinterState(this),
PLUGED_IN: new PlugedInPrinterState(this),
TURNED_ON: new TurnedOnPrinterState(this),
WARMED_UP: new WarmedUpPrinterState(this)
};
this.state = this.states.OFFLINE;
this.type = type;
}
Printer.prototype.plugIn = function() {
this.state.plugIn();
return this;
};
Printer.prototype.turnOn = function() {
this.state.turnOn();
return this;
};
Printer.prototype.warmUp = function() {
this.state.warmUp();
return this;
};
Printer.prototype.turnOff = function() {
this.state.turnOff();
return this;
};
Printer.prototype.unplug = function() {
this.state.unplug();
return this;
};
Printer.prototype.getType = function() {
return this.type;
};
Printer.prototype.setState = function(state) {
this.state = state;
return this;
};
return {
Printer: Printer
};
})();
var Test = {}
Test.run = function() {
var printer = new Printers.Printer("HP LaserJet 10000");
printer.plugIn()
.turnOn()
.warmUp()
.turnOff()
.unplug();
};
return {
Test: Test
};
})();
/* test */
DesignPatterns.State.Test.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment