๋กœ์ผ“๐Ÿพ
article thumbnail

 

์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ๋Š” OCP์™€ DIP๊ฐ€ ์œ„๋ฐฐ ๋˜๋Š” ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ์Šคํ”„๋ง์ด ์•„๋‹Œ ์ˆœ์ˆ˜ ์ž๋ฐ”๋กœ ํ•ด๊ฒฐํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์ž ๊น ์ง€๋‚œ ๊ธ€์˜ ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

public class DriverImpl implements Driver{

    Car car = new K3();
    Engine engine = new Electronic();

    @Override
    public void findOwnCarDetail() {
        car.findCarName();
        car.findCarBrand();
        engine.WayToMove();
    }
}

DriverImpl ์—ญํ• ์— ๊ตฌํ˜„์ฒด๊ฐ€ ์ง์ ‘ new ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธ ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿด ๊ฒฝ์šฐ ์–ผํ•๋ณด๋ฉด OCP์™€ DIP๋ฅผ ์ž˜ ์ง€ํ‚จ ๊ฒƒ ๊ฐ™์ง€๋งŒ ์‚ฌ์‹ค ์•„๋‹ˆ๋ผ๊ณ  ์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ ๋ง์”€๋“œ๋ ธ์Šต๋‹ˆ๋‹ค.

 

์ด๋ฒˆ์—๋„ ๊ฐ“์˜ํ•œ๋‹˜์˜ ๋ง์„ ๋นŒ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. 

"๋กœ๋ฏธ์˜ค์™€ ์ค„๋ฆฌ์—ฃ ๊ณต์—ฐ์„ ํ•˜๋ฉด ๋กœ๋ฏธ์˜ค ์—ญํ• ์„ ๋ˆ„๊ฐ€ ํ• ์ง€ ์ค„๋ฆฌ์—ฃ ์—ญํ• ์„ ๋ˆ„๊ฐ€ ํ• ์ง€๋Š” ๋ฐฐ์šฐ๋“ค์ด ์ •ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋‹ค. ์ด์ „ ์ฝ”๋“œ๋Š” ๋งˆ์น˜ ๋กœ๋ฏธ์˜ค ์—ญํ• (์ธํ„ฐํŽ˜์ด์Šค)์„ ํ•˜๋Š” ๋ ˆ์˜ค๋‚˜๋ฅด๋„ ๋””์นดํ”„๋ฆฌ์˜ค(๊ตฌํ˜„์ฒด, ๋ฐฐ์šฐ)๊ฐ€ ์ค„๋ฆฌ์—ฃ ์—ญํ• (์ธํ„ฐํŽ˜์ด์Šค)์„ ํ•˜๋Š” ์—ฌ์ž ์ฃผ์ธ๊ณต(๊ตฌํ˜„์ฒด, ๋ฐฐ์šฐ)์„ ์ง์ ‘ ์ดˆ๋น™ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ๋””์นดํ”„๋ฆฌ์˜ค๋Š” ๊ณต์—ฐ๋„ ํ•ด์•ผํ•˜๊ณ  ๋™์‹œ์— ์—ฌ์ž ์ฃผ์ธ๊ณต๋„ ๊ณต์—ฐ์— ์ง์ ‘ ์ดˆ๋น™ํ•ด์•ผ ํ•˜๋Š” ๋‹ค์–‘ํ•œ ์ฑ…์ž„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค."

 

์ •๋ง ๊ทธ ์–ด๋–ค ์˜ˆ์‹œ๋ณด๋‹ค ์ตœ๊ณ ์˜ ์˜ˆ์‹œ์ธ๊ฒƒ ๊ฐ™๋„ค์š”. ๋ฐฐ์šฐ๋Š” ๋ฐฐ์šฐ์—๋งŒ ๋ชฐ๋‘ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๊ณต์—ฐ ์žฅ์†Œ๋ฅผ ์ •ํ•˜๊ณ , ์—ญํ• ์—๋Š” ๋ˆ„๊ตด ์บ์ŠคํŒ… ํ• ์ง€์— ๋Œ€ํ•ด์„œ๋Š” ๋ฐฐ์šฐ๊ฐ€ ์•„๋‹Œ [๊ณต์—ฐ ๊ธฐํš์ž]๊ฐ€ ๊ฒฐ์ •ํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฐ ์—ญํ• ์„ ํ•ด์ค„ [๊ณต์—ฐ ๊ธฐํš์ž]๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์ž๋ฐ”์—์„œ๋Š” [๊ณต์—ฐ ๊ธฐํš์ž]๋ฅผ AppConfig๋ผ๋Š” ํด๋ž˜์Šค๊ฐ€ ๋‹ด๋‹นํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

public class AppConfig {

    public Driver driver(){
        return new DriverImpl(car(), engine());
    }

