Quote a line on the Internet:
Singleton Mode(Singleton), also calledList ModeIs a common software design model. When this mode is applied, the class of the singleton object must ensure that only one instance exists. In many cases, the whole system only needs to have one global object, which facilitates us to coordinate the overall behavior of the system.
Http://zh.wikipedia.org/wiki/%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F
According to this definition and concept, if we really need a unique object access system when building a system, we can consider using the singleton mode. Actually in Java C ++. Net has already been described. Use flash as3 to describe it. It is not a fresh article.
To create an instance object, you can use the following simple method:
VaR OBJ: Example = new example (); // create a simple instance.
Well, next we will create a simple Singleton mode, called the player class or the main character class.
Package {
Public class player {
Private Static Var _ instance: player;
Public Function myclass (){}
Public static function getinstance (): player {
_ Instance = new player ();
Return _ instance;
}
}
}
Then modify some
Package {
Public class player {
Private Static Var _ instance: Player = NULL;
Public Function myclass () {If (_ instance! = NULL) throw new error ("creation failed ");}
Public static function getinstance (): player {
If (_ instance = NULL)
_ Instance = new player ();
Return _ instance;
}
}
}
When using him
VaR _ PLAYER: Player = player. getinstance ();
Add parameters:
This player category can be found in the game world.
Package {
Public class player {
Private Static Var _ instance: Player = NULL;
Public Function myclass (Name: String = NULL) {If (_ instance! = NULL) throw new error ("creation failed ");}
Public static function getinstance (Name: string): player {
If (_ instance = NULL)
_ Instance = new player (name );
Return _ instance;
}
}
}
You can also modify this class.
VaR _ PLAYER: Player = player. getinstance ("hero ");
Example: Write a class that loads XML externally. You can use the singleton mode. If you are interested, try it.