Looking at the "Essential.Actionscript.2.0", the book is written, and the book written by the authority of ActionScript. Note, the chapter of the MVC mode. The MVC is not a new concept, which is now popular in SmallTalk, and the Document / View structure in C is also this principle. Change the contents of the Document from the View response through the message map. Look at MOCK's writing, you know that he is a java background, including the OBERSERABLE class and OBERSERVER interface, and the implementation of the MVC is simply a traditional Java Writing.
About MVC, here there is an authority instructions: http://st-www.cs.uiuc.edu/Users/smarch/st-docs/mvc.html
I also use the MOOCK method, in accordance with the original Java example, a sputum temperature and a Celsius temperature display. The models used in the models are the same, but the performance of View is different. This is the advantage of MVC. There is also a VIEW displayed with a thermometer, you can use my COM.FLASHVAN.GRAPHIC class to be implemented, and then put the source code together next time. Utils package and the content inside the MVC package I will omit, you can download http://mock.org/eas2/examples/eas2_mvcclock.zip
Pre {Font-Family: "Courier New", Courier, Arial; font-size: 12px;}
. galan {color: # 000000;
. readyword {color: # 993300;
.IDENTIFIER {color: # 000087;
.properties {color: # 000087;
{Color: # 000087;
.LINECMMMENT, .BLOCKCOMMENT {Color: # 808080;
. String {color: # 0000ff;}
// model
Import util.observable;
Import Temperature. *;
Class Temperature.temperature Temperature.temperatureModel Extends Observable
{
Private var temperaturef: Number = 32.0;
Public function getf (): Number
{
Return TemperatureF;
}
Public function getc (): Number
{
Return (TemperatureF - 32.0) * 5.0 / 9.0;
}
Public Function SetF (Tempf: Number): Void
{
TemperatureF = Tempf;
setChanged ();
NotifyObservers ();
}
Public Function SetC (Tempc: Number): Void
{
Temperaturef = Tempc * 9.0 / 5.0 32.0;
setChanged ();
NotifyObservers ();
}
}
// view base
IMPORT Util. *;
Import Temperature. *;
Import mvc. *;
Import mx.containers.window;
Import mx.controls.button;
Import mx.controls.textinput;
Import mx.controls.label;
Import mx.managers.depthmanager; Class Temperature.temperatureGUI Extends AbstractView
{
Private var temperatureWindow: Window;
Private var label: label;
Private var display: textinput;
PRIVATE VAR UPTN: Button;
Private var downbtn: button;
Private var temperaturegui_mc: movieclip;
Private function TemperatureGUI (L: String, M: TemperatureModel, C: Controller, Target: MovieClip, Depth: Number, x: Number, Y: Number
{
Super (m, c);
MaketemperatureGUI (L, Target, Depth, X, Y);
}
Private function MakeTemperatureGUI (L: String, Target: MovieClip, Depth: Number, x: Number, Y: Number: VoID
{
// Create an empty movieclip
TemperatureGUI_MC = Target.createemptyMovieClip ("GUI" Depth, Depth;
//TemperatureGUI_MC._X = x;
//temperaturegui_mc._y = y;
//window
TemperatureGUI_MC.createClassObject (Window, "TemperatureWindow", 0);
TemperatureWindow.Title = L "Temperature display";
TemperatureWindow.move (x, y);
TemperatureWindow.setsize (250, 200);
//label
Label = TemperatureWindow.createClassChildATDepth (label, depthmanager.ktop);
Label.Text = L "temperature is:";
Label.move (20, 50);
// Text display box
Display = TemperatureWindow.createClassChildATDepth (TextInput, DepthManager.ktop);
Display.editable = false;
Display.setsize (200, Display.height);
Display.move (label.x, label.y label.height 10);
// Tour button
Upbtn = TemperatureWindow.createClassChildATDepth (Button, DepthManager.ktop);
Upbtn.label = "heating";
Upbtn.setsize (60, upbtn.height);
Upbtn.move (Display.x 30, Display.y Display.Height 10);
Upbtn.addeventListener ("Click", getController ());
// cooling button
DownBTN = TemperatureWindow.createClassChildATDepth (Button, DepthManager.ktop);
Downbtn.label = "cooling"; DownBtn.setsize (60, downbtn.height);
Downbtn.move (Upbtn.x Upbtn.width 10, Upbtn.y);
Downbtn.adDeventListener ("Click", getController ());
Update ();
}
Public Function DefaultController (Model: OBSERVABLE): Controller
{
Return New TemperatureController (Model);
}
Public Function setDisplay (s: string): void {display.text = s;}
Public function getDisplay (): Number
{
// var result: Number = 0.0;
Var Result: Number = Number (Display.Text);
Return Result;
}
Public Function Update (o: Observable, Infoobj: Object): Void
{
}
}
// Farenheit View
IMPORT Util. *;
Import Temperature. *;
Import mvc. *;
Class Temperature.farenheitgui Extends Temperaturegui
{
Public Function Farenheitgui (M: TemperatureModel, C: Controller, Target: MovieClip, Depth: Number, x: Number, Y: Number
{
Super ("Hua", M, C, Target, Depth, X, Y);
// setDisplay ("" model.getf ());
}
Public Function Update (o: Observable, Infoobj: Object): Void
{
SetDisplay (" TemperatureModel (getModel ()). getf ());
}
}
// Celsius View
IMPORT Util. *;
Import Temperature. *;
Import mvc. *;
Class Temperature.celsiusGUI Extends Temperaturegui
{
Public Function Celsiusgui (M: TemperatureModel, C: Controller, Target: MovieClip, Depth: Number, x: Number, Y: Number
{
Super ("Celsius", M, C, Target, Depth, X, Y);
// setDisplay ("" model.getf ());
}
Public Function Update (o: Observable, Infoobj: Object): Void
{
SetDisplay (" TemperatureModel (getModel ()). getc ());
}
}
// Controller
Import Temperature.temperature Temperature.temperature ModuR;
Import mvc. *;
IMPORT Util. *;
Class Temperature.TemperatureController Extends AbstractController
{
Public Function TemperatureController (TM: OBSERVABLE)
{
Super (TM);
Public function uptemperature ()
{
VAR M: TemperatureModel = TemperatureModel (getModel ());
M.SETF (M.GETF () 1);
}
Public function downtemperature ()
{
VAR M: TemperatureModel = TemperatureModel (getModel ());
M.SETF (M.GETF () - 1);
}
PUBLIC FUNCTION CLICK (E: Object): Void
{
Switch (e.target.label)
{
Case "heating": UpTemperature (); Break;
Case "Cooling": Downtemperature (); BREAK;
DEFAULT: BREAK;
}
}
}
// main program
Import Temperature. *;
Class Temperature.thermometer
{
// Program entry
Public Static Function Main (Target: MovieClip, F: Number)
{
VAR TM: TemperatureModel = New TemperatureModel ();
Var fg: farenheitgui = new farenheitgui (TM, undefined, target, 2, 10, 50);
VAR CG: Celsiusgui = New Celsiusgui (TM, undefined, target, 3, 300, 50);
TM.AddobServer (fg);
TM.AddobServer (CG);
IF (f! = undefined) TM.SETF (f);
}
}
//*.fla
Import temperature.thermometer;
THERMOMETER.MAIN (THIS);
In the FLA file, you want to drag the four components of Window, Button, TextInput, Label in the library, or not