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