Java

[Effective Java] ์•„์ดํ…œ 34. int ์ƒ์ˆ˜ ๋Œ€์‹  ์—ด๊ฑฐ ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋ผ.

quedevel 2023. 3. 21. 10:30
728x90
๋ฐ˜์‘ํ˜•

๐ŸŽฏ ์•„์ดํ…œ 34. int ์ƒ์ˆ˜ ๋Œ€์‹  ์—ด๊ฑฐ ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋ผ.

  • ์ •์ˆ˜ ์—ด๊ฑฐ ํŒจํ„ด - ์ƒ๋‹นํžˆ ์ทจ์•ฝํ•˜๋‹ค!

    public class IntegerConstants {
      public static final int APPLE_FUJI = 0;
      public static final int APPLE_PIPPIN = 1;
      public static final int APPLE_GRANNY_SMITH = 2;
    
      public static final int ORANGE_NAVEL = 0;
      public static final int ORANGE_TEMPLE = 1;
      public static final int ORANGE_BLOOD = 2;
    }

    ์œ„ ๊ฐ™์€ ์ •์ˆ˜ ์—ด๊ฑฐ ํŒจํ„ด์€ ํƒ€์ž… ์•ˆ์ „์„ ๋ณด์žฅํ•  ๋ฐฉ๋ฒ•์ด ์—†์œผ๋ฉฐ ํ‘œํ˜„๋ ฅ๋„ ์ข‹์ง€ ์•Š๋‹ค.

    ์˜ค๋ Œ์ง€๋ฅผ ๊ฑด๋„ค์•ผ ํ•  ๋ฉ”์„œ๋“œ์— ์‚ฌ๊ณผ๋ฅผ ๋ณด๋‚ด๊ณ  ๋™๋“ฑ ์—ฐ์‚ฐ์ž(==)๋กœ ๋น„๊ตํ•˜๋”๋ผ๋„ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์•„๋ฌด๋Ÿฐ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค.


  • ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ์—ด๊ฑฐ ํƒ€์ž…
    public enum Apple { FUJI, PIPPIN, GRANNY_SMITH }
    public enum Orange { NAVEL, TEMPLE, BLOOD }
    ์ž๋ฐ”์˜ ์—ด๊ฑฐ ํƒ€์ž…์€ ์™„์ „ํ•œ ํ˜•ํƒœ์˜ ํด๋ž˜์Šค๋ผ์„œ ๋‹ค๋ฅธ ์–ธ์–ด์˜ ์—ด๊ฑฐ ํƒ€์ž…๋ณด๋‹ค ํ›จ์”ฌ ๊ฐ•๋ ฅํ•˜๋‹ค.

    ์—ด๊ฑฐ ํƒ€์ž…์€ ๋ฐ–์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์‚ฌ์‹ค์ƒ final์ด๋‹ค.

    ๋”ฐ๋ผ์„œ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ธ์Šคํ„ด์Šค๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ํ™•์žฅ ํ•  ์ˆ˜ ์—†์œผ๋‹ˆ ์—ด๊ฑฐ ํƒ€์ž… ์„ ์–ธ์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ์ธ์Šคํ„ด์Šค๋“ค์€ ๋”ฑ ํ•˜๋‚˜์”ฉ๋งŒ ์กด์žฌํ•จ์ด ๋ณด์žฅ๋œ๋‹ค.

    if. Apple ์—ด๊ฑฐ ํƒ€์ž…์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธํ–ˆ๋‹ค๋ฉด, ๊ฑด๋„ค๋ฐ›์€ ์ฐธ์กฐ๋Š” Apple์˜ ์„ธ ๊ฐ€์ง€ ๊ฐ’ ์ค‘ ํ•˜๋‚˜์ž„์ด ํ™•์‹คํ•˜๋‹ค.

    ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๊ฐ’์„ ๋„˜๊ธฐ๋ ค ํ•˜๋ฉด ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค.

  • ๋ฐ์ดํ„ฐ์™€ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ–๋Š” ์—ด๊ฑฐ ํƒ€์ž…

    public enum Planet {
      MERCURY(3.302e+23, 2.439e6),
      VENUS  (4.869e+24, 6.052e6),
      EARTH  (5.975e+24, 6.378e6),
      MARS   (6.419e+23, 3.393e6),
      JUPITER(1.899e+27, 7.149e7),
      SATURN (5.685e+26, 6.027e7),
      URANUS (8.683e+25, 2.556e7),
      NEPTUNE(1.024e+26, 2.477e7);
    
      private final double mass;           // ์งˆ๋Ÿ‰(๋‹จ์œ„: ํ‚ฌ๋กœ๊ทธ๋žจ)
      private final double radius;         // ๋ฐ˜์ง€๋ฆ„(๋‹จ์œ„: ๋ฏธํ„ฐ)
      private final double surfaceGravity; // ํ‘œ๋ฉด์ค‘๋ ฅ(๋‹จ์œ„: m / s^2)
    
      // ์ค‘๋ ฅ์ƒ์ˆ˜(๋‹จ์œ„: m^3 / kg s^2)
      private static final double G = 6.67300E-11;
    
      // ์ƒ์„ฑ์ž
      Planet(double mass, double radius) {
          this.mass = mass;
          this.radius = radius;
          surfaceGravity = G * mass / (radius * radius);
      }
    
      public double mass()           { return mass; }
      public double radius()         { return radius; }
      public double surfaceGravity() { return surfaceGravity; }
    
      public double surfaceWeight(double mass) {
          return mass * surfaceGravity;  // F = ma
      }
    }

    ์—ด๊ฑฐ ํƒ€์ž… ์ƒ์ˆ˜ ๊ฐ๊ฐ์„ ํŠน์ • ๋ฐ์ดํ„ฐ์™€ ์—ฐ๊ฒฐ์ง€์œผ๋ ค๋ฉด ์ƒ์„ฑ์ž์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ์ธ์Šคํ„ด์Šค ํ•„๋“œ์— ์ €์žฅํ•˜๋ฉด๋œ๋‹ค.


