Android and design mode-Singleton Mode

Source: Internet
Author: User

Android and design mode-Singleton Mode
Concept:
Java Singleton mode is a common design mode. Singleton mode is divided into three types: lazy Singleton, ELE. Me Singleton, and registered Singleton.
The Singleton mode has the following features:
  1. A singleton class can only have one instance.
2. The Singleton class must create its own unique instance.
3. The Singleton class must provide this instance to all other objects.

The Singleton mode ensures that a class has only one instance, and the instance is self-instantiated and provided to the entire system. In computer systems, driver objects of thread pools, caches, log objects, dialogs, printers, and graphics cards are often designed as Singleton. These applications have more or less resource manager functions. Each computer can have several printers, but only one Printer Spooler can be used to prevent two print jobs from being output to the Printer at the same time. Each computer can have several communication ports. The system should centrally manage these communication ports to prevent a communication port from being simultaneously called by both requests. In short, the singleton mode is selected to avoid inconsistency and avoid multiple leaders. (Source and Network)

The Singleton mode is widely used in the Android source code and has been used in many familiar Telephony modules, such as sim card management UiccController and telephone management CallManager. The following describes the basic model of the singleton mode:


Lazy singleton:

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() {    .......    }    .......}

In this example, call CallManager. getInstance () to obtain the CallManager instance.


Hungry Chinese Style singleton:

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 new 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() should only be 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) {    ......    }    ......}


In this example, UiccController. make () is used to create an instance, and UiccController. getInstance () is used to obtain the instance.


Registration form example:

The example is not found yet...


Not yet resumed. please correct me if there is anything wrong.

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.