-
Notifications
You must be signed in to change notification settings - Fork 78
/
Controller.as
183 lines (168 loc) · 6.03 KB
/
Controller.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
PureMVC - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved.
Your reuse is governed by the Creative Commons Attribution 3.0 United States License
*/
package org.puremvc.as3.core
{
import org.puremvc.as3.core.*;
import org.puremvc.as3.interfaces.*;
import org.puremvc.as3.patterns.observer.*;
/**
* A Singleton <code>IController</code> implementation.
*
* <P>
* In PureMVC, the <code>Controller</code> class follows the
* 'Command and Controller' strategy, and assumes these
* responsibilities:
* <UL>
* <LI> Remembering which <code>ICommand</code>s
* are intended to handle which <code>INotifications</code>.</LI>
* <LI> Registering itself as an <code>IObserver</code> with
* the <code>View</code> for each <code>INotification</code>
* that it has an <code>ICommand</code> mapping for.</LI>
* <LI> Creating a new instance of the proper <code>ICommand</code>
* to handle a given <code>INotification</code> when notified by the <code>View</code>.</LI>
* <LI> Calling the <code>ICommand</code>'s <code>execute</code>
* method, passing in the <code>INotification</code>.</LI>
* </UL>
*
* <P>
* Your application must register <code>ICommands</code> with the
* Controller.
* <P>
* The simplest way is to subclass </code>Facade</code>,
* and use its <code>initializeController</code> method to add your
* registrations.
*
* @see org.puremvc.as3.core.view.View View
* @see org.puremvc.as3.patterns.observer.Observer Observer
* @see org.puremvc.as3.patterns.observer.Notification Notification
* @see org.puremvc.as3.patterns.command.SimpleCommand SimpleCommand
* @see org.puremvc.as3.patterns.command.MacroCommand MacroCommand
*/
public class Controller implements IController
{
/**
* Constructor.
*
* <P>
* This <code>IController</code> implementation is a Singleton,
* so you should not call the constructor
* directly, but instead call the static Singleton
* Factory method <code>Controller.getInstance()</code>
*
* @throws Error Error if Singleton instance has already been constructed
*
*/
public function Controller( )
{
if (instance != null) throw Error(SINGLETON_MSG);
instance = this;
commandMap = new Array();
initializeController();
}
/**
* Initialize the Singleton <code>Controller</code> instance.
*
* <P>Called automatically by the constructor.</P>
*
* <P>Note that if you are using a subclass of <code>View</code>
* in your application, you should <i>also</i> subclass <code>Controller</code>
* and override the <code>initializeController</code> method in the
* following way:</P>
*
* <listing>
* // ensure that the Controller is talking to my IView implementation
* override public function initializeController( ) : void
* {
* view = MyView.getInstance();
* }
* </listing>
*
* @return void
*/
protected function initializeController( ) : void
{
view = View.getInstance();
}
/**
* <code>Controller</code> Singleton Factory method.
*
* @return the Singleton instance of <code>Controller</code>
*/
public static function getInstance() : IController
{
if ( instance == null ) instance = new Controller( );
return instance;
}
/**
* If an <code>ICommand</code> has previously been registered
* to handle a the given <code>INotification</code>, then it is executed.
*
* @param note an <code>INotification</code>
*/
public function executeCommand( note : INotification ) : void
{
var commandClassRef : Class = commandMap[ note.getName() ];
if ( commandClassRef == null ) return;
var commandInstance : ICommand = new commandClassRef();
commandInstance.execute( note );
}
/**
* Register a particular <code>ICommand</code> class as the handler
* for a particular <code>INotification</code>.
*
* <P>
* If an <code>ICommand</code> has already been registered to
* handle <code>INotification</code>s with this name, it is no longer
* used, the new <code>ICommand</code> is used instead.</P>
*
* The Observer for the new ICommand is only created if this the
* first time an ICommand has been regisered for this Notification name.
*
* @param notificationName the name of the <code>INotification</code>
* @param commandClassRef the <code>Class</code> of the <code>ICommand</code>
*/
public function registerCommand( notificationName : String, commandClassRef : Class ) : void
{
if ( commandMap[ notificationName ] == null ) {
view.registerObserver( notificationName, new Observer( executeCommand, this ) );
}
commandMap[ notificationName ] = commandClassRef;
}
/**
* Check if a Command is registered for a given Notification
*
* @param notificationName
* @return whether a Command is currently registered for the given <code>notificationName</code>.
*/
public function hasCommand( notificationName:String ) : Boolean
{
return commandMap[ notificationName ] != null;
}
/**
* Remove a previously registered <code>ICommand</code> to <code>INotification</code> mapping.
*
* @param notificationName the name of the <code>INotification</code> to remove the <code>ICommand</code> mapping for
*/
public function removeCommand( notificationName : String ) : void
{
// if the Command is registered...
if ( hasCommand( notificationName ) )
{
// remove the observer
view.removeObserver( notificationName, this );
// remove the command
commandMap[ notificationName ] = null;
}
}
// Local reference to View
protected var view : IView;
// Mapping of Notification names to Command Class references
protected var commandMap : Array;
// Singleton instance
protected static var instance : IController;
// Message Constants
protected const SINGLETON_MSG : String = "Controller Singleton already constructed!";
}
}