Design Mode exercise: Singleton Mode

Source: Internet
Author: User

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.

 

 

 

 

 

 

 

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.