Skip to content

Latest commit

ย 

History

History
92 lines (64 loc) ยท 3.31 KB

factory.md

File metadata and controls

92 lines (64 loc) ยท 3.31 KB

ํŒฉํ† ๋ฆฌ (Factory) ๋””์ž์ธ ํŒจํ„ด

์ž‘์„ฑ์ž : ์„œ๊ทธ๋ฆผ

ํŒฉํ† ๋ฆฌ ๋””์ž์ธ ํŒจํ„ด์ด๋ž€ ๊ฐ์ฒด ์ƒ์„ฑ ์ฒ˜๋ฆฌ๋ฅผ ๋ณ„๋„์˜ ํด๋ž˜์Šค(ํŒฉํ† ๋ฆฌ)์—๊ฒŒ ๋งก๊ธฐ๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ A a = new A(); ์ด๋Ÿฐ ์‹์œผ๋กœ ํ•˜์ง€ ์•Š๊ณ , Factory.getInstance("A"); ์ฒ˜๋Ÿผ ํŒฉํ† ๋ฆฌ์—๊ฒŒ ๊ฐ์ฒด ์ƒ์„ฑ์„ ๋ถ€ํƒํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ํŒฉํ† ๋ฆฌ ๋””์ž์ธ ํŒจํ„ด์„ ์ ์šฉํ•˜๋ฉด ์ถ”ํ›„ ๊ฐ์ฒด ์ƒ์„ฑ์˜ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์ƒ๊ฒผ์„ ๋•Œ ์ˆ˜์ •์ด ์šฉ์ดํ•˜๋‹ค.

ํŒฉํ† ๋ฆฌ ๋””์ž์ธ ํŒจํ„ด ๊ตฌํ˜„

Factory ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด Product ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด์ž.

Product Interface

interface Product {
    public void sell();
}

Product Interface๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค Pencil, Note

class Pencil implements Product {

    @Override
    public void sell() {
        System.out.println("sell Pencil !!!");
    }
}

class Note implements Product {

    @Override
    public void sell() {
        System.out.println("sell Note !!!");
    }
}

Product ๊ฐ์ฒด ์ƒ์„ฑ

ํŒฉํ† ๋ฆฌ ๋””์ž์ธ ํŒจํ„ด์„ ์ ์šฉํ•˜์ง€ ์•Š์•˜์„ ๋•Œ

Product pencil = new Pencil();
pencil.sell();

ํŒฉํ† ๋ฆฌ ๋””์ž์ธ ํŒจํ„ด์„ ์ ์šฉํ•œ๋‹ค๋ฉด

ํŒฉํ† ๋ฆฌ ๋””์ž์ธ ํŒจํ„ด์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” Factory ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค.

์ด ๋•Œ, Factory ํด๋ž˜์Šค์˜ ๊ฐ์ฒด ์ƒ์„ฑ ๋ฉ”์„œ๋“œ๋Š” static์œผ๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ์™ธ๋ถ€์—์„œ๋Š” Factory ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์ „ํ˜€ ๊ด€์‹ฌ์ด ์—†๊ณ , ๋ฌธ์ž์—ด์„ ํ†ตํ•ด ์ƒ์„ฑํ•œ ๊ฐ์ฒด์™€ ๊ทธ ์ธํ„ฐํŽ˜์ด์Šค์—๋งŒ ๊ด€์‹ฌ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

class ProductFactory {
    public static Product getProduct(String className) {
        Product p = null;
        switch (className) {
            case "Pencil": p = new Pencil(); break;
            case "Note": p = new Note(); break;
        }
        return p;
    }
}

Factory ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

Product pencil = ProductFactory.getProduct("Pencil");
pencil.sell();

/*
        // getProduct ๋ฉ”์„œ๋“œ๋ฅผ static์œผ๋กœ ์„ ์–ธํ•˜์ง€ ์•Š์•˜์„ ๋•Œ
        ProductFactory factory = new ProductFactory();
        Product pencil = factory.getTransportation("A");
 */

Factory Java ์ „์ฒด ๊ตฌํ˜„ โ–ถ๏ธ FactoryTest.java

Factory Design Pattern์˜ ์žฅ์ 

  1. ๊ณ„์ธต ๊ฐ„ ์˜์กด๋„๊ฐ€ ๋‚ฎ์•„์ง„๋‹ค.

    ๋งŒ์ผ A ํด๋ž˜์Šค ํŒŒ์ผ์ด ๋ณ€๊ฒฝ/์‚ญ์ œ ๋˜์—ˆ์„ ๋•Œ, ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑ(new A())ํ–ˆ์„ ๋•Œ๋Š” ๊ฐ์ฒด ์ƒ์„ฑ ์ฝ”๋“œ๋ฅผ ๋‹ค ๊ณ ์ณ์•ผ ํ•œ๋‹ค. ๋ฐ˜๋ฉด ํŒฉํ† ๋ฆฌ ๋””์ž์ธ ํŒจํ„ด์„ ์“ฐ๋ฉด, Factory ์ฝ”๋“œ๋งŒ ๊ณ ์น˜๋ฉด ๋œ๋‹ค.

    ์ฆ‰ ํด๋ž˜์Šค์— ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์ƒ๊ฒผ์„ ๋•Œ, ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ์‚ฌ์šฉํ•˜๋Š” main ๋ฉ”์„œ๋“œ ์ชฝ์—์„œ๋Š” ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค. ๋Œ€์‹  interface๋Š” ๋ฐ˜๋“œ์‹œ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. Factory๋ฅผ ํ†ตํ•ด return ๋˜์–ด ์˜ค๋Š” ์‹ค์ œ ๊ฐ์ฒด ๋…€์„์—๊ฒŒ๋Š” ๊ด€์‹ฌ์ด ์—†๊ณ , interface์—๋งŒ ๊ด€์‹ฌ์ด ์žˆ์œผ๋ฉฐ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์— ๋”ฐ๋ผ์„œ interface์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค.

  2. ์™ธ๋ถ€์— ์†Œํ†ต ๊ตฌ์กฐ๊ฐ€ ์ƒ๊ธด๋‹ค.

    ๋ฌธ์ž์—ด์„ ํ†ตํ•ด ๊ฐ์ฒด ์ƒ์„ฑ์„ ์š”์ฒญํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์™ธ๋ถ€์—์„œ ๋ฌธ์ž์—ด์„ ์ฝ์–ด์™€ ๊ทธ ๋ฌธ์ž์—ด๋กœ ๊ฐ์ฒด ์ƒ์„ฑ์„ ํ˜ธ์ถœํ•˜๋Š” ๋“ฑ์˜ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ์™ธ๋ถ€ ์†Œํ†ต ๊ตฌ์กฐ๊ฐ€ ์ƒ๊ธฐ๋ฉฐ ํ•˜๋“œ์ฝ”๋”ฉ ํ•˜๋Š” ๊ฒƒ์ด ๋งŽ์ด ์ค„์–ด๋“ค ์ˆ˜ ์žˆ๋‹ค.