Back-end/Spring

[Spring] ν…œν”Œλ¦Ώ 콜백 νŒ¨ν„΄

WithSeungRyu 2020. 8. 17. 16:12

πŸ’‘ ν…œν”Œλ¦Ώ 콜백 νŒ¨ν„΄μ΄λž€?

 

μ „λž΅ νŒ¨ν„΄μ˜ κΈ°λ³Έ ꡬ쑰에 μΈν„°νŽ˜μ΄μŠ€λ₯Ό μƒμ†ν•˜λŠ” 클래슀λ₯Ό λ§Œλ“€μ§€ μ•Šκ³  읡λͺ… λ‚΄λΆ€ 클래슀λ₯Ό ν™œμš©ν•˜λŠ” 방식

 

이런 방식을 μŠ€ν”„λ§μ—μ„œλŠ” ν…œν”Œλ¦Ώ 콜백 νŒ¨ν„΄μ΄λΌκ³  ν•œλ‹€.

 

 

μ „λž΅ νŒ¨ν„΄μ˜ μ»¨ν…μŠ€νŠΈλ₯Ό ν…œν”Œλ¦Ώμ΄λΌ λΆ€λ₯΄κ³ , 읡λͺ… λ‚΄λΆ€ 클래슀둜 λ§Œλ“€μ–΄μ§€λŠ” 였브젝트λ₯Ό 콜백이라 ν•œλ‹€.

μ „λž΅ νŒ¨ν„΄κ³Ό DI의 μž₯점을 읡λͺ… λ‚΄λΆ€ 클래슀 μ‚¬μš© μ „λž΅κ³Ό κ²°ν•©ν•΄ λ…νŠΉν•˜κ²Œ ν™œμš©

 

주둜 try/catch/finally 블둝을 μ‚¬μš©ν•˜λŠ” μ½”λ“œμ— μ‚¬μš©λœλ‹€.

 

 

πŸ’‘ ν…œν”Œλ¦Ώ 콜백의 λ™μž‘μ›λ¦¬

 

μ½œλ°±μ€ 보톡 단일 λ©”μ†Œλ“œ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•œλ‹€.

 

λ˜ν•œ ν•˜λ‚˜μ˜ ν…œν”Œλ¦Ώμ—μ„œ μ—¬λŸ¬ κ°€μ§€ μ’…λ₯˜μ˜ μ „λž΅μ„ μ‚¬μš©ν•΄μ•Ό ν•œλ‹€λ©΄ ν•˜λ‚˜ μ΄μƒμ˜ 콜백 였브젝트λ₯Ό μ‚¬μš© ν•  수 μžˆλ‹€.

μ½œλ°±μ€ 일반적으둜 ν•˜λ‚˜μ˜ λ©”μ†Œλ“œλ₯Ό κ°€μ§„ μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•œ 읡λͺ… λ‚΄λΆ€ 클래슀둜 λ§Œλ“€μ–΄μ§„λ‹€.

 

콜백 μΈν„°νŽ˜μ΄μŠ€μ˜ λ©”μ†Œλ“œμ—λŠ” 보톡 νŒŒλΌλ―Έν„°κ°€ μžˆλ‹€.

 

 

πŸ’‘ μž‘μ—… 흐름

 

  1. ν΄λΌμ΄μ–ΈνŠΈμ˜ 역할은 콜백 였브젝트λ₯Ό λ§Œλ“€κ³ , 콜백이 μ°Έμ‘°ν•  정보λ₯Ό 제곡
  2. λ§Œλ“€μ–΄μ§„ μ½œλ°±μ€ ν΄λΌμ΄μ–ΈνŠΈκ°€ ν…œν”Œλ¦Ώμ˜ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•  λ•Œ νŒŒλΌλ―Έν„°λ‘œ 전달
  3. ν…œν”Œλ¦Ώμ€ μ •ν•΄μ§„ 흐름을 따라 μž‘μ—…, λ‚΄λΆ€μ—μ„œ μƒμ„±ν•œ 참쑰정보λ₯Ό κ°–κ³  콜백 였브젝트의 λ©”μ†Œλ“œ 호좜
  4. μ½œλ°±μ€ ν΄λΌμ΄μ–ΈνŠΈ λ©”μ†Œλ“œμ— μžˆλŠ” 정보와 ν…œν”Œλ¦Ώμ΄ μ œκ³΅ν•œ 참쑰정보λ₯Ό μ΄μš©ν•΄ μž‘μ—… μˆ˜ν–‰
  5. μž‘μ—… μˆ˜ν–‰ν•œ ν›„ λ‹€μ‹œ ν…œν”Œλ¦Ώμ— λ°˜ν™˜
  6. ν…œν”Œλ¦Ώμ€ 콜백이 λŒλ €μ€€ 정보λ₯Ό μ‚¬μš©ν•΄μ„œ μž‘μ—…μ„ λ§ˆμ € μˆ˜ν–‰
  7. μ΅œμ’… 결과에 따라 ν΄λΌμ΄μ–ΈνŠΈμ— λ‹€μ‹œ λ°˜ν™˜

