๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ ํŒ/์„œ๋น„์Šค & ์•„ํ‚คํ…์ฒ˜

์ƒ์„ฑ ํŒจํ„ด

by 1mj 2022. 10. 28.

์ƒ์„ฑ ํŒจํ„ด

์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“œ๋Š” ์ ˆ์ฐจ๋ฅผ ์ถ”์ƒํ™”ํ•˜๋Š” ํŒจํ„ด์ด๋‹ค.

  • ์‹œ์Šคํ…œ์ด ์–ด๋–ค ๊ตฌ์ฒด ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์บก์Šํ™”ํ•œ๋‹ค.
  • ๊ฐ์ฒด์˜ ์ƒ์„ฑ/์กฐํ•ฉ์„ ์บก์Šํ™”ํ•˜์—ฌ ํŠน์ • ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝ๋˜์–ด๋„ ํ”„๋กœ๊ทธ๋žจ ๊ตฌ์กฐ์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋„๋ก ์œ ์—ฐ์„ฑ์„ ์ œ๊ณตํ•œ๋‹ค.

 

๋นŒ๋” (Builder)

  • ๋ณต์žกํ•œ ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ์ •๊ณผ ํ‘œํ˜„๋ฐฉ๋ฒ•์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ๋‹จ๊ณ„๋ณ„๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ํŒจํ„ด์ด๋‹ค.
  • ๋งŽ์€ Optional ํ•œ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋‚˜ ์ง€์†์„ฑ ์—†๋Š” ์ƒํƒœ ๊ฐ’๋“ค์— ๋Œ€ํ•ด ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐํ•œ๋‹ค.
  • ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ์„ฑ์„ ์ตœ์†Œํ™”ํ•˜์—ฌ ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ์‹œ์  ์ถ”์ ์ด ์‰ฝ๋‹ค.
๐Ÿ’ก ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ์„ฑ ์ตœ์†Œํ™”
์ˆ˜์ •์ž ํŒจํ„ด์€ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ์„ฑ์„ ์—ด์–ด๋‘์–ด ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋Š” ์‹œ์ ์„ ํŒŒ์•…ํ•˜๊ธฐ ํž˜๋“  ๋ฐ˜๋ฉด, ๋นŒ๋” ํŒจํ„ด์€ ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ์‹œ์ ์ด ๊ฐ์ฒด์˜ ์ƒ์„ฑ ์‹œ์  ๋ฟ์ด๋ผ ์ถ”์ ์ด ์‰ฝ๊ณ  ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ๋†’๋‹ค.

 

public class User {
    private final String name;
    private final int age;

    public static class Builder {
        // Required parameters
        private final String name;

        // Optional parameters
        private int age = 0;	// ์„ ํƒ์  ์ธ์ž๋Š” ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”

        public Builder(String name) {
            this.name = name;
        }

        public Builder age(int val) {
            age = val;
            return this;	// . ์œผ๋กœ ์ฒด์ธ์„ ์ด์–ด๋‚˜๊ฐ€๊ธฐ ์œ„ํ•œ ๋ฐ˜ํ™˜
        }
        
        public User build() {
            return new User(this);
        }
    }

    private User(Builder builder) {
        name = builder.name;
        age = builder.age;
    }
}

<<Main Class>>
User user = new User
	.Builder("Anna")    // ํ•„์ˆ˜ ๊ฐ’
    .age(15)			// ์„ ํƒ ๊ฐ’
    .build();           // build() ๊ฐ€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜

 

