Class: PureMVC::Facade
- Inherits:
-
Object
- Object
- PureMVC::Facade
- Defined in:
- src/patterns/facade/facade.rb
Overview
A base Multiton IFacade implementation.
Constant Summary collapse
- MULTITON_MSG =
Message Constants
'Facade instance for this Multiton key already constructed!'
Class Method Summary collapse
-
.get_instance(key, &factory) ⇒ IFacade
Facade Multiton Factory method.
-
.has_core?(key) ⇒ Boolean
Check if a Core is registered or not.
-
.instance_map ⇒ Hash{String => IFacade}
The Multiton IFacade instanceMap.
-
.mutex ⇒ Mutex
Mutex used to synchronize access to the instance map for thread safety.
-
.remove_core(key) ⇒ Object
Remove a Core.
Instance Method Summary collapse
-
#has_command?(notification_name) ⇒ Boolean
Check if a Command is registered for a given Notification.
-
#has_mediator?(mediator_name) ⇒ Boolean
Check if a Mediator is registered or not.
-
#has_proxy?(proxy_name) ⇒ Boolean
Check if a Proxy is registered.
-
#initialize(key) ⇒ Facade
constructor
Constructor.
-
#initialize_controller ⇒ Object
Initialize the
Controller. -
#initialize_facade ⇒ Object
Initialize the Multiton
Facadeinstance. -
#initialize_model ⇒ Object
Initialize the
Model. -
#initialize_notifier(key) ⇒ Object
Set the Multiton key for this facade instance.
-
#initialize_view ⇒ Object
Initialize the
View. -
#notify_observers(notification) ⇒ Object
Notify
Observers. -
#register_command(notification_name, &factory) ⇒ Object
Register an
ICommandwith theControllerby Notification name. -
#register_mediator(mediator) ⇒ Object
Register an
IMediatorwith theView. -
#register_proxy(proxy) ⇒ Object
Register an
IProxywith theModelby name. -
#remove_command(notification_name) ⇒ Object
Remove a previously registered
ICommandtoINotificationmapping from the Controller. -
#remove_mediator(mediator_name) ⇒ IMediator?
Remove an
IMediatorfrom theView. -
#remove_proxy(proxy_name) ⇒ IProxy?
Remove an
IProxyfrom theModelby name. -
#retrieve_mediator(mediator_name) ⇒ IMediator?
Retrieve an
IMediatorfrom theView. -
#retrieve_proxy(proxy_name) ⇒ IProxy?
Retrieve an
IProxyfrom theModelby name. -
#send_notification(name, body = nil, type = nil) ⇒ Object
Create and send an
INotification.
Constructor Details
#initialize(key) ⇒ Facade
Constructor.
This IFacade implementation is a Multiton, so you should not call the constructor directly. Instead, use the static factory method and pass the unique key for this instance with factory: PureMVC::Facade.get_instance(key) { |key| PureMVC::Facade.new(key) }.
72 73 74 75 76 77 78 79 |
# File 'src/patterns/facade/facade.rb', line 72 def initialize(key) raise MULTITON_MSG if self.class.instance_map[key] self.class.instance_map[key] = self @model = @view = @controller = nil initialize_notifier(key) initialize_facade end |
Class Method Details
.get_instance(key, &factory) ⇒ IFacade
Facade Multiton Factory method.
34 35 36 37 38 |
# File 'src/patterns/facade/facade.rb', line 34 def get_instance(key, &factory) mutex.synchronize do instance_map[key] ||= factory.call(key) end end |
.has_core?(key) ⇒ Boolean
Check if a Core is registered or not.
44 45 46 |
# File 'src/patterns/facade/facade.rb', line 44 def has_core?(key) instance_map.key?(key) end |
.instance_map ⇒ Hash{String => IFacade}
The Multiton IFacade instanceMap.
23 |
# File 'src/patterns/facade/facade.rb', line 23 def instance_map = (@instance_map ||= {}) |
.mutex ⇒ Mutex
Mutex used to synchronize access to the instance map for thread safety.
27 |
# File 'src/patterns/facade/facade.rb', line 27 def mutex = (@mutex ||= Mutex.new) |
.remove_core(key) ⇒ Object
Remove a Core.
Removes the Model, View, Controller, and Facade instances associated with the given key.
54 55 56 57 58 59 60 61 |
# File 'src/patterns/facade/facade.rb', line 54 def remove_core(key) mutex.synchronize do Model.remove_model(key) View.remove_view(key) Controller.remove_controller(key) instance_map.delete(key) end end |
Instance Method Details
#has_command?(notification_name) ⇒ Boolean
Check if a Command is registered for a given Notification
158 159 160 |
# File 'src/patterns/facade/facade.rb', line 158 def has_command?(notification_name) !!@controller&.has_command?(notification_name) end |
#has_mediator?(mediator_name) ⇒ Boolean
Check if a Mediator is registered or not
219 220 221 |
# File 'src/patterns/facade/facade.rb', line 219 def has_mediator?(mediator_name) !!@view&.has_mediator?(mediator_name) end |
#has_proxy?(proxy_name) ⇒ Boolean
Check if a Proxy is registered
188 189 190 |
# File 'src/patterns/facade/facade.rb', line 188 def has_proxy?(proxy_name) !!@model&.has_proxy?(proxy_name) end |
#initialize_controller ⇒ Object
Initialize the Controller.
Called by the initialize_facade method.
Override this method in your subclass of Facade if one or both of the following are true:
-
You wish to initialize a different
IController. -
You have
Commandsto register with theControllerat startup.
If you don’t want to initialize a differentIController,callsuper.initialize_controller() at the beginning of your method, then register Commands.
103 104 105 |
# File 'src/patterns/facade/facade.rb', line 103 def initialize_controller @controller = Controller.get_instance(@multiton_key) { |key| Controller.new(key) } end |
#initialize_facade ⇒ Object
Be sure to call super.initialize_facade when overriding.
Initialize the Multiton Facade instance.
This method is called automatically by the constructor. Override it in your subclass to perform any subclass-specific initialization.
87 88 89 90 91 |
# File 'src/patterns/facade/facade.rb', line 87 def initialize_facade initialize_model initialize_controller initialize_view end |
#initialize_model ⇒ Object
Initialize the Model.
Called by the initializeFacade method.
Override this method in your subclass of Facade if one or both of the following are true:
-
You wish to initialize a different
IModel. -
You have
Proxys to register with the Model that do not retrieve a reference to theFacadeat construction time.
If you don’t want to initialize a different IModel, call super.initialize_model() at the beginning of your method, then register Proxys.
Note: This method is rarely overridden; in practice you are more likely to use a Command to create and register Proxys with the Model, since Proxys with mutable data will likely need to send INotifications and thus will likely want to fetch a reference to the Facade during their construction.
123 124 125 |
# File 'src/patterns/facade/facade.rb', line 123 def initialize_model @model = Model.get_instance(@multiton_key) { |key| Model.new(key) } end |
#initialize_notifier(key) ⇒ Object
Set the Multiton key for this facade instance.
This is not meant to be called directly. It is invoked internally by the constructor when get_instance is called. However, it must be public to implement INotifier.
263 264 265 |
# File 'src/patterns/facade/facade.rb', line 263 def initialize_notifier(key) @multiton_key = key end |
#initialize_view ⇒ Object
Initialize the View.
Called by the initializeFacade method.
Override this method in your subclass of Facade if one or both of the following are true:
-
You wish to initialize a different
IView. -
You have
Observersto register with theView.
If you don’t want to initialize a different IView, call super.initialize_view() at the beginning of your method, then register IMediator instances.
Note: This method is rarely overridden; in practice you are more likely to use a Command to create and register Mediators with the View, since IMediator instances will need to send INotifications and thus will likely want to fetch a reference to the Facade during their construction.
142 143 144 |
# File 'src/patterns/facade/facade.rb', line 142 def initialize_view @view = View.get_instance(@multiton_key) { |key| View.new(key) } end |
#notify_observers(notification) ⇒ Object
Notify Observers.
This method is left public mostly for backward compatibility and to allow you to send custom notification classes using the facade.
Usually you should call send_notification and pass the parameters, never having to construct the notification yourself.
241 242 243 |
# File 'src/patterns/facade/facade.rb', line 241 def notify_observers(notification) @view&.notify_observers(notification) end |
#register_command(notification_name, &factory) ⇒ Object
Register an ICommand with the Controller by Notification name.
150 151 152 |
# File 'src/patterns/facade/facade.rb', line 150 def register_command(notification_name, &factory) @controller&.register_command(notification_name, &factory) end |
#register_mediator(mediator) ⇒ Object
Register an IMediator with the View.
203 204 205 |
# File 'src/patterns/facade/facade.rb', line 203 def register_mediator(mediator) @view&.register_mediator(mediator) end |
#register_proxy(proxy) ⇒ Object
Register an IProxy with the Model by name.
172 173 174 |
# File 'src/patterns/facade/facade.rb', line 172 def register_proxy(proxy) @model&.register_proxy(proxy) end |
#remove_command(notification_name) ⇒ Object
Remove a previously registered ICommand to INotification mapping from the Controller.
165 166 167 |
# File 'src/patterns/facade/facade.rb', line 165 def remove_command(notification_name) @controller&.remove_command(notification_name) end |
#remove_mediator(mediator_name) ⇒ IMediator?
Remove an IMediator from the View.
227 228 229 |
# File 'src/patterns/facade/facade.rb', line 227 def remove_mediator(mediator_name) @view&.remove_mediator(mediator_name) end |
#remove_proxy(proxy_name) ⇒ IProxy?
Remove an IProxy from the Model by name.
196 197 198 |
# File 'src/patterns/facade/facade.rb', line 196 def remove_proxy(proxy_name) @model&.remove_proxy(proxy_name) end |
#retrieve_mediator(mediator_name) ⇒ IMediator?
Retrieve an IMediator from the View.
211 212 213 |
# File 'src/patterns/facade/facade.rb', line 211 def retrieve_mediator(mediator_name) @view&.retrieve_mediator(mediator_name) end |
#retrieve_proxy(proxy_name) ⇒ IProxy?
Retrieve an IProxy from the Model by name.
180 181 182 |
# File 'src/patterns/facade/facade.rb', line 180 def retrieve_proxy(proxy_name) @model&.retrieve_proxy(proxy_name) end |
#send_notification(name, body = nil, type = nil) ⇒ Object
Create and send an INotification.
Keeps us from having to construct new notification instances in our implementation code.
252 253 254 |
# File 'src/patterns/facade/facade.rb', line 252 def send_notification(name, body = nil, type = nil) notify_observers(Notification.new(name, body, type)) end |