commandpattern.py

来自「this is the most basic to learn python」· Python 代码 · 共 39 行

PY
39
字号
#: c06:CommandPattern.py

class Command:
  def execute(self): pass

class Loony(Command):
  def execute(self):
    print "You're a loony."

class NewBrain(Command):
  def execute(self):
    print "You might even need a new brain."

class Afford(Command):
  def execute(self):
    print "I couldn't afford a whole new brain."

# An object that holds commands:
class Macro:
  def __init__(self):
    self.commands = []
  def add(self, command): 
    self.commands.append(command) 
  def run(self):
    for c in self.commands:
      c.execute()

macro = Macro()
macro.add(Loony())
macro.add(NewBrain())
macro.add(Afford())
macro.run()
#<hr>
output = '''
You're a loony.
You might even need a new brain.
I couldn't afford a whole new brain.
'''

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?