Java Serial 54-Two Singleton Modes, Interface Details

1. The singleton pattern is divided into two types:

(1) Hungry Han singleton: an object is created during the class loading stage.

(2) Lazy singleton: objects are created only when they are used.(Example in serials 53 is lazy)

Hungry Han Style Example:

 

package com.bjpowernode.java_learning;

​

public class D54_1_HungtySingtonMode {

  public static void main(String[] args) {

    Customer54 c1 = Customer54.getCustomer54();

    Customer54 c2 = Customer54.getCustomer54();

    System.out.println(c1==c2);

  }

}

//Hungry Han Style Single Case Mode

class Customer54{

  private static Customer54 c = new Customer54();

  private Customer54() {}

  public static Customer54 getCustomer54(){

    return c;

  }

}

2. Interfaces are also a type of reference and can be equated to classes

1. How to define interfaces, syntax:

 

[modifier] interface interface name ()

 

 

2. Interfaces can only appear: constants, abstract methods

3. Interfaces are a special Abstract class, especially when they are completely abstract

4. There is no construction method in the interface and it cannot be instantiated

5. Multiple inheritance between interfaces

6. A class can implement multiple interfaces.(Here the implementation can be equated to inheritance)

7. A non-abstract class implements an interface, requiring all methods in the interface to be "implemented/overridden/overridden"

 

package com.bjpowernode.java_learning;

​

public interface D54_2_InterfaceExercise {

  public static final String SUCCESS = "sucess";

  public static final double PI = 3.1415926;

 

  //public static final You can omit it in an interface because, as grammar rules know, it can only be a constant

  byte MAX_VALUE = 127;

 

  public abstract void m54();//Abstract method

 

  void test54();//This is also the abstract method, where public abstract It can also be omitted.For the same reason

}

​

interface B54 {

  void m2();

}

interface C54{

  void m3();

}

interface D54{

  void m4();

}

interface E53 extends B54,C54,D54{

  void m5();

}

//implements Is the meaning of realization

//Since classes are single-inherited, a new keyword is used here implements Implement

class MyClass implements B54,C54{

  public void m2() {}//Because m2 Originally an abstract method, here our new class must override the override m2 Method

  public void m3() {}

}

​

class F54 implements E54{

  public void m2() {}

  public void m3() {}

  public void m4() {}

  public void m5() {}

}

 

 

We have exemplified the above seven grammars.

3. The function of the interface (see an example first, sum up next time)

 

package com.bjpowernode.java_learning;

​

public interface D54_3_CustomerService {

  void logout();

}

 

package com.bjpowernode.java_learning;

​

public class D54_4_ImplementCustomer{

  public static void main(String[] args) {

    //The following program is called Interface-oriented

    D54_3_CustomerService d1 = new D54_4_ImplementCustomerService();//polymorphic

    d1.logout();//The underlying class, the type is the interface, can be the method in the interface used, actually call the method of the class actually used because the underlying class is the class

  }

}

class D54_4_ImplementCustomerService implements D54_3_CustomerService{

  //Implement abstract methods in interfaces

  public void logout() {

    System.out.println("Successfully exited the system!");

  }

}

4. Source code:

D54_1_HungtySingtonMode.java

D54_2_InterfaceExercise.java

D54_3_CustomerService.java

D54_4_ImplementCustomer.java

Address:

https://github.com/ruigege66/Java/blob/master/D54_1_HungtySingtonMode.java

https://github.com/ruigege66/Java/blob/master/D54_2_InterfaceExercise.java

https://github.com/ruigege66/Java/blob/master/D54_3_CustomerService.java

https://github.com/ruigege66/Java/blob/master/D54_4_ImplementCustomer.java

2.CSDN: https://blog.csdn.net/weixin_44630050 (Xinyue Jun Don't Know-Rui)

3. Blog Park: https://www.cnblogs.com/ruigege0000/

4. Welcome to the WeChat Public Number: Fourier Transform, Personal Public Number, only for learning and communication, Background Reply "Gift Pack" to get big data learning materials

 

Keywords: Java github Big Data

Added by ExpendableDecoy on Thu, 28 Nov 2019 18:44:09 +0200