๐Ÿ”ฅ TIL - Day 80 ๋””์ž์ธ ํŒจํ„ด - ํ”„๋ก์‹œ ํŒจํ„ด

Kim Dae Hyunยท2022๋…„ 1์›” 8์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
91/93

๋””์ž์ธ ํŒจํ„ด์„ ๊ณต๋ถ€ํ•  ๋•Œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ํ•ด๋‹น ๋””์ž์ธ ํŒจํ„ด์ด ์–ด๋–ค ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋˜์–ด์•ผ ํ•˜๋Š” ์ง€๋ฅผ ์•Œ์•„์•ผ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋กœ ๋””์ž์ธ ํŒจํ„ด์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋”๋ผ๋„ ๋””์ž์ธ ํŒจํ„ด์˜ ์˜๋„๋ฅผ ์ œ๋Œ€๋กœ ํŒŒ์•…ํ•˜์ง€ ๋ชปํ•˜๊ณ  ์‹ค์ œ ์ ํ•ฉํ•œ ์‚ฌ์šฉ์ฒ˜๋ฅผ ์ฐพ์ง€ ๋ชปํ•œ๋‹ค๋ฉด ์˜๋ฏธ๊ฐ€ ์—†๋‹ค. ์‚ฌ์šฉ์ฒ˜๋ฅผ ์ œ๋Œ€๋กœ ์•Œ๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด๋ณด์ž.


๐Ÿ“Œ ํ”„๋ก์‹œ ํŒจํ„ด ๊ตฌํ˜„

ํ”„๋ก์‹œ ํŒจํ„ด์€ ์‹ค์ œ๊ฐ์ฒด๋กœ์˜ ์ ‘๊ทผ์„ ํ†ต์ œํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
์ ‘๊ทผ์„ ํ†ต์ œํ•œ๋‹ค๋Š” ๊ฒƒ์ด ๋ณด์•ˆ์ ์ธ ๊ฒƒ๋งŒ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. (๋‹น์—ฐํžˆ ๋‹จ์ˆœํ•œ ์ ‘๊ทผํ†ต์ œ์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.) ๋ฐ์ดํ„ฐ๋ฅผ ํ•ด์‹ฑํ•˜๋Š” ๋“ฑ์˜ ์„ฑ๋Šฅํ–ฅ์ƒ ๋ชฉ์ ์œผ๋กœ๋„ ์‹ค์ œ๊ฐ์ฒด๋กœ์˜ ์ ‘๊ทผํ†ต์ œ๊ฐ€ ์ด๋ฃจ์–ด์ง„๋‹ค.

์‹ค์ œ๊ฐ์ฒด์— ์•ž์„œ ๋ฐ˜๋“œ์‹œ ํ”„๋ก์‹œ ๊ฐ์ฒด๊ฐ€ ๋จผ์ € ํ˜ธ์ถœ๋˜๋„๋ก ํ•˜๊ณ  ๊ตฌํ˜„์— ๋”ฐ๋ผ ํ”„๋ก์‹œ ๊ฐ์ฒด์—์„œ ์‹ค์ œ๊ฐ์ฒด๋กœ ์š”์ฒญ์„ ์ „๋‹ฌํ•œ๋‹ค. ํ”„๋ก์‹œ ๊ฐ์ฒด์—์„œ ์‹ค์ œ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ˜๋“œ์‹œ ์‹ค์ œ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.

  • ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์€ ์‹ค์ œ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ํ”„๋ก์‹œ ๊ฐ์ฒด๋กœ ์š”์ฒญ๋˜์–ด์•ผ ํ•œ๋‹ค.
  • ํ”„๋ก์‹œ ๊ฐ์ฒด๋Š” ์‹ค์ œ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์‹ค์ œ๊ฐ์ฒด์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.

๊ฐ„๋‹จํ•˜๊ฒŒ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ํ”„๋ก์‹œ ํŒจํ„ด์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ฒฐ๊ณผ์™€ ์ ์šฉ๋œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณด์ž.

[ ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜ ]
public interface Subject {

    String operation();
}

์‹ค์ œ๊ฐ์ฒด๋Š” ํ˜ธ์ถœ๋์„ ๋•Œ ๋กœ๊ทธ๋ฅผ ์ฐ๊ณ  1์ดˆ๊ฐ„ ์ž‘์—…์„ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.

[ ์‹ค์ œ๊ฐ์ฒด์— ํ•ด๋‹นํ•˜๋Š” ๊ตฌํ˜„ ํด๋ž˜์Šค ์ •์˜ ]
@Slf4j
public class RealSubject implements Subject {

    @Override
    public String operation() {
        log.info("RealSubject ํ˜ธ์ถœ");
        sleep(1000);
        return "data";
    }

