Итак, недавно, дабы освоить шаблон MVC, я делал с ним кнопку, но как мне тут подсказали, кнопка - это чистое вьюв и для него MVC не нужно. По этому кнопку и переделал в один класс.
Теперь я сделал меню сайта с помощью шаблона проектирования MVC.
Документ-класс:

Код AS3:
package net.freedomstate.elements
{
/**
* ...
* @author Scorpion
*/
import flash.display.Sprite;
import net.freedomstate.elements.menu.View;
public class Menu extends Sprite
{
private var _view: View;
public function Menu(): void
{
_view = new View;
addChild(_view);
}
}
}
Классы MVC:

Код AS3:
package net.freedomstate.elements.menu
{
/**
* ...
* @author Scorpion
*/
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.*;
import net.freedomstate.elements.Butt;
public class View extends Sprite
{
private var _arrayButtons: Array = [ ];
private var _model: Model;
private var _controller: Controller;
private var _currentPage: Sprite;
public function View(): void
{
_model = new Model();
_controller = new Controller(_model);
_model.addEventListener(Event.CHANGE, viewPage);
viewMenu();
viewPage();
}
private function addButton(text: String, page: String): void
{
if (_arrayButtons.length == 0) _arrayButtons[_arrayButtons.length] = new Butt(175, 200, text)
else _arrayButtons[_arrayButtons.length] = new Butt(_arrayButtons[_arrayButtons.length - 1].x + _arrayButtons[_arrayButtons.length - 1].width + 5, 200, text);
addChild(_arrayButtons[_arrayButtons.length - 1]);
_arrayButtons[_arrayButtons.length - 1].name = page;
_arrayButtons[_arrayButtons.length - 1].addEventListener(MouseEvent.CLICK, clickButton);
}
private function clickButton(e: Event): void
{
_controller.dispatchEvent(new DataEvent(Event.ADDED_TO_STAGE, false, false, e.target.name));
}
private function viewMenu(): void
{
switch(_model.dostype)
{
case "guest": guestMenu(); break;
case "user": userMenu(); break;
}
}
private function viewPage(e: Event = null): void
{
if (_currentPage) { removeChild(_currentPage); _currentPage = null }
_currentPage = new _model.currentPage;
addChild(_currentPage);
}
private function guestMenu(): void
{
deleteButtonds()
addButton("Правила", "Description");
addButton("Карта", "Map");
addButton("Органи влади", "Authorities");
addButton("Підприємства", "Enterprises");
addButton("Реєстрація", "Registration");
addButton("Вхід", "Input");
}
private function userMenu(): void
{
deleteButtonds()
addButton("Правила", "Description");
addButton("Карта", "Map");
addButton("Органи влади", "Authorities");
addButton("Підприємства", "Enterprises");
addButton("Профіль", "Profile");
addButton("Вхід", "Input");
}
private function deleteButtonds(): void
{
for (var i: int = 0; i < _arrayButtons.length; i++ )
{
if (_arrayButtons[i]) { removeChild(_arrayButtons[i]); _arrayButtons[i] = null; }
}
}
}
}

Код AS3:
package net.freedomstate.elements.menu
{
/**
* ...
* @author Scorpion
*/
import flash.events.*;
import net.freedomstate.pages.*;
public class Controller extends EventDispatcher
{
private var _model: Model;
public function Controller(rModel: Model): void
{
_model = rModel;
this.addEventListener(Event.ADDED_TO_STAGE, addPage);
}
private function addPage(e: DataEvent): void
{
switch (e.data)
{
case "Description": _model.currentPage = Description; break;
case "Authorities": _model.currentPage = Authorities; break;
case "Enterprises": _model.currentPage = Enterprises; break;
case "Registration": _model.currentPage = Registration; break;
case "Input": _model.currentPage = Input; break;
case "Map": _model.currentPage = Map; break;
case "Profile": _model.currentPage = Profile; break;
}
_model.dispatchEvent(new DataEvent(Event.ADDED_TO_STAGE));
}
}
}

Код AS3:
package net.freedomstate.elements.menu
{
/**
* ...
* @author Scorpion
*/
import flash.events.*;
import net.freedomstate.pages.*;
public class Model extends EventDispatcher
{
private var _dostype: String;
private var _currentPage: Class;
public function Model(): void
{
_dostype = "guest";
_currentPage = Description;
this.addEventListener(Event.ADDED_TO_STAGE, addPage);
}
private function addPage(e: DataEvent): void
{
dispatchEvent(new Event(Event.CHANGE));
}
public function get dostype(): String
{
return _dostype;
}
public function get currentPage(): Class
{
return _currentPage;
}
public function set currentPage(page: Class): void
{
_currentPage = page;
}
}
}
Все ли я тут сделал правильно и достаточно ли гибким есть построение этого компонента на случай, если надо будет вносить в него изменения?