명령들을 추상화해서 class로 만들어 객체로 사용하는 패턴
class Command:
def execute(self):
pass
class PrintCommand(Command):
def __init__(self, print_str:str):
self.print_str = print_str
def execute(self):
print(f"from print command : {self.print_str}")
from typing import List
class Dog:
def sit(self):
print("The dog sat down")
def stay(self):
print("The dog is staying")
class DogCommand(Command):
#prefer enums
def __init__(self, dog:Dog,commands:List[str]):
self.dog = dog
self.commands = commands
def execute(self):
for command in self.commands:
if command == 'sit':
self.dog.sit()
elif command == 'stay':
self.dog.stay()
class Invoker:
def __init__(self):
self.command_list = []
def addCommand(self, command:Command):
self.command_list.append(command)
def runCommands(self):
for command in self.command_list:
command.execute()