java-callback mechanism

Explain the callback mechanism through a case study:

Case 1:

The teacher thanked an example on the blackboard, "1 + 1=", and Xiao Ming filled in the blanks.

Since he has learned addition within 10 years, Xiao Ming can calculate it by himself.

package ss;

public class Student {

    private String name=null;

    public Student(String name){
        this.name=name;
    }

    public void setName(String name){
        this.name=name;
    } 
    //The addition operation within 10 years of Xiaoming Institute
    public int caleADD(int a,int b){
        if(a<10&&b<10){
            return a+b;
        }
        else{
            return 0;
        }

    }

    //Addition within 10
    public void fillBack(int a,int b){
        int result=caleADD(a, b);
        if(result==0){
            System.out.println("No more than 10!!");
        }else{
        System.out.println(name+"Mental calculation or arithmetic"+a+"+"+b+"="+result);
        }
    }   
}

When filling in the Balnk, Xiao Ming took a clacADD and got the result of 2. He wrote the result in the blank. The test code is as follows:

package ss;

public class Test {
    public static void main(String[] args) {
        int a=1;
        int b=2;
        Student student=new Student("Xiao Ming");
        student.fillBack(a, b);
    }
}

Case 2:

Suddenly, the kindergarten teacher wrote "168 + 291 =" on the blackboard for Xiaoming to finish, and then returned to the office.
Why can't all the teachers get along with Xiao Ming? How about Mingming ChaoGang? At this time, Xiao Ming students obviously can no longer rely on mental arithmetic as above to complete, is in a foolish time, Xiao Hong students in the class handed over a calculator that can only calculate the addition!!! Xiao Ming just knew how to use the calculator, so he calculated the results by the calculator and completed filling in the blanks.

The supercalculator's add method should contain two operands and Ming's own reference:

package ss;

public class SuperCalculator {

    public void add(int a,int b,Student xiaoming){
        int result=a+b;
        xiaoming.fillBlank(a, b, result);
    }
}

Xiao Ming's side does not need mental arithmetic, nor need to use a calculator, so it only needs a way to ask Xiao Hong for help. The code is as follows:
Xiao Ming's fillBlank method is what we often call a callback function.

package ss;

public class Student {

    private String name=null;

    public Student(String name){
        this.name=name;
    }

    public void setName(String name){
        this.name=name;
    } 

    //The addition operation within 10 years of Xiaoming Institute
    public int caleADD(int a,int b){
        if(a<10&&b<10){
            return a+b;
        }
        else{
            return 0;
        }

    }

    //Output the results of the addition operation of Xiaoming Institute within 10
    public void fillBack(int a,int b){
        int result=caleADD(a, b);
        if(result==0){
            System.out.println("No more than 10!!");
        }else{
        System.out.println(name+"Mental calculation or arithmetic"+a+"+"+b+"="+result);
        }
    }

    //Help-seeking methods
    public void callHelp(int a,int b){
        new SuperCalculator().add(a, b, this);
    }

    //Output of the results after help-seeking
    public void fillBlank(int a,int b,int result){

        System.out.println(name+"For Xiaohong's calculation:"+a+"+"+b+"="+result);
    }


}

Test results:

 public class Test
 {
    public static void main(String[] args)
    {
         int a = 26549;
         int b = 16487;
         Student s = new Student("Xiao Ming");
         s.callHelp(a, b);
     }
}

Case 3:

There was a gray-haired old lady at the door. Every day there was rain and wind, and there was a stall selling junk food that was going out of date. As a result of old age, the brain is somewhat confused, often unable to figure out how much money they have earned. One day, she overheard Xiao Ming and her friends boasting about how to fight wit and courage with Xiao Hong's help. So my mother-in-law decided to find a small red card super calculator to help her, and offered a package of Weilong hot sticks as a reward. Xiao Hong could not resist the temptation and agreed.

Xiao Hong hopes to continue to provide computing services to the children in her class, and also to provide accounting services to her wife, and even to expand other people's business in the future. So she has agreed to all customers a way to deal with the problem uniformly, that is, how to handle the operands she needs and what to do after doing the calculation. Do it. This unified method, Xiaohong made an interface, provided to everyone, the code is as follows:

public interface doJob
 {
     public void fillBlank(int a, int b, int result);
 }

Xiaohong has modified her calculator to handle different people who implement the doJob interface at the same time. The code is as follows:

 public class SuperCalculator
 {
     public void add(int a, int b, doJob  customer)
     {
         int result = a + b;
         customer.fillBlank(a, b, result);
     }
 }

After Xiao Ming and his wife got the interface, as long as they realized it, it would be equivalent to telling Xiao Hong the way to deal with the result according to the unified mode, using internal classes as mentioned before, the code is as follows:

Xiaoming's:

package ss;

public class Student {

    private String name=null;

    public Student(String name){
        this.name=name;
    }

    public void setName(String name){
        this.name=name;
    } 



    public class doHomeWork implements doJob{

        @Override
        public void fillBack(int a, int b, int result) {
              System.out.println(name + "Calculating Xiaohong for Help:" + a + " + " + b + " = " + result);

        }

    }

    public void callHelp(int a,int b,int result){
        new SuperCalculator().add(a, b, new doHomeWork());
    }   
}

Wife's:

package ss;

public class Seller{

    private String name=null;

    public Student(String name){
        this.name=name;
    }

    public void setName(String name){
        this.name=name;
    } 



    public class doHomeWork implements doJob{

        @Override
        public void fillBack(int a, int b, int result) {
              System.out.println(name + "Calculating Xiaohong for Help:" + a + " + " + b + " = " + result);

        }

    }

    public void callHelp(int a,int b,int result){
        new SuperCalculator().add(a, b, new doHomeWork());
    }

}

Test:

public class Test
  {
      public static void main(String[] args)
      {
          int a = 56;
          int b = 31;
          int c = 26497;
          int d = 11256;
          Student s1 = new Student("Xiao Ming");
         Seller s2 = new Seller("Mother-in-law");

         s1.callHelp(a, b);
         s2.callHelp(c, d);
     }
 }

Keywords: calculator

Added by s_ff_da_b_ff on Mon, 01 Jul 2019 21:51:47 +0300