|
1 , Singleton mode
Advantages:
1) Save memory
2) Avoid State conflicts due to multiple states
The singleton model is divided into lazy mode and bad-han mode.
2 , a hungry man mode
1) Privatization Construction method
2) Create an instance in this class
3) The instance needs to be privatized private, to ensure that the instance can only be assigned once, using the modifier final to modify
4) provide a get to allow the outside world to use this instance
5) The method requires a static
The code format is as follows:
6) Hungryinstance Class
Main method:
3 , lazy mode
1) Privatization Construction method
2) Properties of the privatization statement
3) getinstance Determines whether the property is empty if NULL creates an object that is not empty directly returns an object
4) method requires static
The code is as follows:
INSTANCE
New in the Mian method:
The biggest difference between the A hungry man and the lazy type is that
A hungry man-type, more secure |