class RemoteControlWithUndo: def __init__(self): self.onCommands = [NoCommand()] * 7 self.offCommands = [NoCommand()] * 7 self.undoCommand = NoCommand() def setCommand(self, slot, onCommand, offCommand): # type: (int, Command, Command) -> None assert isinstance(onCommand, Command) assert isinstance(offCommand, Command) self.onCommands[slot] = onCommand self.offCommands[slot] = offCommand def onButtonWasPushed(self, slot): self.onCommands[slot].execute() self.undoCommand = self.onCommands[slot] def offButtonWasPushed(self, slot): self.offCommands[slot].execute() self.undoCommand = self.offCommands[slot] def undoButtonWasPushed(self): self.undoCommand.undo() def __str__(self): stringBuffer = "\n------- Remote Control -------\n" index = 0 for command in self.onCommands: stringBuffer += "[slot " + str(index) + "]" + command.__class__.__name__ + ",\t " + \ self.offCommands[index].__class__.__name__ + "\n" index += 1 stringBuffer += "[undo]" + self.undoCommand.__class__.__name__ + "\n" return stringBuffer
class RemoteControl(): SLOT_COUNT = 7 def __init__(self): self.on_commands = [] self.off_commands = [] self.undo_command = NoCommand() for i in range(0, RemoteControl.SLOT_COUNT): self.on_commands.append(NoCommand()) self.off_commands.append(NoCommand()) def set_command(self, slot, on_command, off_command): if slot >= RemoteControl.SLOT_COUNT: print('Slot Range is 0 ~ 6') return self.on_commands[slot] = on_command self.off_commands[slot] = off_command def on_button_pushed(self, slot): self.on_commands[slot].execute() self.undo_command = self.on_commands[slot] def off_button_pushed(self, slot): self.off_commands[slot].execute() self.undo_command = self.off_commands[slot] def undo_button_pushed(self): self.undo_command.undo()
def __init__(self): self.on_commands = [] self.off_commands = [] self.undo_command = NoCommand() for i in range(0, RemoteControl.SLOT_COUNT): self.on_commands.append(NoCommand()) self.off_commands.append(NoCommand())
def __init__(self): self.onCommands = [] self.offCommands = [] noCommand = NoCommand() for i in range(7): self.onCommands.append(noCommand) self.offCommands.append(noCommand) self.undoCommand = noCommand
from command import Command, NoCommand no_command = NoCommand() # NullObject pattern is here. # It is used when we don't have anything to return # and to remove the responsibility from the client to handle None value class Remote: def __init__(self, capacity): self.capacity = capacity self.on_commands: list[Command] = [ no_command for _ in range(0, self.capacity) ] # NullObject self.off_commands: list[Command] = [ no_command for _ in range(0, self.capacity) ] # NullObject self.recent_command: Command = no_command # NullObject def set_command(self, position: int, on_command: Command, off_command: Command): self.on_commands[position] = on_command self.off_commands[position] = off_command def on_button_was_pushed(self, position: int): self.on_commands[position].execute() self.recent_command = self.on_commands[position] def off_button_was_pushed(self, position: int): self.off_commands[position].execute() self.recent_command = self.off_commands[position]
def __init__(self): self.onCommands = [NoCommand()] * 7 self.offCommands = [NoCommand()] * 7 self.undoCommand = NoCommand()