๐Ÿ’ก ๋นŒ๋” ํŒจํ„ด ๊ตฌํ˜„ ๋ฐฉ๋ฒ•
- ๋นŒ๋” ํด๋ž˜์Šค๋ฅผ Static Nested Class๋กœ ์ƒ์„ฑํ•œ๋‹ค.(๊ด€๋ก€: ํด๋ž˜์Šค์ด๋ฆ„ + Builder)
- ๋นŒ๋” ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋Š” public ์ ‘๊ทผ ์ง€์ •์ž๋ฅผ ๊ฐ€์ง€๊ณ  ํ•„์ˆ˜ ๊ฐ’๋“ค์€ ์ƒ์„ฑ์ž๋กœ ์ œ๊ณตํ•œ๋‹ค.
- ์„ ํƒ ๊ฐ’๋“ค์€ ๊ฐ๊ฐ์˜ ์†์„ฑ๋งˆ๋‹ค ๋ฉ”์†Œ๋“œ๋กœ ์ œ๊ณตํ•˜๋˜ ๋ฉ”์†Œ๋“œ์˜ ๋ฆฌํ„ด ๊ฐ’์€ ๋นŒ๋” ๊ฐ์ฒด ์ž์‹ ์ด์–ด์•ผ ์ฒด์ธ์œผ๋กœ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.
- ๋นŒ๋” ํด๋ž˜์Šค ๋‚ด์— build() ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•˜์—ฌ ์ตœ์ข…์ ์œผ๋กœ ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
- ๋กฌ๋ณต์˜ @Builder ์–ด๋…ธํ…Œ์ด์…˜์ด ์œ„์™€ ๋™์ผํ•œ ์—ญํ• ์„ ํ•œ๋‹ค.

 

์ถ”์ƒํŒฉํ† ๋ฆฌ(Abstract Factory)

  • ๊ตฌ์ฒด์ ์ธ ํด๋ž˜์Šค์— ์˜์กดํ•˜์ง€ ์•Š๊ณ  ์„œ๋กœ ์—ฐ๊ด€๋˜๊ฑฐ๋‚˜ ์˜์กด์ ์ธ ๊ฐ์ฒด๋“ค์˜ ์กฐํ•ฉ์„ ๋งŒ๋“œ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ํŒจํ„ด์ด๋‹ค.
  • ๊ด€๋ จ์„ฑ ์žˆ๋Š” ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ๊ฐ์ฒด๋ฅผ ์ผ๊ด€์„ฑ ์žˆ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ƒ์„ฑํ•˜๊ณ  ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ผ๋ฆฌ ์‰ฝ๊ฒŒ ๊ต์ฒด๋  ์ˆ˜ ์žˆ๋„๋ก ๊ณ ์•ˆ๋˜์—ˆ๋‹ค.
public interface ComputerFactsFactory {
    Monitor createMonitor();
}

public class WhiteComputerFactsFactory implements ComputerFactsFactory {
    @Override
    public Monitor createMonitor() {
        return new WhiteMonitor();
    }
}

<<MainClass>>
private final ComputerFactsFactory computerFactory;
// ์ถ”์ƒํ™” ๋˜์–ด์žˆ๊ณ  ๊ตฌ์ฒด์ ์ธ ํด๋ž˜์Šค๊ฐ€ ์•„๋‹Œ ์ธํ„ฐํŽ˜์ด์Šค์— ์˜์กดํ•˜์—ฌ ๊ฒฐํ•ฉ๋ ฅ์ด ์•ฝํ•จ
computerFactory.crateMonitor();

 

 

ํ”„๋กœํ† ํƒ€์ž…(Prototype)

  • ์›ํ˜•์ด ๋˜๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•ด ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•  ๊ฐ์ฒด์˜ ์ข…๋ฅ˜๋ฅผ ๋ช…์‹œํ•˜์—ฌ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒˆ์„ฑ๋  ์‹œ์ ์— ์ธ์Šคํ„ด์Šค ํƒ€์ž…์ด ๊ฒฐ์ •๋˜๋Š” ํŒจํ„ด์ด๋‹ค.
  • ์ธ์Šคํ„ด์Šค๋Š” ์ƒˆ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์ž์‹ ์„ ๋ณต์ œํ•˜๊ณ  ์›๋ณธ ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ์šด ๊ฐ์ฒด์— ๋ณต์‚ฌํ•˜์—ฌ ํ•„์š”์— ๋”ฐ๋ผ ์ˆ˜์ •ํ•˜๋Š” ๋งค์ปค๋‹ˆ์ฆ˜์„ ์ œ๊ณตํ•œ๋‹ค.
  • ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ ๋น„์šฉ์ด ๋“ค๊ณ  ์ด๋ฏธ ์œ ์‚ฌํ•œ ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.

 

