org::puremvc::perl5::patterns::mediator::Mediator
inherits:
Base pureMVC Mediator
concept implementation.
A concrete Mediator
implementor usually looks something like this:
package com::me::myapp::mediators::AMediator;
use strict; use warnings;
use org::puremvc::perl5::patterns::mediator::Mediator; our @ISA = qw( org::puremvc::perl5::patterns::mediator::Mediator );
#********************** #********************** sub listNotificationInterests { return ["ANotification"]; }
#********************** sub handleNotification { my ( $self, $note ) = @_; my $note_name = $note->getName(); return unless $note_name eq "ANotification"; print "AMediator reacted to notification ANotification\n"; }
#********************** sub onRegister { print "AMediator registered\n"; }
#********************** sub onRemove { print "AMediator removed\n"; }
#********************** #********************** 1;
In PureMVC, Mediator
implementors assume these responsibilities:
Implement a common method which returns a list of all notifications the Mediator
has interest in.
Implement a notification callback method.
Implement methods that are called when the Mediator
is registered or removed from the View.
Additionally, mediators typically:
Act as an intermediary between one or more view components ( typically server scripts, external to your application).
In server environments where event concept is implemented, this is often the place where event listeners are added to view components, and their handlers implemented.
Respond to and generate notifications, interacting with of the rest of the PureMVC application.
When a Mediator
is registered with the View
, the View
will call the Mediator
's listNotificationInterests
method. The Mediator
will return an array of notification names which it wishes to be notified about.
The View
will then create an Observer object encapsulating that Mediator
's (handleNotification
) method and register it as an observer for each notification name returned by listNotificationInterests
.
sub new( $mediator_name, $view_component );
It is common to define a default name for your Mediator
class implementation by mean of a constant:
use constant NAME => "com::me::myapp::mediators::AMediator";
To register an instance of your Mediator
class you can then do so:
use com::me::myapp::mediators::AMediator; my $facade = org::puremvc::perl5::patterns::facade::Facade->getInstance();
my $mediator = com::me::myapp::mediators::AMediator->new( com::me::myapp::mediators::AMediator::NAME ); $facade->registerMediator($mediator);
It is possible to register several instances of the same Mediator
class. You must then give different names to each instance registration in order to fully qualify them in your application:
use com::me::myapp::mediators::AMediator; my $facade = org::puremvc::perl5::patterns::facade::Facade->getInstance();
my $mediator = com::me::myapp::mediators::AMediator->new( com::me::myapp::mediators::AMediator::NAME ); $facade->registerMediator($mediator);
my $other_mediator = com::me::myapp::mediators::AMediator->new( "SOME_OTHER_NAME" ); $facade->registerMediator($other_mediator);
Parameters
$mediator_name - String
: name by which any retrieval, removal, or existence checking will be achieved in your application.
$view_component - *
: object instance representing an external component handled by your Mediator
- Optional.
Returns the name by which a Mediator
was registered with the View
.
Returns
String
- The name by which a Mediator
was registered with the View
.
Returns the Mediator's
view component.
Returns
*
- The view component.
sub setViewComponent( $view_component );
Sets the Mediator's
view component.
Parameters
$view_component - *
Any object representing the view component.
sub listNotificationInterests();
Returns an array reference holding the notification names the Mediator
is interested in being notified of.
When your application will dispatch a notification whose name matches one of those in the array, the handleNotification
method of the Mediator
will be automatically called.
Returns
\ARRAY
- The reference on the array holding the notifications' name.
sub handleNotification( $notification );
This method is automatically called by the view if $notification
's name is one of those returned by listNotificationInterests
method.
Parameters
$notification - org::puremvc::perl5::patterns::observer::Notification
The notification to handle.
Called by the view when the Mediator
is registered.
Called by the view when the Mediator
is removed.
org::puremvc::perl5::core::Model
org::puremvc::perl5::core::View
org::puremvc::perl5::core::Controller
org::puremvc::perl5::patterns::facade::Facade
org::puremvc::perl5::patterns::observer::Notification
org::puremvc::perl5::patterns::proxy::Proxy
org::puremvc::perl5::patterns::command::SimpleCommand
org::puremvc::perl5::patterns::command::MacroCommand