Skip to content

Instantly share code, notes, and snippets.

View Sean-Bradley's full-sized avatar
📰
https://sbcode.net

SBCODE Sean-Bradley

📰
https://sbcode.net
View GitHub Profile
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"resolveJsonModule": true,
"esModuleInterop": true,
"outDir": "dist",
"sourceMap": true
},
"files": [
class IProtoType(metaclass=ABCMeta):
"""interface with clone method"""
@abstractstaticmethod
def clone():
"""The clone, deep or shallow, is up to how you
want implement the details in your concrete class?"""
class IUndoRedo(metaclass=ABCMeta):
"""The Undo Redo interface"""
@abstractstaticmethod
def history():
"""the history of the states"""
@abstractstaticmethod
def undo():
"""for undoing the hsitory of the states"""
def execute(self, command_name, *args):
"""Execute a pre defined command and log in history"""
if command_name in self._commands.keys():
self._history_position += 1
self._commands[command_name].execute(args)
if len(self._history) == self._history_position:
# This is a new event in hisory
self._history.append((time.time(), command_name, args))
else:
# This occurs if there was one of more UNDOs and then a new
@property
def history(self):
"""Return all records in the History list"""
return self._history
def undo(self):
"""Undo a command if there is a command that can be undone.
Update the history psoition so that further UNDOs or REDOs
point to the correct index"""
if self._history_position > 0:
class Slider(IUndoRedo):
"""The Invoker Class"""
def __init__(self):
self._commands = {}
self._history = [(0.0, "OFF", ())] # A default setting of OFF
self._history_position = 0 # The position that is used for UNDO/REDO
...
if __name__ == "__main__":
# The Client is the main python app
# The Light is the Reciever
LIGHT = Light()
# Create Commands
SWITCH_ON = SwitchOnCommand(LIGHT)
SWITCH_OFF = SwitchOffCommand(LIGHT)
class Light:
"""The Reciever"""
def turn_on(self):
print("Light turned ON")
def turn_off(self):
print("Light turned OFF")
class Switch:
"""The Invoker Class"""
def __init__(self):
self._commands = {}
self._history = []
class ICommand(metaclass=ABCMeta):
"""The command interface, which all commands will implement"""
@abstractstaticmethod
def execute():
"""The required execute method which all command obejcts will use"""
class SwitchOnCommand(ICommand):
"""A Command object, which implemets the ICommand interface"""