Android design mode-singleton Mode

Source: Internet
Author: User

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;}}

Related Article

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.