Analysis of the RobotLegs framework
I. Advantages:
Decoupling, maintenance, and standardization facilitate division of labor and cooperation.
Ii. framework concept: Model-View-Controller-based meta-design mode + S (service: used to receive external data and external communication)
Ü automatic Dependency Injection (Automated Dependency Injection), which is completed by containers/frameworks instead of creating and obtaining dependent objects by themselves.
Ü Command Pattern ),
N the essence of the command mode is to encapsulate the command and separate the responsibility for issuing the command from that for executing the command;
N each command is an operation: the Requesting Party sends a request and requires an operation. The receiving party receives the request and performs the operation.
N the command mode allows the requester and the recipient to be independent, so that the requester does not have to know the interface of the receiver, or how the request is received, and whether the operation is executed, when it is executed, and how it is executed.
Ü the dispatcher Pattern defines an intermediary object to encapsulate the interaction between series objects. The intermediary allows each object to reference each other without being displayed, so that the coupling is loose and the interaction between them can be changed independently.
Iii. Composition:
Context: extends Context:
Provides communication mechanisms at other layers,
An application does not have to use only one Context, but in most cases, the next Context is enough.
Creating a modular application requires multiple contexts.
Implements IContext
// Generally, the ing and configuration are initialized here.
Override public function startup (): void
The event is the ContextEvent class.
The imported document class this will be bound to the contextView. The contextView can provide operations related to the DisplayObjectContainer object.
Controller & Commands: extends Commands
The Controller layer is reflected by the Command class. command is used to execute a single unit of work of an application, stateless, short-lived object. command is used to communicate with each other between application layers, or to send system events. these system events may start other commands or be received by a Mediator, and then work on a View Component corresponding to this event. command is an excellent place to encapsulate your application business logic.
Command is a short life cycle stateless object. they are released immediately after being instantiated and executed. the Command should only be executed when processing framework events, and should not be instantiated or executed by any other framework actor.
Command is a very useful mechanism for decoupling various actors in an application. because a Command will never be instantiated or executed by Mediator, Model, or Service, these classes will not be coupled to the command, or even do not know the existence of the command.
To fulfill their responsibilities, the Command may:
Ing Mediator, Model, Service, or other commands in Context
Broadcast events that may be received by Mediator or that trigger other commands.
The Model, Service, and Mediator are injected to work directly.
View & Mediators: extends Mediator
View is reflected by the Mediator class. the class inheriting Mediator is used to process the interaction between the framework and View Component. A Mediator listens to framework events and View Component events, and sends framework events when handling the events of View Component. in this way, developers can put the application-specific logic into the Mediator, instead of coupling the View Component to a specific application.
Mediator class is used as an intermediary between user interaction and System View Component.
The ing is usually completed in the setup () method of the Context that holds the contextView reference.
Overwriting onRegister is to add any system or View Component events that this Mediator cares about.
Listener: Mediators is equipped with an EventMap with the mapListener () method.
Send event: dispatch (new SomeEvent (SomeEvent. YOU_WILL_WANT_THIS, myViewComponent. someData ))
We recommend that you use the interfaces implemented by model and service to inject model and service into mediator. But do not inject command or other meditor. command can only be handled through event listening!
Model, Service and the Actor:
The service and model in the MVCS architecture have many similarities in concept. because of this similarity, model and service inherit the same Actor base class. inheriting the Actor base class provides many functions in the application architecture. in MVCS context, we use the inherited Actor base class to define the model and service class that the application needs to manage data and communicate with the outside world. in this document, the model and service classes are called Model and Service respectively.
Model: extends Actor
The Model class is used to encapsulate data at the model layer and provide APIs for it. The Model will send Event Notifications after some work on the data Model. The Model is usually highly portable.
There are many reasons to dispatch an event, including but not limited:
Data has been initialized and ready for use by other actor
Some data slices are added to the Model.
The data is deleted from the Model.
Data changed or updated
The data-related status has changed.
Service: extends Actor
A service layer is used to communicate with the "outside world. web service, file access, or any other action beyond the scope of the application is very suitable for the service class. the Service class Broadcasts System events when processing external events. A service should encapsulate interaction with external services and have high portability.
Service is used to access resources outside the application scope. This includes but is not limited:
Web services
File System
Database
Other Flash applications using localConnection
Note:
Mediator, Service, and Model can send events. The model and service should not listen for and process events. In this way, they will be tightly coupled to the specific logic of the application to reduce the potential portability and reusability.
Command. Mediator is the only method to receive framework events.
Iv. event mechanism:
Robotlegs uses the flash event mechanism for communication, while puremvc uses custom notifications to send messages! (This should be noted, so you must have a certain understanding of flash event mechanisms to customize user event Events)
Robotlegs does not support event bubbling because it does not rely on the Flash display list as the event bus
Note: Understand the Event mechanism (clone method)
Http://www.cnblogs.com/tankaixiong/archive/2012/12/12/2814272.html
5. Learn about SwiftSuspenders
Robotlegs uses an adapter mechanism to provide a dependency injection Mechanism for the framework. By default, the framework is equipped with a SwiftSuspenders injection/reflection library to suit this requirement.
SwiftSuspenders supports three types of dependency injection:
Ü attribute (domain) Injection
U parameter (method/Set Value) Injection
Ü construct Injection
This topic describes property injection and how to use it in Robotlegs. There are two options for property injection. You can use untitled or named injection:
[Inject]
Public var myDependency: Depedency; // untitled Injection
[Inject (name = "myNamedDependency")]
Public var myNamedDependency: NamedDepedency; // name Injection
In Robotlegs, injection ing is provided in three parts: MediatorMap, CommandMap, and Injector.
MapValue is used to map a specific instance of an object to an injector. When a specific class is requested, this specific instance of the class is used for injection (existing instances do not need to be instantiated again by rototlegs)
// A ing/configuration in your application
Var myClassInstance: MyClass = new MyClass ();
Injector. mapValue (MyClass, myClassInstance );
MapClass provides a unique (unique) instance of the mapped class for each injection request.
// A ing/configuration in your application
Injector. mapClass (MyClass );
MapSingleton provides a single _ (_ single) instance for each injection request. Note that the application scope is within the robotlegs framework. It is not mandatory as a singleton except Context.
MapSingletonOf functions like mapSingleton. It is useful for ing abstract classes and interfaces, while mapSingleton is used to map specific class implementations.
// A ing/configuration in your application
Injector. mapSingletonOf (IMyClass, MyClass); // MyClass implements IMyClass
MediatorMap:
Map mediators to view and register them for injection.
MapView (viewClassOrName: *, mediatorClass: Class, injectViewAs: Class = null, autoCreate: Boolean = true, autoRemove: Boolean = true): void
View component intermediary meditor
CommandMap: maps commands to methods that trigger their framework events.
MapEvent (eventType: String, commandClass: Class, eventClass: Class = null, oneshot: Boolean = false)
1 Command type 2 class for executing commands (the Command must be inherited and the execute method must be rewritten) 3 event class 4, whether to trigger only once
Note:
After you use robotsleg to manage a practical instance, if you create an object in the Code, the object cannot contain the injected object, because manual new is not injected through the robotslegs proxy.