The design pattern is the predecessor in the development process summarizes some experience, we in the development process according to the actual situation, applies the suitable design pattern, may make the program structure more simple, facilitates the program the extension and the maintenance, but also is not does not use the design pattern the procedure is not good, if the simple procedure does not have, has the kind of superfluous feeling.
Singleton mode can be said to be the simplest of all modes, it can only create one instance from start to finish, there are two forms, namely lazy and a hungry man type
First, a hungry man-style, very simple, the beginning of the creation of an instance, in fact, will not be called or no matter
Package com.dzt.singleton;/** * A hungry man type, thread safe * * @author Administrator * */public class Singletonhungry {private stat IC Singletonhungry instance = new Singletonhungry ();p rivate singletonhungry () {}public static singletonhungry GetInstance () {return instance;}}
Second, lazy, because the thread is not safe, in multi-threaded processing will be problematic, so need to add synchronization
Package com.dzt.singleton;/** * lazy, which is thread insecure, if there are multiple threads executing, there may be multiple instances created * * @author Administrator * */public class Singletonidler {private static Singletonidler instance = Null;private Singletonidler () {}public static singletonidler get Instance () {if (Instance = = null) {Instance = new Singletonidler ();} return instance;}}
Add the code after the synchronization, every time you have to judge the synchronization lock, more expensive, but also can be improved
Package com.dzt.singleton;/** * Lazy type * * @author Administrator * */public class Singletonidler {private static Sing Letonidler instance = Null;private Singletonidler () {}public synchronized static Singletonidler getinstance () {if (Instan CE = = null) {instance = new Singletonidler ();} return instance;}}
Add synchronized code blocks, only to determine the synchronization, if you have created an instance will not be judged, reducing the time
Package com.dzt.singleton;/** * Lazy type * * @author Administrator * */public class Singletonidler {private static Sing Letonidler instance = Null;private Singletonidler () {}public static Singletonidler getinstance () {if (instance = = null) {s Ynchronized (Singletonidler.class) {if (instance = = null) instance = new Singletonidler ();}} return instance;}}
Singleton mode is also used in Androidd native applications, such as phone
Notificationmgr.java class
private static Notificationmgr sinstance;private Notificationmgr (Phoneapp app) {mApp = App;mcontext = app; Mnotificationmanager = (Notificationmanager) app.getsystemservice (context.notification_service); MStatusBarManager = (Statusbarmanager) app.getsystemservice (context.status_bar_service); Mpowermanager = (PowerManager) App.getsystemservice (context.power_service); mphone = App.phone; Todo:better style to use Mcm.getdefaultphone ()//everywhere INSTEADMCM = App.mcm;statusbarhelper = new Statusbarhelper ();} Static Notificationmgr Init (Phoneapp app) {synchronized (Notificationmgr.class) {if (sinstance = = null) {sinstance = new N Otificationmgr (app);//Update The notifications that need-be-touched at Startup.sInstance.updateNotificationsAtStartup ();} else {LOG.WTF (Log_tag, "init () called Multiple times! Sinstance = "+ sinstance);} return sinstance;}}
Single-instance mode for Android design mode