안드로이드 스튜디오에서 Interface CallbackEvent를 생성하고 나서 EventRegistration Class 에서 CallbackEvent 변수를 선언하고 생성자 만들기를 하면 자동으로 생성자가 만들어지는 걸 볼 수 있다.
public interface CallbackEvent { // 1 Step 인터페이스 정의 public void callbackMethod(); } |
public class EventRegistration { // 2 Step 변수 선언 CallbackEvent callbackEvent;
// 3 Step 생성자 생성 public EventRegistration(CallbackEvent callbackEvent) { System.out.println("2. EventRegistration Constructor"); this.callbackEvent = callbackEvent; }
public void doWork(){ System.out.println("4. doWork 메소드"); callbackEvent.callbackMethod(); } } |
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
System.out.println("1. MainActivity"); CallbackEvent callbackEvent = new CallbackEvent() { @Override public void callbackMethod() { System.out.println("5. call callback method from callee"); } };
EventRegistration eventRegistration = new EventRegistration(callbackEvent); System.out.println("3. eventRegistration.doWork()"); eventRegistration.doWork(); } } |
생성자 생성과 동시에 EventRegistration(CallbackEvent callbackEvent) 를 지정하는 방법과
별도 setCallbackEvent(CallbackEvent callbackEvent) 를 하는 방법으로 구분해서 해보고 있다.
public interface CallbackEvent { // 1 Step 인터페이스 정의 public void callbackMethod(); } |
public class EventRegistration { Context mContext; // 2 Step 변수 선언 CallbackEvent callbackEvent;
// 3 Step 생성자 선언 public EventRegistration(Context context) { System.out.println("3. EventRegistration Constructor"); mContext = context; }
// 4 Step CallbackEvent Set public void setCallbackEvent(CallbackEvent callbackEvent){ System.out.println("5. setCallbackEvent"); this.callbackEvent = callbackEvent; }
public void doWork(){ System.out.println("7. doWork 메소드 응답"); callbackEvent.callbackMethod(); } } |
public class MainActivity extends AppCompatActivity { Context context;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = getBaseContext();
System.out.println("1. MainActivity 실행");
System.out.println("2. EventRegistration 생성자 호출"); EventRegistration eventRegistration = new EventRegistration(context);
System.out.println("4. setCallbackEvent 연동"); eventRegistration.setCallbackEvent(new CallbackEvent() { @Override public void callbackMethod() { System.out.println("8. call callback method from callee"); } });
System.out.println("6. eventRegistration.doWork() 실행"); eventRegistration.doWork(); } } |
예제3.
안드로이드에서 보편적으로 사용되는 예제이다.
public interface CallbackEvent { void callbackMethod(); }
|
import android.content.Context;
public class NewClass { private Context mContext; private CallbackEvent callbackEvent;
public NewClass(Context mContext) { System.out.println("2. NewClass Constructor"); this.mContext = mContext; }
public void setCallbackEvent(CallbackEvent callbackEvent){ System.out.println("3. setCallbackEvent"); this.callbackEvent = callbackEvent; }
public void doWork(){ //Do somthing...
//call back main callbackEvent.callbackMethod(); System.out.println("5. doWork 메소드 응답"); } }
|
import android.content.Context; import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements CallbackEvent { Context mContext; NewClass newClass;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; System.out.println("1. MainActivity 실행"); doSomething(); }
private void doSomething(){ newClass = new NewClass(mContext); newClass.setCallbackEvent(this); newClass.doWork(); System.out.println("6. newClass.doWork()"); }
@Override public void callbackMethod() { System.out.println("4. callbackMethod from callee."); } }
|