When using mvc in as3.0 to build a project, you can use events at the model layer. In this example, MVC is used to create a simple digital clock function. In the model layer, four methods are used to process event sending. And a simple comparison.
Method 1. The model class inherits from EventDispather.
Method 2. model class composite EventDispather
Method 3. The model class inherits from Sprite.
Method 4: model class implementation IEventDispatcher
The method 1 is set by default for the current code annotation. If you set the corresponding annotation, you can try to use four methods to implement this simple example.
Except method 2 is not good, all other methods are feasible.
Environment: Flash build4, sdk3.5
Code: http://files.cnblogs.com/bigbigdotnet/MVC.rar
Program running effect:
Document class (program entry ):
Main.
Package
{
Import Control .*;
Import Model .*;
Import View .*;
Import flash. display. Sprite;
[SWF (width = "200", height = "100")]
Public class Main extends Sprite
{
Public function Main ()
{
Var model: Model_A = new Model_A ();
// Var model: Model_ B = new Model_ B ();
// Var model: Model_C = new Model_C ();
// Var model: Model_D = new Model_D ();
Var controller: Controller = new Controller (model );
Var view: View = new View (model, controller );
AddChild (view );
View. x = 30;
View. y = 30;
}
}
}
Controller ):
Controller.
Package Control
{
Import flash. utils. Timer;
Import flash. events. TimerEvent;
Import Model .*;
Public class Controller
{
Private var model: Model_A;
// Private var model: Model_ B;
// Private var model: Model_C;
// Private var model: Model_D;
Private var timer: Timer;
Public function Controller (model: Model_A): void {
This. model = model;
}
Public function startTime (): void {
Model. startTime ();
}
}
}
View ):
View.
Package View
{
Import Control .*;
Import Model .*;
Import flash. display. Sprite;
Import flash. events. Event;
Import flash. text. TextField;
Public class View extends Sprite
{
Private var model: Model_A;
// Private var model: Model_ B;
// Private var model: Model_C;
// Private var model: Model_D;
Private var controller: Controller;
Private var tf: TextField;
Public function View (model: Model_A, controller: Controller): void {
This. controller = controller;
Tf = new TextField ();
AddChild (tf );
Controller. startTime ();
This. model = model;
// Used when the Mode layer is Model_A:
Model. addEventListener ("action", onActionHandler );
// Used when the Mode layer is Model_ B:
// Model. getSender (). addEventListener ("action", onActionHandler );
// Used when the Mode layer is Model_C:
// Model. addEventListener ("action", onActionHandler );
// Used when the Mode layer is Model_D:
// Model. addEventListener ("action", onActionHandler );
}
Private function onActionHandler (e: Event): void {
Tf. text = "time:" + model. hour + ":" + model. minutes + ":" + model. second;
Trace(e.tar get );
// When method 2 is used, the following code will return an error! This is because when _ dispather is created in mode_ B, this reference is not injected into _ dispatcher,
// That is to say, mode_ B class reference is not injected into _ dispatcher. The target object of the listener function points to dispatcher instead of mode_ B itself.
// Even if a message is sent in the mode_ B class, you can write dispatcher = new EventDispatcher ();, but the following code returns an error!
// If the IEventDispather interface is implemented in the mode_ B class, write dispatcher = new EventDispatcher (this); the following code is executed correctly.
Trace(e.tar get. hour );
}
}
}
Model ):
Method 1: Model_A.as
Package Model
{
Import flash. events .*;
Import flash. utils. Timer;
// Method 1: Model inherits from EventDispatcher
Public class Model_A extends EventDispatcher
{
Public var hour: String;
Public var minutes: String;
Public var second: String;
Public var timer: Timer;
Public function Model_A (){}
Public function startTime (): void {
Timer = new Timer (1000 );
Timer. addEventListener (TimerEvent. TIMER, onTimerHandler );
Timer. start ();
}
Private function onTimerHandler (e: TimerEvent): void {
Var nowDate: Date = new Date ();
Hour = nowDate. getHours ()> 9? String (nowDate. getHours (): "0" + nowDate. getHours ();
Minutes = nowDate. getMinutes ()> 9? String (nowDate. getMinutes (): "0" + nowDate. getMinutes ();
Second = nowDate. getSeconds ()> 9? String (nowDate. getSeconds (): "0" + nowDate. getSeconds ();
// Directly inherits from EventDispatcher, so the dispatchEvent broadcast event is called directly.
DispatchEvent (new Event ("action "));
}
}
}
Method 2: Model_ B .as
Package Model
{
Import flash. events .*;
Import flash. utils. Timer;
// Method 1: The Model is not inherited from EventDispatcher
Public class Model_ B
{
Public var hour: String;
Public var minutes: String;
Public var second: String;
Public var timer: Timer;
// Define an EventDispatcher object
Public var _ dispatcher: EventDispatcher;
Public function Model_ B (){
InitSender ();
}
Public function startTime (): void {
Timer = new Timer (1000 );
Timer. addEventListener (TimerEvent. TIMER, onTimerHandler );
Timer. start ();
}
Public function getSender (): EventDispatcher {
Return _ dispatcher;
}
Private function initSender (): void {
_ Dispatcher = new EventDispatcher ();
}
Private function onTimerHandler (e: TimerEvent): void {
Var nowDate: Date = new Date ();
Hour = nowDate. getHours ()> 9? String (nowDate. getHours (): "0" + nowDate. getHours ();
Minutes = nowDate. getMinutes ()> 9? String (nowDate. getMinutes (): "0" + nowDate. getMinutes ();
Second = nowDate. getSeconds ()> 9? String (nowDate. getSeconds (): "0" + nowDate. getSeconds ();
// Call _ dispatchEvent to broadcast the event
_ Dispatcher. dispatchEvent (new Event ("action "));
}
}
}
Method 3: Model_C.as
Package Model
{
Import flash. display. Sprite;
Import flash. events .*;
Import flash. utils. Timer;
// Method 3: inherited from Sprite, and Sprite is also a subclass of the EventDispather class,
Public class Model_C extends Sprite
{
Public var hour: String;
Public var minutes: String;
Public var second: String;
Public var timer: Timer;
Public function Model_C (){
}
Public function startTime (): void {
Timer = new Timer (1000 );
Timer. addEventListener (TimerEvent. TIMER, onTimerHandler );
Timer. start ();
}
Private function onTimerHandler (e: TimerEvent): void {
Var nowDate: Date = new Date ();
Hour = nowDate. getHours ()> 9? String (nowDate. getHours (): "0" + nowDate. getHours ();
Minutes = nowDate. getMinutes ()> 9? String (nowDate. getMinutes (): "0" + nowDate. getMinutes ();
Second = nowDate. getSeconds ()> 9? String (nowDate. getSeconds (): "0" + nowDate. getSeconds ();
// Directly call the dispatchEvent broadcast event of the EventDispather class inherited by Spreite
Super. dispatchEvent (new Event ("action "));
}
}
}
Method 4: Model_C.as
Package Model
{
Import flash. events .*;
Import flash. utils. Timer;
// Method 4: Model implements IEventDispatcher directly, but five required methods must be implemented.
Public class Model_D implements IEventDispatcher
{
Public var hour: String;
Public var minutes: String;
Public var second: String;
Public var timer: Timer;
Private var _ dispatcher: EventDispatcher;
Public function Model_D (){
// This is very important. That is, to pass the reference of this class to EventDispatcher.
// The target object of the listening function points to the reference of the Model_D class instead of the _ dispatcher;
_ Dispatcher = new EventDispatcher (this );
}
Public function addEventListener (type: String, listener: Function, useCapture: Boolean = false, priority: int = 0, useWeakReference: Boolean = false): void
{
_ Dispatcher. addEventListener (type, listener, useCapture, priority, useWeakReference );
}
Public function removeEventListener (type: String, listener: Function, useCapture: Boolean = false): void
{
_ Dispatcher. removeEventListener (type, listener, useCapture );
}
Public function dispatchEvent (event: Event): Boolean
{
Return _ dispatcher. dispatchEvent (event );
}
Public function hasEventListener (type: String): Boolean
{
Return _ dispatcher. hasEventListener (type );
}
Public function willTrigger (type: String): Boolean
{
Return _ dispatcher. willTrigger (type );
}
Public function startTime (): void {
Timer = new Timer (1000 );
Timer. addEventListener (TimerEvent. TIMER, onTimerHandler );
Timer. start ();
}
Private function onTimerHandler (e: TimerEvent): void {
Var nowDate: Date = new Date ();
Hour = nowDate. getHours ()> 9? String (nowDate. getHours (): "0" + nowDate. getHours ();
Minutes = nowDate. getMinutes ()> 9? String (nowDate. getMinutes (): "0" + nowDate. getMinutes ();
Second = nowDate. getSeconds ()> 9? String (nowDate. getSeconds (): "0" + nowDate. getSeconds ();
// Call the dispatchEvent broadcast event
_ Dispatcher. dispatchEvent (new Event ("action "));
}
}
}