This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"compilerOptions": { | |
"target": "es2017", | |
"module": "commonjs", | |
"resolveJsonModule": true, | |
"esModuleInterop": true, | |
"outDir": "dist", | |
"sourceMap": true | |
}, | |
"files": [ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?""" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
... | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Light: | |
"""The Reciever""" | |
def turn_on(self): | |
print("Light turned ON") | |
def turn_off(self): | |
print("Light turned OFF") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Switch: | |
"""The Invoker Class""" | |
def __init__(self): | |
self._commands = {} | |
self._history = [] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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""" |