Object-oriented: singleton, Lambda expression

The singleton pattern guarantees that the object is unique in memory

public class Singleton{
    private static final Singleton singleObject = new Singleton();
    private Singleton(){}
    public static getSingleton(){
        return singleObject;
    }
}

The hungry man pattern creates object instances the first time this class is referenced.

Full-Han Model

public class Singleton{
    private static Singleton singleObject = null;
    private Singleton(){}
    public static getSingleton(){
        if(singleObject == null){
            singleObject = new Singleton;
        }
        return singleObject;
    }
}

Double lock mode

//This approach only considers thread safety, but every call getSingleton()Method
//All must be synchornized Queue up
public class Singleton{
    private static Singleton singleObject = null;
    private Singleton(){}
    public static getSingleton(){
        synchronized(Singleton.class){
            if(singleObject == null){
            singleObject = new Singleton;
        }
   }
return singleObject; } }

After improvement

//First, determine whether an object exists, such as no entry thread waiting
//Existing objects only need to be returned and threads do not need to wait
public class Singleton{
    private static volatile Singleton singleObject = null;
    private Singleton(){}
    public static getSingleton(){
        if(singleObject == null){
            synchronized(Singleton.class){
                if(singleObject == null){
                singleObject = new Singleton;
                }
            }
        return singleObject;
        }
    }
}

Comparisons of several models:
1. Hunger mode is thread-safe because instance objects are created during class loading, and object references are returned directly in getInstance() method.

Benefits: Thread security issues need not be addressed.

Disadvantage: If too many hungry cases are used in a large environment, too many instance objects will be produced.

2. The full-featured mode is not thread-safe, because it generates instance objects only when needed. Before production, it will judge whether the object reference is empty. Here, if multiple threads enter the judgment at the same time, it will generate multiple instance objects, which is not in line with the idea of singleton. Therefore, in order to ensure thread safety, the satiety mode identifies the method with synchronized keyword. The reason is called "full man", because it is full, not anxious to produce examples, in need of production.

Benefits: Delayed loading will produce objects only when used.

Disadvantage: Synchronization needs to be guaranteed at the cost of efficiency.

3. The double lock mode is the optimization of the satiety mode. It makes double judgement. When the instance object has been created, it does not need to lock and improves the efficiency.

Enumeration Writing Singleton Model

public enum EnumSingleton{
    singleObject;
    private String name;
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
}

Lambda expression

//Create threads
Thread td = new Thread(new Runnable){
    public void run(){
        System.out.println("---");
    }
};
td.start();

//Expression
Thread td = new Thread(() -> System.out.println("---"));
td.start();


//sort
List<String> list = Arrays.asList(new String[]{"a", "b", "c"});
Collections.sort(list, new Comparator<String>){
    public int compare(String str1, String str2){
        return str1.conpareTo(str2);
    }
});

//Expression
Collections.sort(list, (str1, str2) -> str1.compareTo(str2));

When using Lambda expressions instead of internal classes to create objects, the block of code represented by Lambda will replace the body of the method that implements the abstract method, and the Lambda expression is equivalent to an anonymous method.

Parametric list: Parametric list allows omitting parametric types. If the parameter list has only one parameter, even parentheses of the parameter list can be omitted.

Arrows - > must be made up of middle lines and larger than symbols in English.

Code block, if the code block has only one statement, allows to omit the curly brackets of the code block. The Lambada code block has only one return statement, and even omits the return keyword. Lambda needs to return a value. Its code block omits the return keyword and automatically returns the value of this statement.

public class LambdaClass2{
    
    public void Method1(InterfaceClass1 inClass1){
            //System.out.println(inClass1);
            inClass1.InterfaceClass1Method();
        }
    public void Method2(InterfaceClass2 inClass2){
            inClass2.InterfaceClass2Method("Suitable for travel");
        }
    public void Method3(InterfaceClass3 inClass3){
            //inClass3.InterfaceClass3Method(5, 3);
            System.out.println(inClass3.InterfaceClass3Method(5, 3));
        }
        
    public static void main(String[] args){
        LambdaClass2 lc = new LambdaClass2();
        //If there is only one statement, curly braces can be omitted
        lc.Method1(() -> System.out.println("This is an apple."));
        
        //Lambda With only one parameter, parentheses can be omitted
        lc.Method2(str -> {
            System.out.println("It's a nice day." + str);
            System.out.println("Sleet");
        });
        
        //There is only one statement in the code block, which can be omitted even if the expression needs to return a value. return Keyword
        lc.Method3((a, b) -> a + b);
    }
}
interface InterfaceClass1{
    void InterfaceClass1Method();
}
interface InterfaceClass2{
    void InterfaceClass2Method(String str);
}
interface InterfaceClass3{
    int InterfaceClass3Method(int a, int b);
}

Keywords: PHP Lambda

Added by neogranas on Wed, 19 Jun 2019 04:06:14 +0300