Concept:
JavaThe single-case mode is a common design pattern. The singleton pattern is divided into three kinds: lazy type single case, a hungry man type single case, registration type single case three kinds.
There are a few features of the Singleton model:
1, the Singleton class can only have one instance.
< /span> 2 , the Singleton class must create its own unique instance on its own.
Span lang= "en-US" style= "padding:0px; line-height:1.8; margin:0px ">3 , the Singleton class must provide this instance to all other objects.
A singleton pattern ensures that a class has only one instance. and instantiate it yourself and provide this instance to the system as a whole. In a computer system, the driver objects of the thread pool, cache, log Object, dialog box, printer, and video card are often designed as singleton. These apps have more or less the functionality of the resource manager. Each computer can have several printers, but it can only have one printer Spooler, to prevent two print jobs from outputting to the printer at the same time. Each computer can have several communication ports, and the system should centrally manage these communications ports to avoid a communication port being called at the same time by two requests at the same time.
In short, the selection of Singleton mode is to avoid inconsistent state, to avoid long-running political. (This segment source and network)
Single-case mode in the Android source code used very widely, in my familiar with the telephony module used a lot, for example, SIM card management Uicccontroller, telephone management CallManager and so on. Here's a look at the basic model of the Singleton pattern:
Lazy Single-Case:
Public final class CallManager { ... .. Singleton instance private static final CallManager instance = new CallManager (); ....... /** * Get singleton instance of CallManager * @return CallManager */public static CallManager GetInstance () { return INSTANCE; } ....... Private CallManager () { ... } .......}
This example uses the Callmanager.getinstance () call directly to obtain the CallManager instance.
a hungry Man type single case:
public class Uicccontroller extends Handler {... private static uicccontroller[] Minstance = {NULL, NULL, NULL, NULL}; ... public static uicccontroller make (Context C, commandsinterface ci, int simid) {synchronized (MLock) { if (Featureoption.mtk_gemini_support) {if (Minstance[simid]! = null) {throw NE W runtimeexception ("Uicccontroller.make () should only be called once"); } Minstance[simid] = new Uicccontroller (c, CI, simid); return Minstance[simid]; } else {if (minstance[0]! = null) {throw new RuntimeException ("Uicccontroller.make () s Hould only being called once "); } minstance[0] = new Uicccontroller (c, CI); return minstance[0]; }}} ... public static uicccontroller getinstance (int simid) {synchronized (MLock) { if (featureoption.mtK_gemini_support) {if (minstance[simid] = = null) {throw new RuntimeException ( "Uicccontroller.getinstance can ' t be called before make ()"); } return Minstance[simid]; } else {if (minstance[0] = = null) {throw new RuntimeException ( "Uicccontroller.getinstance can ' t be called before make ()"); } return minstance[0]; }}} ... private Uicccontroller (Context C, commandsinterface ci, int simid) {...} ......}
When this example is used, the instance is first created Uicccontroller.make (). Then Uicccontroller.getinstance () gets the instance.
Registration single case:
temporary not found the sample ...
Please correct me if there is an incorrect place to be continued.
Android and design mode--Singleton (Singleton) mode