ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋Š” ๋ฌด์—‡์ผ๊นŒ? ๋ฌด์—‡์ด ์ข‹์„๊นŒ?

Factory Method

  • ์‚ฌ์šฉํ•  ํด๋ž˜์Šค๋ฅผ ์ •ํ™•ํžˆ ๋ชฐ๋ผ๋„ ๊ฐœ์ฒด ์ƒ์„ฑ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” ํŒจํ„ด
public final class Cup {
    private int sizeMl;
 
    private Cup(int sizeMl) {
        this.sizeMl = sizeMl;
    }
 
    public static Cup createOrNull(CupSize size) {
        switch (size) {
            case SMALL:
                return new Cup(355);
            case MEDIUM:
                return new Cup(473);
            case LARGE:
                return new Cup(651);
            default:
                assert(false) : "Unhandled CupSize:" + size;
                return null;
        }
    }
}
  • ์ƒ์„ฑ์ž๊ฐ€ private์ด๋ผ ์ƒ์„ฑ์„ ๋ฌด์กฐ๊ฑด ์ •์  ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ํ•ด์•ผํ•จ
  • ํ•ด๋‹น ์ •์  ํ•จ์ˆ˜์—์„œ ์ปต์˜ ์šฉ๋Ÿ‰์„ ๊ฒฐ์ •ํ•จ

์ƒ์„ฑ์ž ๋Œ€์‹  ์ •์  ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ์˜ ์žฅ์ 

  • null์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ƒ์„ฑ์ž๋Š” ์ƒ์„ฑ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ์˜ˆ์™ธ๋ฅผ ๋˜์งˆ ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.
    • swift๋Š” nilable initializer๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

๋‹คํ˜•์ ์ธ ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ํŒจํ„ด

  • ์ปต ์‚ฌ์ด์ฆˆ๋Š” ๋‚˜๋ผ๋งˆ๋‹ค ๋‹ค๋ฅด๋‹ค.
  • ๊ฐ™์€ small, medium, large์—ฌ๋„ ๊ฐ ๋‚˜๋ผ๋งˆ๋‹ค ํ•ด๋‹น ๊ทœ๊ฒฉ์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ
  • ์ด๋Ÿฐ ๊ฒฝ์šฐ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ• ๊นŒ?

๋ฐฉ๋ฒ•

  1. createOrNull์— ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ country๋ฅผ ๋„ฃ๋Š”๋‹ค.
    • OO ์ ์ธ ๋ฐฉ๋ฒ•์€ ์•„๋‹˜
  2. createOrNull์„ ๋‹คํ˜•์ ์œผ๋กœ ๋งŒ๋“ ๋‹ค.
    • Country์—์„œ ์ด๋ฅผ ์žฌ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉ
    • OO ์ ์ธ ๋ฐฉ๋ฒ•

์˜ˆ์‹œ

// Cup
 
public final class Cup {
    private int sizeMl;
 
    Cup(int sizeMl) { // Package ์ ‘๊ทผ ์ œ์–ด์ž
        this.sizeMl = sizeMl;
    }
 
    public int getSize() {
        return this.sizeMl;
    }
}
 
// ์™œ ์ธํ„ฐํŽ˜์ด์Šค ์‚ฌ์šฉ ์•ˆํ•จ? - ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐˆ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๊ธฐ ๋•Œ๋ฌธ
public abstract class Menu {
    // ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ ์ƒ๋žต
    public abstract Cup createCupOrNull(CupSize size); // ๊ฐ€์ƒ ์ƒ์„ฑ์ž๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Œ
}
 
public class AmericanMenu extends Menu {
    @Override
    public Cup createCupOrNull(CupSize size) {
        switch (size) {
            case SMALL:
                return new Cup(473);
            case MEDIUM:
                return new Cup(621);
            case LARGE:
                return new Cup(887);
            default:
                assert(false) : "Unhandled CupSize:" + size;
                return null;
        }
    }
}
 
// Korean ์ƒ๋žต
  • createCupOrNull(CupSize size)๋Š” ๊ฐ€์ƒ ์ƒ์„ฑ์ž๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Œ

๋‹คํ˜•์ ์ธ ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ํŒจํ„ด 2

  • Cup๋„ ์ถ”์ƒ์ ์ผ ์ˆ˜ ์žˆ๋‹ค.
public abstract class Cup {
    private int sizeMl;
 
    protected Cup(int sizeMl) {
        this.sizeMl = sizeMl;
    }
 
    public int getSize() {
        return this.sizeMl;
    }
}
 
public final class GlassCup extends Cup {
    GlassCup(int sizeMl) {
        super(sizeMl);
    }
}
 
public final class PaperCup extends Cup {
    private Lid lid;
 
    PaperCup(int sizeMl, Lid lid) {
        super(sizeMl);
 
        this.lid = lid;
    }
}
 
// ์ข…์ด์ปต์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณ€๊ฒฝ๋จ
public class AmericanMenu extends Menu {
    @Override
    public Cup createCupOrNull(CupSize size) {
        switch (size) {
            case SMALL:
                return new PaperCup(473);
            case MEDIUM:
                return new PaperCup(621);
            case LARGE:
                return new PaperCup(887);
            default:
                assert(false) : "Unhandled CupSize:" + size;
                return null;
        }
    }
}
  • ๊ตฌ์ฒด์ ์ธ ๊ฒƒ๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์„œ ํ•„์š”ํ•˜๋ฉด ์ถ”์ƒํ™”ํ•ด๋ผ
  • ์ฒ˜์Œ๋ถ€ํ„ฐ ์ถ”์ƒ์ ์ธ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ๋ผ.

ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ์˜ ์žฅ์ 

  • ํด๋ผ์ด์–ธํŠธ๋Š” ๋ณธ์ธ์—๊ฒŒ ์ต์ˆ™ํ•œ ์ธ์ž๋ฅผ ํ†ตํ•ด ๊ฐœ์ฒด ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
    • ml๊ฐ€ ์•„๋‹Œ, ์‚ฌ์ด์ฆˆ๋กœ ์ƒ์„ฑ ๊ฐ€๋Šฅ
  • ์˜ค๋ฅ˜์ผ์‹œ null์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋‹คํ˜•์ ์œผ๋กœ ๊ฐœ์ฒด ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
    • ๊ทธ๋ž˜์„œ ๊ฐ€์ƒ ์ƒ์„ฑ์ž ํŒจํ„ด์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค.

Reference