    public Car car(){
        return new K3();
    }

    public Engine engine(){
        return new Electronic();
    }

}

 

์œ„ AppConfig๋Š” ์ˆœ์ˆ˜ ์ž๋ฐ”๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์Šคํ”„๋ง์—์„œ ์ œ๊ณต๋˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์Šคํ”„๋ง์œผ๋กœ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ ๊ธ€์—์„œ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

AppConfig์—์„œ Car ์—ญํ• (์ธํ„ฐํŽ˜์ด์Šค)๋กœ๋Š” ์–ด๋–ค ๊ตฌํ˜„์ฒด๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€, Engine ์—ญํ• (์ธํ„ฐํŽ˜์ด์Šค)๋กœ๋Š” ์–ด๋–ค ๊ตฌํ˜„์ฒด๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€ ์ฃผ์ž…ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Driver ์—ญํ• (์ธํ„ฐํŽ˜์ด์Šค)์„ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์™ธ๋ถ€์—์„œ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

public class DriverImpl implements Driver{

    private final Car car;//์—ญํ• ๋งŒ ์•Œ๋ ค์ฃผ๊ณ  ๊ตฌํ˜„์ฒด๋Š” ๋ฌด์—‡์ธ์ง€ ์•Œ๋ ค์ค„ ํ•„์š”๊ฐ€ ์—†๋‹ค.
    private final Engine engine;

    public DriverImpl(Car car, Engine engine) {
        this.car = car;//์—ญํ• ์„ ์ •ํ•ด์ค€๋‹ค.
        this.engine = engine;//์—ญํ• ์„ ์ •ํ•ด์ค€๋‹ค.
    }

    @Override
    public void findOwnCarDetail() {
        car.findCarName();
        car.findCarBrand();
        engine.WayToMove();
    }
}

 

Driver ๊ตฌํ˜„์ฒด๋ฅผ ์œ„์™€ ๊ฐ™์ด ๋ฐ”๊ฟ”์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ „ ์ฝ”๋“œ์—์„œ๋Š” new K3(), new Fuel() ์ด๋Ÿฐ์‹์œผ๋กœ ์ง์ ‘ ์—ญํ• ์„ ์ง€์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์œ„ ์ฝ”๋“œ๋Š” ์ž๋ฐ”๊ฐ€ ์ด๋ฅผ ์•Œ์•„์„œ ์ฃผ์ž…์„ ์‹œ์ผœ์ค๋‹ˆ๋‹ค.

 

์ด์ „์—์„œ๋Š” ๊ตฌํ˜„์ฒด์— ์ง์ ‘ K3, Fuel ์ง€์ •ํ•ด์ฃผ์—ˆ๋‹ค. ํ•œ๋งˆ๋””๋กœ ๊ตฌํ˜„ ๊ฐ์ฒด๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์˜ ์ œ์–ด ํ๋ฆ„์„ ์Šค์Šค๋กœ๋ฅผ ์กฐ์ข…ํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ AppConfig๋ฅผ ์ƒ์„ฑํ•œ ๋’ค๋กœ๋Š” ๊ตฌํ˜„ ๊ฐ์ฒด๋Š” ์—ญํ• (์ธํ„ฐํŽ˜์ด์Šค)๋“ค๋งŒ ํ˜ธ์ถœํ•˜์ง€, ๊ตฌํ˜„์ฒด์— ๋Œ€ํ•ด์„œ๋Š” ํ˜ธ์ถœํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Driver ๊ตฌํ˜„ ๊ฐ์ฒด๋Š” Car, Engine์ด๋ผ๋Š” ์—ญํ• ์— ์–ด๋–ค ๊ตฌํ˜„์ฒด๊ฐ€ ์ฃผ์ž…๋  ๊ฒƒ์ธ์ง€๋Š” ์ „ํ˜€ ์•Œ ์ˆ˜ ์—†์ฃ .

 

์ด๋ ‡๋“ฏ ํ”„๋กœ๊ทธ๋žจ์— ๋Œ€ํ•œ ์ œ์–ด ํ๋ฆ„์„ ์™ธ๋ถ€๊ฐ€ ์•„๋‹Œ AppConfig๊ฐ€ ๊ฐ€์ง€๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋ฅผ [์ œ์–ด์˜ ์—ญ์ „(IoC, Inversion of Control)]์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ AppConfig๊ฐ€ ๊ตฌํ˜„ ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…์‹œ์ผœ์ฃผ๋Š” ๊ฒƒ์„ [์˜์กด๊ด€๊ณ„ ์ฃผ์ž…(DI, Dependency Injection)]๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  AppConfig์ฒ˜๋Ÿผ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋ฉด์„œ ์˜์กด๊ด€๊ณ„๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ๊ฒƒ์„ DI ์ปจํ…Œ์ด๋„ˆ(IoC ์ปจํ…Œ์ด๋„ˆ)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

