[TIL-DAY15] Java 문법 정리 [결합도]

김유란·2025년 1월 14일

1. 객체 간의 결합도

Loose Coupling (느슨한 결합)

  • 객체 지향 설계의 주요 목표 중 하나로 사용을 지향함
  • 클래스 간의 의존성을 최소화한 설계 방식
  • 인터페이스나 추상 클래스를 통해 서로 상호작용
  • 유연성과 재사용성이 높음
// Interface
public interface NotificationService {
    void sendNotification(String message);
}

// Email Notification
public class EmailNotificationService implements NotificationService {
    @Override
    public void sendNotification(String message) {
        System.out.println("Email sent: " + message);
    }
}

// SMS Notification
public class SMSNotificationService implements NotificationService {
    @Override
    public void sendNotification(String message) {
        System.out.println("SMS sent: " + message);
    }
}

// Client
public class NotificationManager {
    private NotificationService service;

    public NotificationManager(NotificationService service) {
        this.service = service;
    }

    public void notifyUser(String message) {
        service.sendNotification(message);
    }
}

// Main
public class Main {
    public static void main(String[] args) {
        NotificationService emailService = new EmailNotificationService();
        NotificationManager manager = new NotificationManager(emailService);
        manager.notifyUser("Hello via Email!");

        NotificationService smsService = new SMSNotificationService();
        manager = new NotificationManager(smsService);
        manager.notifyUser("Hello via SMS!");
    }
}
  • NotificationManagerNotificationService 인터페이스에만 의존하며, 구체적인 구현(Email, SMS)에 의존하지 않음

Tight Coupling (강한 결합)

  • 클래스가 서로의 구체적인 구현에 의존하는 설계 방식
  • 한 클래스가 변경되면 다른 클래스도 수정해야 할 가능성 높아짐
  • 의존성이 높고, 확장성이 저하됨
// Interface
public interface NotificationService {
    void sendNotification(String message);
}

// Email Notification
public class EmailNotificationService implements NotificationService {
    @Override
    public void sendNotification(String message) {
        System.out.println("Email sent: " + message);
    }
}

// SMS Notification
public class SMSNotificationService implements NotificationService {
    @Override
    public void sendNotification(String message) {
        System.out.println("SMS sent: " + message);
    }
}

// Client
public class NotificationManager {
    private NotificationService service;

    public NotificationManager(NotificationService service) {
        this.service = service;
    }

    public void notifyUser(String message) {
        service.sendNotification(message);
    }
}

// Main
public class Main {
    public static void main(String[] args) {
        NotificationService emailService = new EmailNotificationService();
        NotificationManager manager = new NotificationManager(emailService);
        manager.notifyUser("Hello via Email!");

        NotificationService smsService = new SMSNotificationService();
        manager = new NotificationManager(smsService);
        manager.notifyUser("Hello via SMS!");
    }
}
  • NotificationManagerEmailNotificationService의 구체적인 구현에 의존하며, 다른 타입의 알림(SMS 등)을 추가하려면 코드를 수정해야 함

0개의 댓글