๐Ÿง‘๐Ÿปโ€๐Ÿ’ป์‹ฑ๊ธ€ํ†ค ์ปจํ…Œ์ด๋„ˆ๐Ÿ“โ—๏ธโ—๏ธ

DEVJUNยท2021๋…„ 12์›” 21์ผ
0
post-thumbnail
post-custom-banner

์Šคํ”„๋ง์€ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ชฉ์ ์œผ๋กœ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์—ฌ๋Ÿฌ ๊ณ ๊ฐ์ด ๋™์‹œ์— ์š”์ฒญ์„ ํ•œ๋‹ค!!

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณ ๊ฐ์—๊ฒŒ ๋™์‹œ์— ์š”์ฒญ์ด ๋“ค์–ด์˜ฌ ๋•Œ ๊ฐ์ฒด๊ฐ€ ๊ณ„์† ์ƒ์„ฑ๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๊ฐ€ ๋งค์šฐ ์‹ฌํ• ์ˆ˜ ์žˆ๋‹ค.
๋˜ํ•œ ๊ฐ์ฒด์˜ ์ƒ์„ฑ๋„ ๋น„์šฉ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์—… ์ž…์žฅ์—์„œ๋Š” ์—„์ฒญ๋‚œ ๋น„์šฉ์ด ๋‚˜๊ฐˆ๊ฒƒ์ด๋‹ค.

์œ„์˜ ์ƒํ™ฉ์„ ์ฝ”๋“œ๋กœ ๋‚˜ํƒ€๋‚ด์—ˆ๋‹ค!

 @Test
    @DisplayName("์Šคํ”„๋ง ์—†๋Š” ์ˆœ์ˆ˜ํ•œ DI ์ปจํ…Œ์ด๋„ˆ")
    void pureContainer(){
        AppConfig appConfig = new AppConfig();

        //1. ์กฐํšŒ: ํ˜ธ์ถœํ•  ๋•Œ ๋งˆ๋‹ค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
        MemberService memberService1 = appConfig.memberService();

        //2. ์กฐํšŒ: ํ˜ธ์ถœํ•  ๋•Œ ๋งˆ๋‹ค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ (๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„) -> ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์„ ํ†ตํ•ด ํ•ด๊ฒฐ
        MemberService memberService2 = appConfig.memberService();
        System.out.println("memberService1 = " + memberService1);
        System.out.println("memberService2 = " + memberService2);

        //memberService 1 != memberService2
        assertThat(memberService1).isNotSameAs(memberService2);

    }

์„œ๋น„์Šค๋ฅผ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์„ ๋ณผ์ˆ˜ ์žˆ๋‹ค...๐Ÿ˜ฎโ€๐Ÿ’จ

์œ„์™€ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์ด๋‹ค. ๋””์ž์ธ ํŒจํ„ด์€ ๋‚˜์ค‘์— ๋”ฐ๋กœ ํฌ์ŠคํŒ…ํ•  ์˜ˆ์ •์ด๋‹ค.

์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์— ๋Œ€ํ•ด ๊ฐ„๋žตํžˆ ์ด์•ผ๊ธฐํ•˜์ž๋ฉด ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ 1๊ฐœ๋งŒ ์ƒ์„ฑํ•˜๋„๋ก ํ•˜๋Š” ๋””์ž์ธ ํŒจํ„ด์ด๋‹ค.

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค๋ฅผ ์ง์ ‘ ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ , ๋™์‹œ์— ์‹ฑ๊ธ€ํ†ค ํŒจํ„ด ์‚ฌ์šฉ์‹œ ๋ฐœ์ƒํ•˜๋Š” DIP(์˜์กด๊ด€๊ณ„ ์—ญ์ „ ์›์น™), OCP(๊ฐœ๋ฐฉ-ํ์‡„ ์›์น™) ์œ„๋ฐ˜์˜ ๋ฐœ์ƒ์„ ๋ง‰์•„์ค€๋‹ค.



๋‹ค์Œ์€ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‚ฌ์šฉํ•œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์ด๋‹ค!

@Test
    @DisplayName("์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์™€ ์‹ฑ๊ธ€ํ†ค")
    void springContainer(){
        //AppConfig appConfig = new AppConfig();

        //์Šคํ”„๋ง์€ ์‹ฑ๊ธ€ํ†ค ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.
        ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
        MemberService memberService1 = ac.getBean("memberService", MemberService.class);
        MemberService memberService2 = ac.getBean("memberService", MemberService.class);

        //์ฐธ์กฐ๊ฐ’ ํ™•์ธ
        System.out.println("memberService1 = " + memberService1);
        System.out.println("memberService2 = " + memberService2);

        assertThat(memberService1).isSameAs(memberService2);
    }

๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ๊ฒ€์ •์ƒ‰ ์‚ฌ๊ฐํ˜•์œผ๋กœ ํ‘œ์‹œํ•œ ๋ถ€๋ถ„์ด ์„œ๋น„์Šค๋ฅผ ์š”์ฒญํ–ˆ์„ ๋•Œ ์ฐธ์กฐ๊ฐ’์ด๋‹ค. ์ฐธ์กฐ๊ฐ’์ด ๊ฐ™์€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.


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


๐Ÿ˜ฑ ์‹ฑ๊ธ€ํ†ค ๋ฐฉ์‹์˜ ์ฃผ์˜์ โ—๏ธ

์‹ฑ๊ธ€ํ†ค ํŒจํ„ด์ด๋“ , ์Šคํ”„๋ง ๊ฐ™์€ ์‹ฑ๊ธ€ํ†ค ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‚ฌ์šฉํ•˜๋“ , ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค๋ฅผ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑํ•ด์„œ ๊ณต์œ ํ•˜๋Š” ์‹ฑ๊ธ€ํ†ค ๋ฐฉ์‹์€ ์—ฌ๋Ÿฌ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ํ•˜๋‚˜์˜ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค๋ฅผ ๊ณต์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด๋Š” ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๊ฒŒ ์„ค๊ณ„ํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

์ฆ‰โ—๏ธ ํŠน์ • ํด๋ผ์ด์–ธํŠธ์— ์˜์กด์ ์ธ ํ•„๋“œ๊ฐ€ ์žˆ์œผ๋ฉด ์•ˆ๋œ๋‹ค.

๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ๋ณด์ž!

package hello.core.singleton;

public class StatefulService {

    private int price; //์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๋Š” ํ•„๋“œ

    public void order(String name, int price){
       System.out.println("name = " + name + " price = " + price);
        this.price = price; //๋ฌธ์ œ!!
    }


    public int getPrice(){
        return price;
    }
}

package hello.core.singleton;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import static org.junit.jupiter.api.Assertions.*;

class StatefulServiceTest {
    
    @Test
    void statefulServiceSingleton(){
        ApplicationContext ac = new AnnotationConfigApplicationContext(TestConfig.class);
        StatefulService statefulService1 = ac.getBean("statefulService", StatefulService.class);
        StatefulService statefulService2 = ac.getBean("statefulService", StatefulService.class);
        
        statefulService1.order("userA", 10000);
        statefulService2.order("userB", 20000);
        
        //ThreadA: ์‚ฌ์šฉ์ž A ์ฃผ๋ฌธ ๊ธˆ์•ก ์กฐํšŒ ๊ธฐ๋Œ€์™€ ๋‹ฌ๋ฆฌ 20000์›์ด ๋‚˜์˜ด...
        int price = statefulService1.getPrice();
        System.out.println("price = " + price);

        Assertions.assertThat(statefulService1.getPrice()).isEqualTo(20000);
    }
    
    static class TestConfig{
        
        @Bean
        public StatefulService statefulService(){
            return new StatefulService();
        }
    }

}

์‚ฌ์šฉ์žA์˜ ์ฃผ๋ฌธ๊ธˆ์•ก์€ 10000์›์ธ๋ฐ price ํ•„๋“œ๋ฅผ ๊ณต์œ ํ•˜์—ฌ 20000์›์ด๋ผ๋Š” ์ด์ƒํ•œ ๊ฒฐ๊ณผ๊ฐ’์ด ๋‚˜์™”๋‹ค.

๐Ÿ“๋”ฐ๋ผ์„œ ์Šคํ”„๋ง ๋นˆ์€ ํ•ญ์ƒ ๋ฌด์ƒํƒœ(staeteless)๋กœ ์˜์กด์ ์ธ ํ•„๋“œ ์—†์ด ์„ค๊ณ„ํ•ด์•ผ ํ•œ๋‹คโ—๏ธโ—๏ธ

์ถœ์ฒ˜

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%ED%95%B5%EC%8B%AC-%EC%9B%90%EB%A6%AC-%EA%B8%B0%EB%B3%B8%ED%8E%B8/dashboard

profile
๐Ÿง‘๐Ÿปโ€๐Ÿ’ปiOS
post-custom-banner

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