public class DrivingApp {
    public static void main(String[] args) {
        AppConfig appConfig = new AppConfig();

        Driver driver = appConfig.driver();
        driver.findOwnCarDetail();
    }
}

์ด๋Ÿฐ ์‹์œผ๋กœ ์•Œ์•„์„œ ๊ฐ์ฒด๊ฐ€ ์•Œ์•„์„œ ์ฃผ์ž…๋˜๋ฏ€๋กœ ์™ธ๋ถ€์—์„  AppConfig๋ฅผ ๋ณด์ง€ ์•Š๋Š” ํ•œ ์–ด๋– ํ•œ ์—ญํ• ์— ์–ด๋–ค ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ง€ ์•Œ์ˆ˜ ์—†์ฃ . ์•Œ ํ•„์š”๋„ ์—†์ฃ . ์šฐ๋ฆฌ๊ฐ€ ์šด์ „ํ•˜๋Š”๋ฐ ์žˆ์–ด์„œ ์šด์ „ํ•˜๋Š” ๋ฐฉ๋ฒ•๋งŒ ์•Œ๋ฉด ๋˜์ง€ ์—”์ง„์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๊ณ , ๋ฐ”ํ€ด๋Š” ์–ด๋–ป๊ฒŒ ๊ตด๋Ÿฌ๊ฐ€๋Š” ์ง€ ์•Œ ํ•„์š”๋Š” ์—†์ž–์•„์š”? ํ•˜๋ฌผ๋ฉฐ ์ž๋™์ฐจ๋งˆ๋‹ค ์—”์ง„์˜ ์ข…๋ฅ˜๊ฐ€ ๋‹ค๋ฅธ๋ฐ ๊ทธ๊ฑฐ ๋ชฐ๋ผ๋„ ์šด์ „๋งŒ ์ž˜ ํ•˜์ž–์•„์š”. 

 

๋งŒ์•ฝ K3 ์ „๊ธฐ์ฐจ๊ฐ€ ์•„๋‹Œ K5 ์—ฐ๋ฃŒ์ฐจ๋กœ ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค๋ฉด AppConfig์˜ ์ฝ”๋“œ๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

public class AppConfig {

    public Driver driver(){
        return new DriverImpl(car(), engine());
    }

    public Car car(){
        return new K5();
    }

    public Engine engine(){
        return new Fuel();
    }

}

์ฝ”๋“œ์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด Driver ๊ตฌํ˜„์ฒด์— ๋” ์ด์ƒ ์ง์ ‘์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ตฌํ˜„์ฒด์— ์˜์กดํ•˜์ง€ ์•Š์ฃ . ๋˜ํ•œ ์ „๊ธฐ๋ฅผ ์—ฐ๋ฃŒ๋กœ ๋ฐ”๊ฟ€ ๋•Œ ๊ตฌํ˜„์ฒด์˜ ์ฝ”๋“œ๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์ˆ˜์ •ํ•  ํ•„์š”๋„ ์—†์Šต๋‹ˆ๋‹ค. AppConfig๋ผ๋Š” [๊ณต์—ฐ ๊ธฐํš์ž]๊ฐ€ ๋‹ค ํ•ด์ฃผ๋‹ˆ๊น์š”.

 

์ด๋ฒˆ ๊ธ€์—์„œ AppConfig๋ผ๋Š” DI ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ด์šฉํ•ด์„œ OCP์™€ DIP๋ฅผ ํ•ด๊ฒฐํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ ๋‹น์žฅ์€ ์ฝ”๋“œ ์ˆ˜๊ฐ€ ์ ๊ธฐ ๋•Œ๋ฌธ์— AppConfig๋ฅผ ๋งŒ๋“ค์–ด์„œ ์—ฌ๊ธฐ๋‹ค๊ฐ€ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ด ์–ด๋ ต์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ฝ”๋“œ๋Ÿ‰์ด ๋งŽ์•„์งˆ ๊ฒฝ์šฐ AppConfig๋กœ๋งŒ ๊ด€๋ฆฌํ•˜๊ธฐ ํž˜๋“ค์–ด์ง‘๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ ๋‹ค์Œ ๊ธ€์—์„œ๋Š” ์Šคํ”„๋ง์„ ์‚ฌ์šฉํ•˜์—ฌ OCP์™€ DIP๋ฅผ ํ•ด๊ฒฐํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์–ด๋Š ์ •๋„ ์™œ ์Šคํ”„๋ง์„ ์‚ฌ์šฉํ•˜๋Š” ์ง€ ์ง์ž‘์ด ๊ฐ€์‹œ์ฃ ?

 

profile on loading

Loading...