[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item20 ์™„๋ฒฝ๊ณต๋žต. ํ…œํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ ํŒจํ„ด

2023. 3. 6. 16:39ใ†JAVA/Effective JAVA

728x90

 

item20. ์ถ”์ƒํด๋ž˜์Šค๋ณด๋‹ค ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์šฐ์„ ํ•˜๋ผ. 

" p.132 ํ…œํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ ํŒจํ„ด"

 

 

๐Ÿ™‚ ํ…œํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ ํŒจํ„ด์ด๋ž€? 

: ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌ์กฐ๋ฅผ ์„œ๋ธŒ ํด๋ž˜์Šค๊ฐ€ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋„๋ก ํ…œํ”Œ๋ฆฟ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๋ฐฉ๋ฒ• 

 

์ „์ฒด์ ์œผ๋กœ๋Š” ๋™์ผํ•œ๋ฐ ๋ถ€๋ถ„์ ์œผ๋กœ ๋‹ค๋ฅธ ๋ถ€๋ถ„์„ ๋”ฐ๋กœ ํ™•์žฅํ•ด์„œ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ฝ”๋“œ ์ค‘๋ณต์„ ์ตœ์†Œํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค!

 

 

public abstract class FileProcessor
{
   private String path;
   
   public FileProcessor(String path)
   {
      this.path = path;
   }
   
   // templateMethod()
   public final int process()
   {
      try(BufferedReader reader = new BufferedReader(new FileReader(path)))
      {
         int result = 0;
         String line = null;
         while((line = reader.readLine()) != null)
         {
            result = getResult(result, Integer.parseInt(line));
         }
         return result;
      }
      catch (IOException e)
      {
         throw new IllegalArgumentException(path + "์— ํ•ด๋‹นํ•˜๋Š” ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค.", e);
      }
   }
   
   // step1()
   protected abstract int getResult(int result, int parseInt);
}

process ๋ฉ”์„œ๋“œ๊ฐ€ ์œ„์˜ ๊ทธ๋ฆผ์—์„œ templatemethod์— ํ•ด๋‹นํ•˜๊ณ  getResult()๊ฐ€ ์ด์ œ ๊ฐ๊ฐ ํ™•์žฅํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์œผ๋กœ step1์— ํ•ด๋‹นํ•œ๋‹ค. 

 

public class Plus extends FileProcessor
{
   public Plus(String path)
   {
      super(path);
   }
   
   @Override
   protected int getResult(int result, int parseInt)
   {
      return result + parseInt;
   }
}

FileProcessor๋ฅผ ์ƒ์†๋ฐ›์•„์„œ getResult๋ฅผ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

 

public static void main(String[] args)
{
   FileProcessor fileProcessor = new Plus("number.txt");
   System.out.println(fileProcessor.process());
}

 

 

๊ทผ๋ฐ ์—ฌ๊ธฐ์„œ ์ด์ œ ์ƒ์†์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋„ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ…œํ”Œ๋ฆฟ ์ฝœ๋ฐฑ ํŒจํ„ด์ด ์žˆ๋‹ค. 

๐Ÿ˜š ํ…œํ”Œ๋ฆฟ ์ฝœ๋ฐฑ ํŒจํ„ด์ด๋ž€? 

๊ตฌ์ฒดํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ์ต๋ช… ๋‚ด๋ถ€ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ 

public  class CallbackFileProcessor
{
   private String path;
   
   public CallbackFileProcessor(String path)
   {
      this.path = path;
   }
   
   public final int process(BiFunction<Integer, Integer, Integer> operator)
   {
      try(BufferedReader reader = new BufferedReader(new FileReader(path)))
      {
         int result = 0;
         String line = null;
         while((line = reader.readLine()) != null)
         {
            result = operator.apply(result, Integer.parseInt(line));
         }
         return result;
      }
      catch (IOException e)
      {
         throw new IllegalArgumentException(path + "์— ํ•ด๋‹นํ•˜๋Š” ํŒŒ์ผ์ด ์—†์Šต๋‹ˆ๋‹ค.", e);
      }
   }
}
public static void main(String[] args)
{
   FileProcessor fileProcessor = new Plus("number.txt");
   System.out.println(fileProcessor.process());
   
   CallbackFileProcessor callbackFileProcessor = new CallbackFileProcessor("number.txt");
   int result = callbackFileProcessor.process((a, b) -> a + b);
   System.out.println("result = " + result);
}

ํด๋ผ์ด์–ธํŠธ๋‹จ์—์„œ ์ต๋ช… ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์ „๋žต์„ ๊ทธ๋•Œ ๊ทธ๋•Œ ์ •ํ•ด์ฃผ๋ฉด์„œ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค!! 

ํ…œํ”Œ๋ฆฟ ์ฝœ๋ฐฑ ํŒจํ„ด์€ ์Šคํ”„๋ง ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ๋กœ ๋ณผ ์ˆ˜ ์žˆ๋Š” ํŒจํ„ด์ด๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90