์œ„์˜ Planet ์˜ˆ์—์„œ ๋ณด์—ฌ์ค€ ํŠน์„ฑ๋งŒ์œผ๋กœ ์—ฐ๊ฑฐ ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋Š” ์ƒํ™ฉ ๋Œ€๋‹ค์ˆ˜๋ฅผ ํ›Œ๋ฅญํžˆ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ƒ์ˆ˜๊ฐ€ ๋” ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์คฌ์œผ๋ฉด ํ•  ๋•Œ๋„ ์žˆ๋‹ค.


  • ๊ฐ’์— ๋”ฐ๋ผ ๋ถ„๊ธฐํ•˜๋Š” ์—ด๊ฑฐ ํƒ€์ž… - ์ด๋Œ€๋กœ ๋งŒ์กฑํ•˜๋Š”๊ฐ€?

    public enum Operation {
      PLUS, MINUS, TIMES, DIVIDE;
    
      public double apply(double x, double y){
          switch (this){
              case PLUS: return x+y;
              case MINUS: return x-y;
              case TIMES: return x*y;
              case DIVIDE: return x/y;
          }
          throw new AssertionError("์•Œ ์ˆ˜ ์—†๋Š” ์—ฐ์‚ฐ: " + this);
      }
    }

    ์œ„ ์†Œ์Šค๋Š” ๋™์ž‘์€ ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ์—ฐ์‚ฐ์„ ์ถ”๊ฐ€ํ• ๋•Œ๋Š” case๋ฌธ์„ ์ถ”๊ฐ€๋ฅผ ํ•ด์•ผํ•œ๋‹ค.

    ๋งŒ์•ฝ ๊นŒ๋จน๊ณ  case๋ฌธ์€ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋‹ค ํ•˜๋”๋ผ๋„ ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ณ  ๋Ÿฐํƒ€์ž…์ค‘ "์•Œ ์ˆ˜ ์—†๋Š” ์—ฐ์‚ฐ"์ด๋ผ๋Š” ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒํ•œ๋‹ค.

    ๋”ฐ๋ผ์„œ ์ƒ์ˆ˜๋ณ„ ํด๋ž˜์Šค ๋ชธ์ฒด, ์ฆ‰ ๊ฐ ์ƒ์ˆ˜์—์„œ ์ž์‹ ์— ๋งž๊ฒŒ ์žฌ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.


  • ์ƒ์ˆ˜๋ณ„ ๋ฉ”์„œ๋“œ ๊ตฌํ˜„์„ ํ™œ์šฉํ•œ ์—ด๊ฑฐ ํƒ€์ž…

    public enum Operation2 {
      PLUS { public double apply(double x, double y){ return x+y; } },
      MINUS { public double apply(double x, double y){ return x-y; } },
      TIMES { public double apply(double x, double y){ return x*y; } },
      DIVIDE { public double apply(double x, double y){ return x/y; } };
    
      public abstract double apply(double x, double y); // ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ์„ ์–ธ
    }

    ์œ„ ์ฒ˜๋Ÿผ ์ถ”์ƒ๋ฉ”์„œ๋“œ apply๋ฅผ ์„ ์–ธํ•˜๋ฉด ๋‹ค๋ฅธ ์—ฐ์‚ฐ์„ ์ถ”๊ฐ€ํ•  ๋•Œ๋„ ๋ฉ”์„œ๋“œ ๊ตฌํ˜„์ฒด๋ฅผ ๊ฐ™์ด ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด

    ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ์ „ํ•˜๋‹ค.


์ฐธ๊ณ  ์ž๋ฃŒ

Joshua Bloch, ใ€ŽEffective Java 3/Eใ€, ๊ฐœ์•ž๋งต์‹œ ์˜ฎ๊น€, ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ธ์‚ฌ์ดํŠธ(2018)
http://www.kyobobook.co.kr/product/detailViewKor.laf?ejkGb=KOR&mallGb=KOR&barcode=9788966262281&orderClick=LEa&Kc=

728x90
๋ฐ˜์‘ํ˜•