    private void sleep(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„์ฒด๋ฅผ ์ฃผ์ž…๋ฐ›์•„ ์‚ฌ์šฉํ•  Client ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•œ๋‹ค.

public class ProxyPatternClient {

    private final Subject subject;

    public ProxyPatternClient(Subject subject) {
        this.subject = subject;
    }

    public void execute() {
        subject.operation();
    }
}

ํ…Œ์ŠคํŠธ
execute๋ฅผ 5๋ฒˆ ํ˜ธ์ถœํ–ˆ๊ณ  execute๋Š” ์‹ค์ œ๊ฐ์ฒด์˜ operation์„ ํ˜ธ์ถœํ•˜๊ธฐ ๋•Œ๋ฌธ์— 5์ดˆ์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋  ๊ฒƒ์ด๋‹ค.

    @Test
    void noProxyTest() {
        RealSubject realSubject = new RealSubject();
        ProxyPatternClient client = new ProxyPatternClient(realSubject);

        client.execute();
        client.execute();
        client.execute();
        client.execute();
        client.execute();
    }

ํ”„๋ก์‹œ ํŒจํ„ด์ด ์ ์šฉ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ์‹ค์ œ๊ฐ์ฒด์˜ 1์ดˆ๊ฐ€ ๊ฑธ๋ฆฌ๋Š” ์ž‘์—…์„ ๋บด๋จน์ง€ ์•Š๊ณ  ๋ชจ๋‘ ์‹คํ–‰ํ–ˆ๋‹ค. ์ด์ œ ํ”„๋ก์‹œ ํŒจํ„ด์„ ์ ์šฉํ•ด์„œ ์‹ค์ œ๊ฐ์ฒด์˜ ์ž‘์—…๊ฒฐ๊ณผ๋ฅผ ์บ์‹ฑํ•ด์„œ ์‹ค์ œ๊ฐ์ฒด๋กœ์˜ ์ ‘๊ทผ์„ ํ†ต์ œํ•ด๋ณด์ž.

ํ”„๋ก์‹œ ์—ญํ• ์„ ํ•  ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•œ๋‹ค. ์‹ค์ œ๊ฐ์ฒด์™€ ๋™์ผํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ํ•„์š”์—ฌ๋ถ€์— ๋”ฐ๋ผ ์‹ค์ œ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด ์ฃผ์ž…๋ฐ›์•„์•ผ ํ•œ๋‹ค.
์ถ”๊ฐ€๋กœ ์บ์‹ฑ์„ ์œ„ํ•œ ํ•„๋“œ๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•œ๋‹ค(cacheData) ๋งŒ์•ฝ cacheData์— ๊ฐ’์ด ์—†๋‹ค๋ฉด (์บ์‹ฑ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋‹ค๋ฉด) ์‹ค์ œ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ์บ์‹ฑ๋œ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

@Slf4j
public class CacheProxy implements Subject {

    private final RealSubject realSubject;
    private String cacheData;

    public CacheProxy(RealSubject realSubject) {
        this.realSubject = realSubject;
    }

    @Override
    public String operation() {
        log.info("CacheProxy ํ˜ธ์ถœ");
        if (cacheData == null) {
            cacheData = realSubject.operation();
        }
        return cacheData;
    }
}

ํด๋ผ์ด์–ธํŠธ๋Š” Subject ์ธํ„ฐํŽ˜์ด์Šค์—๋งŒ ์˜์กดํ•˜๋ฏ€๋กœ ํ”„๋ก์‹œ ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•ด๋„ ์•„๋ฌด๋Ÿฐ ์ง€์žฅ์ด ์—†๋‹ค. ํ”„๋ก์‹œ ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•˜๊ณ  ๋™์ผํ•œ ํ…Œ์ŠคํŠธ๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.

ํ…Œ์ŠคํŠธ

    @Test
    void cacheProxyTest() {
        CacheProxy cacheProxy = new CacheProxy(new RealSubject());
        ProxyPatternClient client = new ProxyPatternClient(cacheProxy);

        client.execute();
        client.execute();
        client.execute();
        client.execute();
        client.execute();
    }


1์ดˆ๋Œ€ ์‹œ๊ฐ„์ด ์†Œ์š”๋๊ณ  ๋กœ๊ทธ๋„ ์‹ค์ œ๊ฐ์ฒด๋Š” ํ•œ ๋ฒˆ๋งŒ ํ˜ธ์ถœ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.



๐Ÿ“Œ ์ฐธ๊ณ 

์ธํ”„๋Ÿฐ - ์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ ๊ณ ๊ธ‰ํŽธ (๊น€์˜ํ•œ ๋‹˜)

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€