[] Kotlin Kotlin Singleton (lazy style and type villains | Java Singleton | Kotlin Singleton | object declaration | companion objects | get methods |? !! sentence and empty)



I. Lanhan Formula single embodiment of ruffian



1. Introduction singleton class: single embodiment is to ensure that the memory of the application, there is only one example of a design pattern;


① starving formula: This class objects as long as a statement, as long as the class is called to, whether or not to use its singleton object, which must be created immediately a singleton object (the object class hungry);

② lazy style: When you declare a class object, do nothing, only class to get singleton object, only to create objects (objects created lazy); only class to get singleton object, only to create an object;



2 singleton class features:


private static Singleton member variables;

privatization constructor;

public static Example acquisition unit;


Detail of Java and Kotlin single embodiment example of a detailed look at the following four examples



II. Java in the lazy type and type villains



. 1 Java hungry man single-mode example: In the first category is called on to create a singleton object;

package singleton.java;

/**
 * 饿汉单例模式
 *      在类第一次被调用就创建单例对象
 */
public class Student {

    // 1 . 私有静态化单例成员变量 ( 饿汉模式 )
    private static Student student = new Student();

    // 2 . 私有化构造函数
    private Student(){}

    // 3 . 公共静态获取单例方法
    public static Student getStudent(){
        return student;
    }

}


. 2 Java lazy man singleton pattern: create a singleton object in the object gets only a single case;

package singleton.java;

/**
 * 懒汉单例模式
 *      只有在单例对象获取时才创建单例对象
 */
public class Student {

    // 1 . 私有静态化单例成员变量
    private static Student student;

    // 2 . 私有化构造函数
    private Student(){}

    // 3 . 公共静态获取单例方法
    public static Student getStudent(){

        // 懒汉模式
        if(student == null){
            student = new Student();
        }
        return student;
    }

}


III. Kotlin corresponding Java lazy ruffian formula and the formula



. 1 Kotlin starving singleton: Kotlin starving singleton pattern is very simple, one line of code can be realized, the action of the line is identical to the code cook Student starving single mode embodiment above in Java;

package singleton.kotlin

/**
 * 饿汉式单例模式实现
 * 		被 object 关键字修饰的类时静态类 , 该类的成员方法和变量都是静态的
 *      调用时直接使用 Student.study() 即可调用该方法 
 */
object Student {
    fun study(){
        println("学习")
    }
}

This example relates to the subject statement, see the next section explains;



. 2 Kotlin single idler embodiment mode: This example relates to the companion object field getter methods defined above, and waited in vain additional knowledge judgment !!;?

package singleton.kotlin

/**
 * 懒汉式单例模式实现
 *
 * 1 . 私有化构造函数
 */
class Student private constructor() {

    /**
     * 伴生对象
     */
    companion object {

        // 2 . 私有化静态变量 , 赋值为空
        // ? 符号表示该变量任何时候不会报空指针异常 , 该变量可以为 空 null
        private var student : Student? = null
            //这是 Kotlin 中特有的 get set 方法定义方式
            //  在成员变量的下面可以直接定义该成员的 get() set() 方法
            get() {
                if (student == null){
                    student = Student()
                }
                return student
            }

        // 3 . 公共 ( 默认 ) 静态方法 , 获取 student 成员
        //  !! 表示该对象必须不能为空
        public fun getSingleton() : Student{
            return student!!
        }

        /**
         * 定义普通方法
         */
        fun study(){
            println("学习")
        }

    }



}

//调用单例方法测试
fun main() {

    //伴生对象方法 可以直接使用 Student.伴生对象方法 进行调用
    //学习
    Student.study()

    //学习
    Student.Companion.study()

}

This example involves the companion object declaration, getter methods, and other knowledge points !!, see the next section describes?;



IV. Kotlin objects (object) statement



. 1 object object declaration: When Kotlin declared using a Student object class that is a singleton;

object Student {
    fun study(){
        println("学习")
    }
}

. Singleton instance of the class 2 timing (the origin starving mode): After the statement object using Student Singleton immediately instantiates the singleton class corresponds starving singleton;


3. Inheritance wherein: Object classes modified single embodiment, can inherit other classes;


4 singleton object class object invocation statement: Direct Student using the class name to call, the name of the class is equivalent to the singleton object, as described above call class singleton Study () method, using the following call;

//调用 Student 单例对象的
Student.study()


V. Kotlin associated objects (companion object)



1. Associated Object: internal class object, use the modified companion object, the object is associated; singleton class is a class inside;


2. Associated Object Name: lazy above singleton pattern, the associated object name is omitted, in which case the default name Companion;


. Example 3 Single call: can directly use the class object's method calls associated, Student class defined above and associated objects, Student with Student.Companion are equivalent; Thus the test, the following two methods are associated object call method;

//调用单例方法测试
fun main() {

    //伴生对象方法 可以直接使用 Student.伴生对象方法 进行调用
    //学习
    Student.study()

    //学习
    Student.Companion.study()

}


VI.? !! role and



? !! and use: ? !! and variable names are added after the modified variable name;


? ① action: using the symbol behind the increase in the variable name indicates that the variable can be assigned to null;?

② !! effect: Use !! symbols is appended to the variable name, it indicates that the variable is not assigned to a null;

Published 307 original articles · won praise 1043 · Views 1.7 million +

Guess you like

Origin blog.csdn.net/han1202012/article/details/105003973