java 三种调用机制(同步调用,异步调用,回调)及依赖关系传递的三种方式

sancaiodm Java 2021-10-14 2395 0

【1】三种调用机制

1:同步调用:一种阻塞式调用,调用方要等待对方执行完毕才返回,它是一种单向调用
2:回调:一种双向调用模式,也就是说,被调用方在接口被调用时也会调用对方的接口;
3:异步调用:一种类似消息或事件的机制,不过它的调用方向刚好相反,接口的服务在收到某种讯息或发生某种事件时,会主动通知客户方(即调用客户方的接口
具体说来:就是A类中调用B类中的某个方法C,然后B类中反过来调用A类中的方法D,D这个方法就叫回调方法,

1、同步调用

同步调用是最基本的调用方式,对象b中的方法直接调用对象a的方法,这个时候程序会等待对象a的方法执行完返回结果之后才会继续往下走。

代码如下:

public class A {

public void methodA()

{

System.out.println("this is class A method");

}

}


public class B {

public void methodB()

{

A a = new A();

a.methodA();

System.out.println("this is class B method");

}

}


public class Test {

public static void main(String[] args) {

B b = new B();

b.methodB();

}

}


结果:

this is class A method

this is class B method


2、异步调用

对象b中的方法调用对象a的方法,程序并不需要等待对象a的方法返回结果值,直接继续往下走。

代码如下:

public class A extends Thread{

@Override

public void run() {

try {

Thread.sleep(3000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("this is class A method");

}

}


public class B {

public void methodB()

{

A a = new A();

a.start();

System.out.println("this is class B method");

}

}


public class Test {

public static void main(String[] args) {

B b = new B();

b.methodB();

}

}


结果:

this is class B method

this is class A method

说明:异步调用我们通常采用多线程的方法来达到目的


3、回调

对象a的方法methodA()中调用对象b的methodB()方法,在对象b的methodB()方法中反过来调用对象a的callBack()方法,这个callBack()方法称为回调函数,这种调用方法称为回调。

代码如下:

public class A {

public void methodA()

{

B b = new B();

b.methodB(new A());

System.out.println("this is class A method : methodA");

}

public void callBack()

{

System.out.println("this is class A method : callBack");

}


}


public class B {

public void methodB(A a)

{

System.out.println("this is class B method : methodB");

a.callBack();

}


}


public class Test {

public static void main(String[] args) {

A a = new A();

a.methodA();

}

}

运行结果:

this is class B method : methodB

this is class A method : callBack

this is class A method : methodA


注意:这里如果为了代码的扩展性更好,可以把类A与类B抽象出一个接口出来,然后用实现类去实现着两个接口,这样代码的扩展性会更好,也能满足更多的业务场景。


回调的核心在于:回调方将本身对象传给调用方,调用方在本身代码逻辑执行完之后,调用回调方的回调方法。

原文链接:https://blog.csdn.net/zhenwei1994/article/details/79615861


【2】依赖关系传递的三种方式

首先先来理解一下java的依赖的定义:

依赖就是:A类中有B类的属性,那么A类就依赖B类 ,也就是说a类中引用了b类的b变量就叫依赖,

2.1  通过接口传递实现依赖

interface IOpenAndClose{   //开关接口
     public void open(ITV tv);//抽象方法,接收接口
}

interface ITV{ ITV接口
     public void play();
}

class HaiEr implements ITV{
     public void play(){
        System.out.println("play haier TV");
     }
}

class OpenAndColse implements IOpenAndClose{
    public void open(ITV tv){
       tv.play();
    }
}

public static void main(String[] args){
    HaiEr haier = new HaiEr();
    OpenAndColse openAndClose = new OpenAndColse();
    openAndClose.open(haier);
}

2.2  通过构造方法实现依赖传递

interface IOpenAndClose{   //开关接口
     public void open(ITV tv);//抽象方法,接收接口
}

interface ITV{ ITV接口
     public void play();
}

class HaiEr implements ITV{
     public void play(){
        System.out.println("play haier TV");
     }
}

class OpenAndColse implements IOpenAndClose{
    public ITV tv //成员
    public OpenAndColse(ITV tv){//构造方法
        this.tv = tv;
    }
    
    public void open(){
       tv.play();
    }
}

public static void main(String[] args){
    HaiEr haier = new HaiEr();
    OpenAndColse openAndClose = new OpenAndColse(haier);
    openAndClose.open();
}


2.3  通过setter方式实现依赖传递

interface IOpenAndClose{   //开关接口
     public void open();//抽象方法,接收接口
     public void setTv(ITV tv)
}

interface ITV{ //ITV接口
     public void play();
}

class HaiEr implements ITV{
     public void play(){
        System.out.println("play haier TV");
     }
}

class OpenAndColse implements IOpenAndClose{
    public ITV tv //成员
    public void setTv(ITV tv){
         this.tv = tv;
    }
    
    public void open(){
       tv.play();
    }
}

public static void main(String[] args){
    HaiEr haier = new HaiEr();
    OpenAndColse openAndClose = new OpenAndColse(haier);
    openAndClose.setTv(haier);
    openAndClose.open();
}


评论