[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item18 ์™„๋ฒฝ๊ณต๋žต. ์ฝœ๋ฐฑ ํ”„๋ ˆ์ž„์›Œํฌ์™€ ์…€ํ”„ ๋ฌธ์ œ

2023. 3. 2. 16:56ใ†JAVA/Effective JAVA

728x90

 

item18. ์ƒ์†๋ณด๋‹ค๋Š” ์ปดํฌ์ง€์…˜์„ ์‚ฌ์šฉํ•˜๋ผ.

" p.119 ์ฝœ๋ฐฑ ํ”„๋ ˆ์ž„์›Œํฌ์™€ ์…€ํ”„๋ฌธ์ œ"

 

 

๋ž˜ํผํด๋ž˜์Šค์—๋Š” ๋‹จ์ ์ด ๊ฑฐ์˜ ์—†๋Š”๋ฐ, ํ•œ๊ฐ€์ง€ ์žˆ๋‹ค๋ฉด ๋ž˜ํผ ํด๋ž˜์Šค๊ฐ€ ์ฝœ๋ฐฑ callback ํ”„๋ ˆ์ž„์›Œํฌ์™€๋Š” ์–ด์šธ๋ฆฌ์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ๋งŒ ์ฃผ์˜ํ•˜๋ฉด ๋œ๋‹ค. 

์ฝœ๋ฐฑ ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ๋Š” ์ž๊ธฐ ์ž์‹ ์˜ ์ฐธ์กฐ๋ฅผ ๋‹ค๋ฅธ ๊ฐ์ฒด์— ๋„˜๊ฒจ์„œ ๋‹ค์Œ ํ˜ธ์ถœ(์ฝœ๋ฐฑ)๋•Œ ์‚ฌ์šฉํ•˜๋„๋ก ํ•œ๋‹ค. 

๋‚ด๋ถ€ ๊ฐ์ฒด๋Š” ๊ทผ๋ฐ ์ž์‹ ์„ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ๋ž˜ํผ์˜ ์กด์žฌ๋ฅผ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ž๊ธฐ ์ž์‹  this๋ฅผ ์ฐธ์กฐ๋กœ ๋„˜๊ธฐ๊ณ  , ์ฝœ๋ฐฑ ๋•Œ๋Š” ๋ž˜ํผ๊ฐ€ ์•„๋‹Œ ๋‚ด๋ถ€ ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋ฅผ self ๋ฌธ์ œ๋ผ๊ณ  ํ•œ๋‹ค. 

 

 

์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ž€? 

: ๋‹ค๋ฅธ ํ•จ์ˆ˜ A์˜ ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ํ•จ์ˆ˜ B๋กœ, ํ•ด๋‹น ํ•จ์ˆ˜A ๋‚ด๋ถ€์—์„œ ํ•„์š”ํ•œ ์‹œ์ ์— ํ˜ธ์ถœ๋  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜ B๋ฅผ ์˜๋ฏธํ•œ๋‹ค. 

public interface FunctionToCall
{
   
   void call();
   
   void run();
}
class BobFunction implements FunctionToCall
{
   private final Service service;
   
   BobFunction(Service service)
   {
      this.service = service;
   }
   
   @Override
   public void call()
   {
      System.out.println("----- call -----");
   }
   
   @Override
   public void run()
   {
      this.service.run(this);
   }
}

-> ์ž๊ธฐ ์ž์‹  this๋ฅผ ๋„˜๊ฒจ์„œ ํ•„์š”ํ•  ๋•Œ ํ˜ธ์ถœํ•ด ์‚ฌ์šฉํ•˜๋„๋ก ํ•œ๋‹ค. 

 

public class Service
{
   public void run(FunctionToCall functionToCall)
   {
      System.out.println("------ do later ------");
      functionToCall.call();
   }
   
   public static void main(String[] args)
   {
      Service service = new Service();
      BobFunction bobFunction = new BobFunction(service);
	  bobFunction.run();
   }
}

 

 

public class BobFunctionWrapper implements FunctionToCall
{
   private final BobFunction bobFunction;
   
   public BobFunctionWrapper(BobFunction bobFunction)
   {
      this.bobFunction = bobFunction;
   }
   
   @Override
   public void call()
   {
      this.bobFunction.call();
      System.out.println("----- another call -----");
   }
   
   @Override
   public void run()
   {
      this.bobFunction.run();
   }
}

๊ทผ๋ฐ ์ด์ œ ์—ฌ๊ธฐ์„œ ๋ž˜ํผ ํฌ๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ FunctionCall์„ ๊ฐ์‹ธ๊ณ , 

public class Service
{
   public void run(FunctionToCall functionToCall)
   {
      System.out.println("------ do later ------");
      functionToCall.call();
   }
   
   public static void main(String[] args)
   {
      Service service = new Service();
      BobFunction bobFunction = new BobFunction(service);
      BobFunctionWrapper bobFunctionWrapper = new BobFunctionWrapper(bobFunction);
//    bobFunction.run();
      bobFunctionWrapper.run();
   }
}

BobFunctionWrapper์—์„œ run์„ ํ•˜๋ฉด ๋‚ด๋ถ€ ๊ฐ์ฒด๋Š” BobFunctionWrapper์˜ ์กด์žฌ๋ฅผ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ž๊ธฐ ์ž์‹  bobFunction์„ ๋„˜๊ธฐ๊ณ , ์ฝœ๋ฐฑ ๋•Œ๋Š” ๋ ˆํผ๊ฐ€ ์•„๋‹Œ ๋‚ด๋ถ€ ๊ฐ์ฒด๋ฅผ callํ•˜๊ฒŒ ๋œ๋‹ค. 

์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ self ๋ฌธ์ œ๋ผ๊ณ  ํ•œ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90