Arthur's Blog
์๋น์ค ๋ก์ผ์ดํฐ(SL)๋? ๋ณธ๋ฌธ
๐ ์ ์
์ ํ๋ฆฌ์ผ์ด์ ์ ํ์ํ ์ ์๋ ๋ชจ๋ ์๋น์ค๋ฅผ ์ป๋ ๋ฐฉ๋ฒ์ ์๊ณ ์๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ ๊ฒ์ด๋ค.
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 |