Ejemplos de alta cohesión y bajo acoplamiento de devoluciones de llamada de Java

1. La información de actualización general y transferencia a diferentes partes, aquí es alta cohesión

Estudiante student_xuxiaodong = new Student_xuxiaodong (); 
Profesor teacherA = nuevo profesor (student_xuxiaodong); 
Log.i ("CallBack", "iniciar AA ...."); 
teacherA.askQuestion (); 

Estudiante mStudent_XuChi = new Student_XuChi (); 
Profesor teacherB = nuevo profesor (mStudent_XuChi); 
Log.i ("CallBack", "iniciar BB ...."); 
teacherB.askQuestion (); 

Estudiante mStudent_XuRong = new Student_XuRong (); 
Profesor teacherC = nuevo profesor (mStudent_XuRong); 
Log.i ("CallBack", "iniciar CC ...."); 
teacherC.askQuestion ();

2. Aliviar la raíz de loto

   

// El alumno le dice al 
paquete de información del profesor com.example.myapplication; 

public interface Student { 
    public void studentTellTeacherInfo (Callback callback); 
}
// El profesor recibe el 
paquete de información del alumno com.example.myapplication; 
import android.util.Log; 
clase pública El profesor implementa la devolución de llamada { 
    Private Student student; 
    public Teacher (Student student) { 
        this.student = student; 
    } 
        @Override 
    public void answerTeacherTime (String name, String work_time) {// ¿Cuánto tiempo solo necesita responder el profesor? 
        Log.i ("CallBack", "Nombre:" + nombre); 
        Log.i ("CallBack", "Entendido, cuánto tiempo hiciste tu tarea" + work_time); 
    } 

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

}

// Estudiante Student_XuChi

package com.example.myapplication; 

importar android.util.Log; 

la clase pública Student_XuChi implementa Student { 
    @Override 
    public void studentTellTeacherInfo (devolución de llamada) { 
        try { 
            Thread.sleep (5000); 
        } catch (InterruptedException e) { 
            e.printStackTrace (); 
        } 
        callback.answerTeacherTime ("Xu_Chi", "5000 秒"); 
        Log.i ("CallBack", "iniciar Student_XuChi" + callback.hashCode ()); 
    } 
}

// Estudiante Student_XuRong

package com.example.myapplication; 

importar android.util.Log; 

la clase pública Student_XuChi implementa Student { 
    @Override 
    public void studentTellTeacherInfo (devolución de llamada) { 
        try { 
            Thread.sleep (5000); 
        } catch (InterruptedException e) { 
            e.printStackTrace (); 
        } 
        callback.answerTeacherTime ("Xu_Chi", "5000 秒"); 
        Log.i ("CallBack", "iniciar Student_XuChi" + callback.hashCode ()); 
    } 
}

// Estudiante Student_xuxiaodong

package com.example.myapplication; 

importar android.util.Log; 

clase pública Student_xuxiaodong implementa Estudiante { 
    @Override 
    public void studentTellTeacherInfo (devolución de llamada) { 
        try { 
            Thread.sleep (3000); 
        } catch (InterruptedException e) { 
            e.printStackTrace (); 
        } 
        callback.answerTeacherTime ("xuxiaodong", "3000 秒"); 
        Log.i ("CallBack", "iniciar Student_xuxiaodong" + callback.hashCode ()); 
    } 

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

 

//Implementación

C: \ Users \ admin> adb logcat -s CallBack
--------- comienzo del sistema
--------- comienzo del principal
01-30 10: 20: 37.768 4494 4494 I CallBack: inicio AA ....
01-30 10: 20: 40.770 4494 4494 I CallBack: Nombre: xuxiaodong
01-30 10: 20: 40.770 4494 4494 I CallBack: Ya veo, se necesitan 3000 segundos para hacer su tarea
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: Nombre: Xu_Chi
01-30 10 : 20: 45.772 4494 4494 I CallBack: Ya veo, se necesitan 5000 segundos para hacer su tarea
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: Nombre: Xu_Chi
01-30 10: 20: 53.773 4494 4494 I CallBack: Ya veo, se necesitan 8000 segundos para hacer su tarea
01-30 10: 20: 53.773 4494 4494 I CallBack: iniciar Student_XuRong 4099893

Supongo que te gusta

Origin blog.csdn.net/u010689853/article/details/113416113
Recomendado
Clasificación