Single-column mode of android source code design mode learning

A single-column object class must ensure that an instance exists, and instantiate itself and provide this instance to the entire system

Usage scenario: Ensure that a certain class has one and only one object scenario, avoiding the generation of multiple objects that consume too many resources, or there should be only one and only one object of a certain type, such as accessing a database

The key points of using single-column mode:
(1) The constructor is not open to the outside world, generally private
(2) Return single-column objects through a static method or enumeration
(3) Ensure that there is only one single-column object, especially in a multi-threaded environment
(4 ) to ensure that single-column objects are not rebuilt when deserialized

(1) Hungry Chinese style

public class IOUtil {
    
    
    private static final IOUtil INSTANCE = new IOUtil();
    private IOUtil(){
    
    }
    public static IOUtil getInstance(){
    
    
        return INSTANCE;
    }
}

(2) The lazy style
is to declare a static object and initialize it when the user calls getInstance for the first time

public class IOUtil {
    
    
    private static IOUtil INSTANCE = null;
    private IOUtil(){
    
    } 
    public static synchronized IOUtil getInstance(){
    
    
        if (INSTANCE == null){
    
    
            INSTANCE = new IOUtil();
        }
        return INSTANCE;
    }
}

A synchronized keyword is added to the getInstance() method, which is a synchronization method of getInstance, which is a means to ensure the uniqueness of a single-column object in a multi-threaded situation. However, synchronized will be synchronized every time getInstance is used, which will cause unnecessary synchronization overhead, so it is recommended not to use this keyword when multi-threading is not used

(3) DCL implements a single column.
The advantage of DCL to realize the singleton mode is that it can not only initialize a single column when needed, but also ensure thread safety, and call getInstance after the initialization of a single column object without synchronization lock

public class IOUtil {
    
    
    private static IOUtil INSTANCE = null ;

    private IOUtil(){
    
    }

    public static IOUtil getInstance(){
    
    
        if (INSTANCE == null){
    
    
            synchronized (IOUtil.class){
    
    
                if (INSTANCE == null){
    
    
                    INSTANCE = new IOUtil();
                }
            }
        }
        return INSTANCE;
    }
}

Advantages: High resource utilization
Disadvantages: Slow response when loading for the first time

(4) Although the static internal class single-column mode
DCL solves the problems of resource consumption, redundant synchronization, and thread safety to a certain extent, it still fails in some cases and is not approved for use.

public class IOUtil {
    
    

    public static IOUtil getInstance(){
    
    
        return SingleHolder.INSTANCE;
    }
    
    private static class SingleHolder{
    
    
        public static final IOUtil INSTANCE = new IOUtil();
    }
}

INSTANCE will not be initialized when the IOutil class is loaded for the first time, and INSTANCE will be initialized only when getInstance is called for the first time. This method not only ensures thread safety, but also ensures the uniqueness of singleton objects. It is recommended to use.

(5) Using containers to implement singleton mode

public class IOUtil {
    
    
    private static HashMap<String,Object> hashMap = new HashMap<>();

    public static void registerService(String key,Object object){
    
    
        if (!hashMap.containsKey(key)){
    
    
            hashMap.put(key,object);
        }
    }

    public static Object getService(String key){
    
    
        return hashMap.get(key);
    }
}

Single-column mode in Android source code
In the Android system, we often obtain system-level services through Context, such as WindowManagerService, ActivityManagerService, etc. More commonly used is the LayoutInflater class, and the more common place for LayoutInflater is in the getView method of ListView

View view = LayoutInflater.from(mContext).inflate(layoutId,null);

You can see that the form(Context) function internally calls the getSystemService(String key) method of the Context class. Let’s continue to look at

Guess you like

Origin blog.csdn.net/qq_42447739/article/details/126256178