✏️ 계산기 예제

 

πŸ“„ JUnit ν…ŒμŠ€νŠΈ μ½”λ“œ

public class CalcSumTest {
	Calculator calculator;
	String numFilepath;
	
	@Before public void setUp() {
		this.calculator = new Calculator();
		this.numFilepath = getClass().getResource("/numbers.txt").getPath();
		
	}
	
	@Test
	public void sumOfNumbers() throws IOException {
		assertThat(calculator.calcSum(this.numFilepath), is(10));
	}
	
	@Test
	public void multiplyOfNumbers() throws IOException {
		assertThat(calculator.calcMultiply(this.numFilepath), is(24));
	}
}

 

πŸ“„ 콜백 μΈν„°νŽ˜μ΄μŠ€

import java.io.BufferedReader;
import java.io.IOException;

public interface BufferedReaderCallback {
	Integer doSomethingWithReader(BufferedReader br) throws IOException;
}

 

πŸ“„ ν…œν”Œλ¦Ώ λ©”μ†Œλ“œ

public Integer fileReadTemplate(String filepath, BufferedReaderCallback callback) throws IOException {
		BufferedReader br = null;
		
		try {
			br = new BufferedReader(new FileReader(filepath));
			int ret = callback.doSomethingWithReader(br);
			return ret;
		}catch(IOException e) {
			System.out.println(e.getMessage());
			throw e;
		}
		finally {
			if(br != null) {
				try { br. close();}
				catch(IOException e) {System.out.println(e.getMessage());}
			}
		}
	}

 

πŸ“„ ν…œν”Œλ¦Ώ/μ½œλ°±μ„ μ μš©ν•œ calcSum, calcMultiply λ©”μ†Œλ“œ

public Integer calcSum(String filepath) throws IOException {
		BufferedReaderCallback sumCallback = new BufferedReaderCallback() {
			public Integer doSomethingWithReader(BufferedReader br) throws IOException {
				Integer sum = 0;
				String line = null;
				while((line = br.readLine()) != null) {
					sum += Integer.valueOf(line);
				}
				return sum;
			}
		};
		
		return fileReadTemplate(filepath, sumCallback);

	}
	
	public Integer calcMultiply(String filepath) throws IOException {
		BufferedReaderCallback sumCallback = new BufferedReaderCallback() {
			public Integer doSomethingWithReader(BufferedReader br) throws IOException {
				Integer multiply = 1;
				String line = null;
				while((line = br.readLine()) != null) {
					multiply *= Integer.valueOf(line);
				}
				return multiply;
			}
		};
		
		return fileReadTemplate(filepath, sumCallback);

	}

 

😁 μœ„μ™€ 같이 ν…œν”Œλ¦Ώ 콜백 νŒ¨ν„΄μ„ μ μš©ν•΄ λ§Œλ“  CalculatorλŠ” μ€‘λ³΅λ˜λŠ” μ½”λ“œ 없이, μ˜ˆμ™Έ κΈ°λŠ₯을 κ°–μΆ˜

사칙 μ—°μ‚° λ©”μ†Œλ“œλ₯Ό λ§Œλ“€μ–΄ 쀄 수 μžˆλ‹€.

 

✏️ μž₯점 

  • μ „λž΅νŒ¨ν„΄μ€ μ‚¬μš©ν•˜λŠ” νŒ©ν† λ¦¬ 객체가 ν•„μš”ν•˜λ‹€. 
  • ν•˜μ§€λ§Œ ν…œν”Œλ¦Ώ 콜백 νŒ¨ν„΄μ€ νŒ©ν† λ¦¬ 객체 없이 ν•΄λ‹Ή 객체λ₯Ό μ‚¬μš©ν•˜λŠ” λ©”μ†Œλ“œμ—μ„œ μΈν„°νŽ˜μ΄μŠ€μ˜ μ „λž΅μ„ 선택 ν•  수 μžˆλ‹€.

✏️ 단점

  • μŠ€ν”„λ§ ν”„λ ˆμž„μ›Œν¬μ—μ„œ DIλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ²Œ 되면 싱글톀 객체가 λ˜μ§€ μ•ŠλŠ”λ‹€.
  • μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•˜μ§€λ§Œ, μ‹€μ œ μ‚¬μš©ν•  클래슀λ₯Ό 직접 μ„ μ–Έν•˜λ―€λ‘œ 결합도가 μ¦κ°€ν•œλ‹€.

 

μ°Έκ³  λ¬Έμ„œ : 

ν† λΉ„μ˜ μŠ€ν”„λ§3 (이일민)

λ°˜μ‘ν˜•