IoC(Inversion of Control, 제어의 역전)은 소프트웨어 설계 원칙 중 하나로, 객체의 생성 및 제어 권한을 개발자가 직접 다루는 것이 아니라 외부에서 처리하도록 하는 개념이다.
이를 통해 모듈 간의 결합도를 낮추고, 코드의 재사용성과 확장성을 높이는 것이 핵심 목표이다.
public interface PaymentService {
void processPayment();
}
public class CreditCardPaymentService implements PaymentService {
@Override
public void processPayment() {
System.out.println("Processing payment through Credit Card");
}
}
public class PaypalPaymentService implements PaymentService {
@Override
public void processPayment() {
System.out.println("Processing payment through Paypal");
}
}
public class OrderService {
private PaymentService paymentService;
public OrderService(PaymentService paymentService) {
this.paymentService = paymentService;
}
public void placeOrder() {
System.out.println("Placing order...");
paymentService.processPayment();
}
}
public class IoCContainer {
public static PaymentService getPaymentService(String type) {
if (type.equals("credit")) {
return new CreditCardPaymentService();
} else if (type.equals("paypal")) {
return new PaypalPaymentService();
}
throw new IllegalArgumentException("Unknown payment type");
}
public class Main {
public static void main(String[] args) {
PaymentService paymentService = IoCContainer.getPaymentService("credit");
OrderService orderService = new OrderService(paymentService);
orderService.placeOrder();
}
}
public interface PaymentService {
void processPayment();
}
public class CreditCardPaymentService implements PaymentService {
@Override
public void processPayment() {
System.out.println("Processing payment through Credit Card");
}
}
public class PaypalPaymentService implements PaymentService {
@Override
public void processPayment() {
System.out.println("Processing payment through Paypal");
}
}
import java.util.HashMap;
import java.util.Map;
public class SimpleIoCContainer {
private Map<Class<?>, Object> beanMap = new HashMap<();
// 객체 등록
public void registerBean(Class<?> clazz, Object bean) {
beanMap.put(clazz, bean);
// 객체 가져오기
public <T> T getBean<Class<T> clazz) {
return clazz.cast(beanMap.get(clazz));
}
// 객체 생성 및 등록 (생성자 주입 방식)
public void registerBeanWithDependencies(Class<?> clazz, Class<?> dependencyClass) {
try {
Object dependency = getBean(dependencyClass);
Object instance = clazz.getConstructor(dependencyClass).newInstance(dependency);
registerBean(clazz, instance);
} cath (Exception e) {
throw new RuntimeException("Failed to create bean with dependencies", e);
}
}
}
public class OrderService {
private PaymentService paymentService;
public OrderService(PaymentService paymentService) {
this.paymentService = paymentService;
}
public void placeOrder() {
System.out.println("Placing order...");
paymentService.processPayment();
}
}
public class Main {
public static void main(String[] args) {
SimpleIoCContainer container = new SimpleIoCConatiner();
container.registerBean(PaymentService.class, new CreditCardPaymentService());
container.registerBeanWithDependencies(OrderService.class, PaymentService.class);
OrderService orderService = container.getBean(OrderService.class);
orderService.placeOrder();
}
}
SimpleIoCContainer는 간단한 IoC 컨테이너 역할을 하며, 객체를 등록하고 의존성을 주입할 수 있다.registerBean() 메서드는 특정 클래스와 객체를 매핑하여 저장한다.registerBeanWithDependencies() 메서드는 의존성을 주입하여 객체를 생성하고 등록한다.