Android design mode-singleton Mode
Design Patterns are some of the experiences accumulated by our predecessors during the development process. During the development process, we apply appropriate design patterns based on actual conditions to make the program structure simpler, it is conducive to program expansion and maintenance, but it is not difficult to use a program without a design pattern. For example, a simple program is no longer needed, and there is a feeling of being superfluous.
The Singleton mode is the simplest of all modes. It can only create one instance from start to end. It can be in two forms: lazy and hungry.
I. Ele. Me is simple. I created an instance from the very beginning. In fact, whether it will be called or not
Package com. dzt. singleton;/*** hunger, thread security ** @ author Administrator **/public class SingletonHungry {private static SingletonHungry instance = new SingletonHungry (); private SingletonHungry () {} public static SingletonHungry getInstance () {return instance ;}}
2. In the lazy way, because the thread is not secure, there may be problems in multi-thread processing, So synchronization is required.
Package com. dzt. singleton;/*** lazy, this is thread unsafe. If multiple threads are running, multiple instances may be created ** @ author Administrator **/public class SingletonIdler {private static SingletonIdler instance = null; private SingletonIdler () {} public static SingletonIdler getInstance () {if (instance = null) {instance = new SingletonIdler () ;}return instance ;}}
After the synchronization code is added, You must judge the synchronization lock every time you come in. This is time-consuming and can be improved.
Package com. dzt. singleton;/*** lazy ** @ author Administrator **/public class SingletonIdler {private static SingletonIdler instance = null; private SingletonIdler () {} public synchronized static SingletonIdler getInstance () {if (instance = null) {instance = new SingletonIdler () ;}return instance ;}}
Add a synchronization code block and only judge the synchronization once. If you have created an instance, the synchronization will not be judged, reducing the time.
Package com. dzt. singleton;/*** lazy ** @ author Administrator **/public class SingletonIdler {private static SingletonIdler instance = null; private SingletonIdler () {} public static SingletonIdler getInstance () {if (instance = null) {synchronized (SingletonIdler. class) {if (instance = null) instance = new SingletonIdler () ;}} return instance ;}}
The Singleton mode is also used in Android 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 NotificationMgr(app);// Update the notifications that need to be touched at startup.sInstance.updateNotificationsAtStartup();} else {Log.wtf(LOG_TAG, "init() called multiple times! sInstance = "+ sInstance);}return sInstance;}}