ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Java] ๋””์ž์ธ ํŒจํ„ด (Factory ํŒจํ„ด)
    Android/Java 2021. 1. 5. 09:43
    ๋ฐ˜์‘ํ˜•
    • ์ด ๊ธ€์€ "์ธํ”„๋Ÿฐ Java ๋””์ž์ธ ํŒจํ„ด" ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

    Factory ํŒจํ„ด์ด๋ž€?

    • Builder ํŒจํ„ด๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํŒจํ„ด์ž…๋‹ˆ๋‹ค.

    • ์ด ํŒจํ„ด์€ ๊ฐ์ฒด ์‚ฌ์ด์˜ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ”๊ธฐ ์œ„ํ•ด ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํŒจํ„ด์ž…๋‹ˆ๋‹ค.

    • Factory ํŒจํ„ด์€ ์ธํ„ฐํŽ˜์ด์Šค๋‚˜ ์ถ”์ƒ ํด๋ž˜์Šค๋กœ ์„ ์–ธ์„ ํ•˜๊ณ  ๋‚ด๋ถ€์—์„œ๋Š” ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ์ด ๋˜๋Š”์ง€ ๋ชจ๋ฅด๋Š”๊ฒŒ ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋Š” ์„œ๋ธŒ ํด๋ž˜์Šค์—์„œ ๊ฒฐ์ •๋œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

    • ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ์ƒˆ๋กœ์šด Fragment๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, Factory ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด import ํ•˜์ง€ ์•Š์•„๋„ ๋˜์–ด ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


    Factory ํŒจํ„ด ์‚ฌ์šฉ

    public interface Subject {
        void describe();
    }
    public interface Factory {
        Subject createFactory(String name);
    }

    ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

    Factory ํŒจํ„ด์„ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋‚˜ ์ถ”์ƒ ํด๋ž˜์Šค๋ฅผ ์„ ์–ธ์„ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    public class Program implements Subject{
        String name;
    
        public Program(String name) {
            this.name = name;
        }
    
        @Override
        public void describe() {
            System.out.println(name+" ์ˆ˜๊ฐ•ํ•ฉ๋‹ˆ๋‹ค");
        }
    }
    public class English implements Subject{
        String name;
    
        public English(String name) {
            this.name = name;
        }
    
        @Override
        public void describe() {
            System.out.println(name+"๋ฅผ ์ˆ˜๊ฐ•ํ•ฉ๋‹ˆ๋‹ค!!");
        }
    }

    ์ดํ›„ Subject ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ implement ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋‘ ๊ฐ€์ง€๋ฅผ ๋งŒ๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

    ๊ทธ๋ฆฌ๊ณ  ์ด ๊ฐ์ฒด๋“ค์„ ์ง์ ‘ new๋กœ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹นํ•˜์—ฌ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ Factory ํŒจํ„ด์„ ํ†ตํ•ด ์บก์Šํ™”๋ฅผ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

    public class SubjectFactory implements Factory {
    
        @Override
        public Subject createFactory(String name) {
            Subject subject = null;
            switch (name){
                case "์˜์–ด":
                    subject = new English(name);
                    break;
                case "ํ”„๋กœ๊ทธ๋ž˜๋ฐ":
                    subject = new Program(name);
                    break;
            }
            return subject;
        }
    }

    SubjetFactory์—์„œ createFactory ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•˜๋ ค๋Š” ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

    import factory.Factory;
    import factory.Subject;
    import factory.SubjectFactory;
    
    public class Main {
        public static void main(String[] args) {
    
            Factory factory = new SubjectFactory();
            Subject subject1 = factory.createFactory("ํ”„๋กœ๊ทธ๋ž˜๋ฐ");
            Subject subject2 = factory.createFactory("์˜์–ด");
    
            subject1.describe();
            subject2.describe();
        }
    }
    • ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋ฉด factory ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ดํ›„ Subject๋กœ ์„ ์–ธ๋งŒ ํ•  ๋ฟ ์„ธ๋ถ€์ ์œผ๋กœ ์–ด๋–ค ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š”์ง€๋Š” ๋‚ด๋ถ€์—์„œ๋งŒ ์•Œ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

    • Program, English์˜ ๊ฐ์ฒด๋ฅผ import ํ•˜๋Š” ๋ถ€๋ถ„๋„ ์—†์Šต๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด Main์—์„œ๋Š” Program๊ณผ English์™€์˜ ๊ฒฐํ•ฉ๋„๊ฐ€ ๋‚ฎ์•„์ง„๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋‘ ๊ฐ€์ง€์˜ ๊ฐ์ฒด๋ฅผ ์ˆ˜์ •ํ•˜๋”๋ผ๋„ Main์˜ ์ฝ”๋“œ๊ฐ€ ์ˆ˜์ •๋  ๊ฐ€๋Šฅ์„ฑ์€ ์ ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

    ๋ฐ˜์‘ํ˜•
Designed by Tistory.