Examples of high cohesion and low coupling of java callbacks

1. The overall update information and transfer to different parts, here is high cohesion

Student student_xuxiaodong = new Student_xuxiaodong();
Teacher teacherA = new Teacher(student_xuxiaodong);
Log.i("CallBack"," start AA  ....  " );
teacherA.askQuestion();

Student mStudent_XuChi = new Student_XuChi();
Teacher teacherB = new Teacher(mStudent_XuChi);
Log.i("CallBack"," start  BB  ....  " );
teacherB.askQuestion();

Student mStudent_XuRong = new Student_XuRong();
Teacher teacherC = new Teacher(mStudent_XuRong);
Log.i("CallBack"," start CC ....  " );
teacherC.askQuestion();

2. Relieve lotus root

   

//The student tells the teacher information 
package com.example.myapplication; 

public interface Student { 
    public void studentTellTeacherInfo(Callback callback); 
}
//The teacher receives student information 
package com.example.myapplication; 
import android.util.Log; 
public class Teacher implements Callback { 
    private Student student; 
    public Teacher(Student student) { 
        this.student = student; 
    } 
        @Override 
    public void answerTeacherTime (String name ,String work_time) {//How long does the teacher only need to answer? 
        Log.i("CallBack"," Name: "+name); 
        Log.i("CallBack"," Got it, how long did you do your homework" +work_time); 
    } 

    public void askQuestion(){ 
        student.studentTellTeacherInfo(this) ; 
    } 

}

// Student Student_XuChi

package com.example.myapplication;

import android.util.Log;

public class Student_XuChi implements Student {
    @Override
    public void studentTellTeacherInfo(Callback callback) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        callback.answerTeacherTime("Xu_Chi","5000秒");
        Log.i("CallBack"," start Student_XuChi  " +        callback.hashCode());
    }
}

//Student Student_XuRong

package com.example.myapplication;

import android.util.Log;

public class Student_XuChi implements Student {
    @Override
    public void studentTellTeacherInfo(Callback callback) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        callback.answerTeacherTime("Xu_Chi","5000秒");
        Log.i("CallBack"," start Student_XuChi  " +        callback.hashCode());
    }
}

// Student Student_xuxiaodong

package com.example.myapplication;

import android.util.Log;

public class Student_xuxiaodong implements Student {
    @Override
    public void studentTellTeacherInfo(Callback callback) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        callback.answerTeacherTime("xuxiaodong","3000秒");
        Log.i("CallBack"," start Student_xuxiaodong  " +        callback.hashCode());
    }

//    @Override
//    public void answerTeacherTime(String name, String work_time) {
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        Callback.
//    }
}

 

//Implementation

C:\Users\admin>adb logcat -s CallBack
--------- beginning of system
--------- beginning of main
01-30 10:20:37.768 4494 4494 I CallBack: start AA ....
01-30 10:20:40.770 4494 4494 I CallBack: Name: xuxiaodong
01-30 10:20:40.770 4494 4494 I CallBack: I see, it takes 3000 seconds to do your homework
01-30 10:20: 40.771 4494 4494 I CallBack: start Student_xuxiaodong 15652383
01-30 10:20:40.771 4494 4494 I CallBack: start BB ....
01-30 10:20:45.772 4494 4494 I CallBack: Name: Xu_Chi
01-30 10:20 :45.772 4494 4494 I CallBack: I see, it takes 5000 seconds to do your homework
01-30 10:20:45.772 4494 4494 I CallBack: start Student_XuChi 99143020
01-30 10:20:45.772 4494 4494 I CallBack: start CC ... .
01-30 10:20:53.773 4494 4494 I CallBack: Name: Xu_Chi
01-30 10:20:53.773 4494 4494 I CallBack: I see, it takes 8000 seconds to do your homework
01-30 10:20:53.773 4494 4494 I CallBack : start Student_XuRong 4099893

Guess you like

Origin blog.csdn.net/u010689853/article/details/113416113