Arthur's Blog

์„œ๋น„์Šค ๋กœ์ผ€์ดํ„ฐ(SL)๋ž€? ๋ณธ๋ฌธ

OOP

์„œ๋น„์Šค ๋กœ์ผ€์ดํ„ฐ(SL)๋ž€?

Leeseojune53 2023. 7. 1. 00:32

๐Ÿ“Œ ์ •์˜

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ํ•„์š”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์„œ๋น„์Šค๋ฅผ ์–ป๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ณ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๊ฐ–๋Š” ๊ฒƒ์ด๋‹ค.

IoC๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” DI ์™ธ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜.

๐Ÿ”— ์ข…์†์„ฑ ๋‹ค์ด์–ด๊ทธ๋žจ

์‹ค์ œ ์‚ฌ์šฉ๋ฐฉ๋ฒ•

class ServiceLocator{
    private static ServiceLocator sInstance;
    public static void load(ServiceLocator arg) {
        sInstance = arg;
    }
    private Map services = new HashMap();
    public static Object getService(String key){
        return sInstance.services.get(key);
    }
    public void loadService (String key, Object service) {
        services.put(key, service);
    }
}

class Loader {
   private void configure() {
        ServiceLocator locator = new ServiceLocator();
        locator.loadService("LocalBookFinder", new LocalBookFinder());
        locator.loadService("RemoteBookFinder", new RemoteBookFinder());      
        ServiceLocator.load(locator);
    }
}

class Library{
	BookFinder finder = (BookFinder) ServiceLocator
        .getService("LocalBookFinder");
}
 

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ServiceLocator์— Loader๊ฐ€ Service๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ , Library class์—์„œ ๊ทธ Service๋ฅผ injectionํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

Factory์™€ ์ฐจ์ด์ 

Factory๋Š” ์ด๋ฆ„๊ณผ ๊ฐ™์ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋Š” ๊ฒƒ์ด๊ณ , SL์€ ์„œ๋น„์Šค(๊ฐ์ฒด)๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.

๐Ÿ‘ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•๋“ค..

IoC๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์žˆ๋‹ค.

  • Events
  • Delegates

๋“ฑ ๋“ฑ

'OOP' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

DI(Dependency Injection)๋ž€?  (0) 2023.07.01
DI vs IoC vs DIP  (0) 2023.07.01
๋†’์€ ์‘์ง‘๋„์™€ ๋‚ฎ์€ ๊ฒฐํ•ฉ๋„  (0) 2023.06.28
๊ฐ์ฒด์ง€ํ–ฅ 5๋Œ€ ์›์น™  (0) 2023.06.26
๊ด€์‹ฌ์‚ฌ์˜ ๋ถ„๋ฆฌ  (0) 2023.06.26