Singleton design mode in Flash

Source: Internet
Author: User
// Singleton Design Mode
// Logon interface:
// Datamodel.
Import MX. Events. eventdispatcher;
Class memsys. datamodel {
Private Static VAR model: datamodel;
Public var addeventlistener: function;
Public var removeeventlistener: function;
Private var dispatchevent: function;
Private Static Var mixin1 = eventdispather. initialize (datamodel. Prototype );
Private function datamodel (){

}
Public static function getinstance (): datamodel {
If (model = NULL ){
Model = new datamodel ();
}
Return Model;
}
Public Function login (loginid: String, PWD: string): void {
VaR loginresult: Boolean;
If (Pwd = "PWD "){
Loginresult = true;
}
Else {
Loginresult = false;
}
Dispathevent ({type: "onlogin", target: This, loginresult: loginresult });
}
}
// Only one instance can be created for a class. The class constructor must be private. The new class cannot be created for the outside world. It can only be created through the static method getinstance () of the class, it checks whether the property exists, such as model. If the property exists, the created instance is returned. Otherwise, the instance is created and returned using its own property, which can be shared by any outside world.
A singleton is usually used to maintain data in a single state of the system, such as statistics on the number of visitors, online or offline. Multiple instances cannot have unified status data.

In flash, when is the singleton mode suitable?
To meet the following requirements, we can consider the singleton mode: a general business logic processor is required to process the same type of jobs, a Data Access Object is required to process a series of external data interactions, provided that consistent data access methods are used.
Because flash is client-based software, the client is basically used by one person, so we can ignore the thread concurrency issues that need to be considered in the software design (in B/S, in a C/S architecture system, these tasks are usually performed by the server ). moreover, although flash itself is multi-threaded, it does not allow us to control the thread by programming.
Therefore, the benefit of using the singleton mode in Flash is: Saving System overhead and increasing Code Reusability. In fact, the biggest benefit is only to save system overhead. As for code reuse, we can use various methods based on our own experience in software design.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.