Package puremvc :: Package patterns :: Module command
[hide private]
[frames] | no frames]

Source Code for Module puremvc.patterns.command

  1  """ 
  2   PureMVC Python Port by Toby de Havilland <toby.de.havilland@puremvc.org>  
  3   PureMVC - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved.  
  4   Your reuse is governed by the Creative Commons Attribution 3.0 License  
  5  """ 
  6   
  7  import puremvc.interfaces 
  8  import puremvc.patterns.observer 
  9   
10 -class MacroCommand(puremvc.patterns.observer.Notifier, puremvc.interfaces.ICommand, puremvc.interfaces.INotifier):
11 """ 12 A base C{ICommand} implementation that executes other C{ICommand}s. 13 14 A C{MacroCommand} maintains an list of C{ICommand} Class references called I{SubCommands}. 15 16 When C{execute} is called, the C{MacroCommand} 17 instantiates and calls C{execute} on each of its I{SubCommands} turn. 18 Each I{SubCommand} will be passed a reference to the original 19 C{INotification} that was passed to the C{MacroCommand}'s 20 C{execute} method. 21 22 Unlike C{SimpleCommand}, your subclass 23 should not override C{execute}, but instead, should 24 override the C{initializeMacroCommand} method, 25 calling C{addSubCommand} once for each I{SubCommand} 26 to be executed. 27 28 @see: L{Controller<puremvc.core.controller.Controller>} 29 @see: L{Notification<puremvc.patterns.observer.Notification>} 30 @see: L{SimpleCommand<puremvc.patterns.command.SimpleCommand>} 31 """ 32 33 subCommands = None 34
35 - def __init__(self):
36 """ 37 MacroCommand Constructor 38 39 You should not need to define a constructor, 40 instead, override the C{initializeMacroCommand} 41 method. 42 """ 43 self.subCommands = [] 44 self.initializeMacroCommand()
45
46 - def initializeMacroCommand(self):
47 """ 48 Initialize the C{MacroCommand}. 49 50 In your subclass, override this method to 51 initialize the C{MacroCommand}'s I{SubCommand} 52 list with C{ICommand} class references like 53 this: 54 55 Note that I{SubCommand}s may be any C{ICommand} implementor, 56 C{MacroCommand}s or C{SimpleCommands} are both acceptable. 57 """ 58 pass
59
60 - def addSubCommand(self, commandClassRef):
61 """ 62 Add a I{SubCommand}. 63 64 The I{SubCommands} will be called in First In/First Out (FIFO) 65 order. 66 67 @param commandClassRef: a reference to the C{Class} of the C{ICommand}. 68 """ 69 self.subCommands.append(commandClassRef)
70 71
72 - def execute(self, notification):
73 """ 74 Execute this C{MacroCommand}'s I{SubCommands}. 75 76 The I{SubCommands} will be called in First In/First Out (FIFO) 77 order. 78 79 @param notification: the C{INotification} object to be passsed to each I{SubCommand}. 80 """ 81 while len(self.subCommands) > 0: 82 commandClassRef = self.subCommands.pop(0) 83 commandInstance = commandClassRef() 84 commandInstance.execute(notification)
85 86
87 -class SimpleCommand(puremvc.patterns.observer.Notifier, puremvc.interfaces.ICommand, puremvc.interfaces.INotifier):
88 """ 89 A base C{ICommand} implementation. 90 91 Your subclass should override the C{execute} 92 method where your business logic will handle the C{INotification}. 93 94 @see: L{Controller<puremvc.core.controller.Controller>} 95 @see: L{Notification<puremvc.patterns.observer.Notification>} 96 @see: L{MacroCommand<puremvc.patterns.command.MacroCommand>} 97 """ 98
99 - def execute(self, notification):
100 """ 101 Fulfill the use-case initiated by the given C{INotification}. 102 103 In the Command Pattern, an application use-case typically 104 begins with some user action, which results in an C{INotification} being broadcast, which 105 is handled by business logic in the C{execute} method of an 106 C{ICommand}. 107 108 @param notification: the C{INotification} to handle. 109 """ 110 pass
111