ํŒฉํ† ๋ฆฌ๋ฉ”์†Œ๋“œ(Factory Method)

  • ๊ฐ์ฒด ์ƒ์„ฑ์˜ ์—ญํ• ์„ ํ•˜๋Š” ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํŒจํ„ด์ด๋‹ค.
  • ์ƒ์„ฑ์ž์™€ ๋ณ„๋„๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ •์ ์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ๊ฐ์ฒด ์ƒ์„ฑ์„ ์บก์Šํ™”ํ•˜์—ฌ ์ œ๊ณตํ•œ๋‹ค.
  • ๊ฐ์ฒด ์ƒ์„ฑ์„ ์„œ๋ธŒ ํด๋ž˜์Šค์—๊ฒŒ ์œ„์ž„ํ•˜๊ณ  ํด๋ž˜์Šค ์ƒ์„ฑ๊ณผ ๋น„์ง€๋‹ˆ์Šค ์ฒ˜๋ฆฌ ๋กœ์ง์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ˜๋‹ค.
  • ๊ฐ€์ƒ ์ƒ์„ฑ์ž ํŒจํ„ด์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค.
๐Ÿ’ก ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ๋„ค์ด๋ฐ ์ปจ๋ฒค์…˜
- from: ํ•˜๋‚˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•„์„œ ๊ฐ์ฒด ์ƒ์„ฑ
- of: ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•„์„œ ๊ฐ์ฒด ์ƒ์„ฑ
- getInstance | instance: ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ(์ด์ „๊ณผ ๊ฐ™์„ ์ˆ˜ ์žˆ์Œ)
- newInstance | create: ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
- get[OrderType]: ๋‹ค๋ฅธ ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ(์ด์ „๊ณผ ๊ฐ™์„ ์ˆ˜ ์žˆ์Œ)
- new[OrderType]: ๋‹ค๋ฅธ ํƒ€์ž…์˜ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ

 

์‹ฑ๊ธ€ํ†ค(Singleton)

  • ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ ์˜ค์ง ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋งŒ ์ƒ์„ฑ๋จ์„ ๋ณด์žฅํ•˜๋Š” ํŒจํ„ด์ด๋‹ค.
  • ํ”„๋กœ๊ทธ๋žจ ์–ด๋””์„œ๋“  ์ธ์Šคํ„ด์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ปค๋„ฅ์…˜ ํ’€, ์Šค๋ ˆ๋“œ ํ’€, ๋””๋ฐ”์ด์Šค ์„ค์ • ๊ฐ์ฒด ๋“ฑ์€ ์ธ์Šคํ„ด์Šค๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค๋ฉด ์ž์›์ด ๋‚ญ๋น„๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ํŒจํ„ด์„ ํ™œ์šฉํ•œ๋‹ค.
public class Singleton {
	// ๋‹จ์ผ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•  ์ฐธ์กฐ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
    private static Singleton singleton = null;
    
    // new ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์—†๋„๋ก ์ƒ์„ฑ์ž์— private ์ ‘๊ทผ ์ œ์–ด์ž๋ฅผ ์ง€์ •ํ•œ๋‹ค.
    private Singleton() {
    }
    
    // ์œ ์ผํ•œ ๋‹จ์ผ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ์ •์  ๋ฉ”์„œ๋“œ๋ฅผ ์ง€์›ํ•ด์•ผ ํ•œ๋‹ค.
    public static Singleton getInstance() {
    	if (singleton == null) {
        	singleton = new Singleton();
        }
        
        return singleton;
    }
}

 

์ถœ์ฒ˜

https://velog.io/@ha0kim/Design-Pattern-%EC%83%9D%EC%84%B1-%ED%8C%A8%ED%84%B4Creational-Patterns

https://it-techtree.tistory.com/m/entry/design-pattern-creation-pattern

๋Œ“๊ธ€