JAVA learning 22 --- interface

Interface understanding

An interface is a specification. It defines a set of rules. Its essence is a contract, standard and specification. Java does not support multiple inheritance. With an interface, you can get the effect of multiple inheritance.

Use of interfaces

  • Use the interface keyword to define the interface
  • In Java, interface and class are two parallel structures
  • Define the members of the interface:
    1. Before JDK7, only global constants and abstract methods can be defined
    Global constant: public static final
    Abstract method: public abstract
    2. In addition to defining global constants and abstract methods, jdk8 can also define static methods and default methods
    Static methods defined in the interface can only be called through the interface
    The default method defined in the interface can be called through the object implementing the class
  • Constructors cannot be defined in interfaces and cannot be instantiated
  • Class implements the interface through the implements method
    If the implementation class overrides all abstract methods in the interface, the implementation class can be instantiated
    If the implementation class does not override all abstract methods in the interface, the implementation class is an abstract class
  • Java classes can implement multiple interfaces
    Format: Class AA extensions BB implementations CC, DD, EE
  • Multiple inheritance can be implemented between interfaces
public class demo {
    public static void main(String[] args) {
        System.out.println(fly.Max_speed);
    }
}
interface fly{
    //Global constant
    public static final int Max_speed = 7900;
    int Min_speed = 0;
    //Abstract method
    public abstract void fly();
    void down();
}
interface attack{
    void attack();
}
class plane implements fly{

    @Override
    public void fly() {
        System.out.println("fly");
    }

    @Override
    public void down() {
        System.out.println("down");
    }
}
abstract class kite implements fly{
}
class  Bullet extends Object implements fly,attack{
    @Override
    public void fly() {
        
    }

    @Override
    public void down() {

    }

    @Override
    public void attack() {

    }
}

Interface creation

  1. Create a non anonymous object of the non anonymous implementation class of the interface
Flash f = new Flash();
com.transfer(f);
  1. Create an anonymous object of the non anonymous implementation class of the interface
com.transfer(new Flash());
  1. Create a non anonymous object of the anonymous implementation class of the interface
        USB usb = new USB() {
            @Override
            public void start() {
                System.out.println("start");
            }

            @Override
            public void stop() {
                System.out.println("stop");
            }
        };
        com.transfer(usb);
  1. Create an anonymous object of the anonymous implementation class of the interface
        com.transfer(new USB() {
            @Override
            public void start() {
                System.out.println("start");
            }

            @Override
            public void stop() {
                System.out.println("stop");
            }
        });

The following are all codes:

public class demo {
    public static void main(String[] args) {
        Computer com = new Computer();
        
        //1
        Flash f = new Flash();
        com.transfer(f);
        
        //2
        com.transfer(new Flash());
        
        //3
        USB usb = new USB() {
            @Override
            public void start() {
                System.out.println("start");
            }

            @Override
            public void stop() {
                System.out.println("stop");
            }
        };
        com.transfer(usb);

        //4
        com.transfer(new USB() {
            @Override
            public void start() {
                System.out.println("start");
            }

            @Override
            public void stop() {
                System.out.println("stop");
            }
        });
    }
}

interface USB{
    void start();
    void stop();
}
class Flash implements USB{
    public void start(){
        System.out.println("start");
    }
    public void stop(){
        System.out.println("stop");
    }
}
class Computer{
    public void transfer(USB usb){
        usb.start();
        System.out.println("loding");
        usb.stop();
    }
}

Application of interface

  • proxy pattern

Proxy pattern is a design pattern used more in Java development. Proxy design is to provide a proxy for other objects to control access to this object.

Application scenario:
1. Security agent: block direct access to real objects
2. Remote proxy: handle remote method calls (RMI) through proxy classes
3. Delayed loading: load the lightweight proxy object first, and then load the real object if necessary

Classification: static agent, dynamic agent

Code implementation:

public class demo {
    public static void main(String[] args) {
        Server server = new Server();
        ProxyServer proxyServer = new ProxyServer(server);
        proxyServer.browse();
    }
}

interface NetWork{
    public void browse();
}
//Proxy class
class Server implements NetWork{

    @Override
    public void browse() {
        System.out.println("browse");
    }
}
//proxy class
class ProxyServer implements NetWork{
    private NetWork work;
    public ProxyServer(NetWork work){
        this.work=work;
    }
    public void check(){
        System.out.println("check");
    }
    @Override
    public void browse() {
        check();
        work.browse();
    }
}
  • Factory mode

The factory pattern separates the creator from the caller, that is, the specific process of creating objects is shielded and isolated to improve flexibility.

Classification:

  • Simple factory mode: used to generate any product in the same hierarchical structure. (for adding new products, the existing code needs to be modified)
  • Factory method mode: used to produce fixed products in the same hierarchical structure. (any product can be added)
  • Abstract factory pattern: used to produce all products of different product families. (there is nothing I can do to add new products, but I support adding product families)

Keywords: Java

Added by PlagueInfected on Fri, 14 Jan 2022 16:52:06 +0200