tonykang22 / study

0 stars 0 forks source link

[Refactoring] 냄새 21. 서로 다른 인터페이스의 대안 클래스들 #46

Open tonykang22 opened 2 years ago

tonykang22 commented 2 years ago

냄새 21. 서로 다른 인터페이스의 대안 클래스들 (Alternative Classes with Different Interfaces)


예시 코드

Before

public class OrderProcessor {

    private EmailService emailService;

    public void notifyShipping(Shipping shipping) {
        EmailMessage emailMessage = new EmailMessage();
        emailMessage.setTitle(shipping.getOrder() + " is shipped");
        emailMessage.setTo(shipping.getEmail());
        emailMessage.setFrom("no-reply@whiteship.com");
        emailService.sendEmail(emailMessage);
    }

}
public class OrderAlerts {

    private AlertService alertService;

    public void alertShipped(Order order) {
        AlertMessage alertMessage = new AlertMessage();
        alertMessage.setMessage(order.toString() + " is shipped");
        alertMessage.setFor(order.getEmail());
        alertService.add(alertMessage);
    }
}
public interface EmailService {
    void sendEmail(EmailMessage emailMessage);
}
public interface AlertService {
    void add(AlertMessage alertMessage);
}


After

어떤 notificationService를 넘겨주는가에 따라 행동이 다르게끔 notificationService 인터페이스를 생성한다. (동일한 인터페이스로 감싸는 방법을 사용해 해결할 수 있다.) (책에는 인터페이스 코드 자체를 수정하는 예시 코드가 있으나 위 예시 코드는 그렇지 못한 경우를 상정)

public interface NotificationService {
    public void senNotification(Notification notification);
}
public class OrderProcessor {

    private NotificationService notificationService;

    public OrderProcessor(NotificationService notificationService) {
        this.notificationService = notificationService;
    }

    public void notifyShipping(Shipping shipping) {
        Notification notification = Notification.newNotification(shipping.getOrder() + " is shipped")
                .receiver(shipping.getEmail())
                .sender("no-reply@gmail.com");
        notificationService.senNotification(notification);
    }
}
public class EmailNotificationService implements NotificationService {

    private EmailService emailService;

    @Override
    public void senNotification(Notification notification) {
        EmailMessage emailMessage = new EmailMessage();
        emailMessage.setTitle(notification.getTitle() + " is shipped");
        emailMessage.setTo(notification.getReceiver());
        emailMessage.setFrom(notification.getSender());
        emailService.sendEmail(emailMessage);
    }
}
public class OrderAlerts {

    private NotificationService notificationService;

    public OrderAlerts(NotificationService notificationService) {
        this.notificationService = notificationService;
    }

    public void alertShipped(Order order) {
        Notification notification = Notification.newNotification(order.toString() + " is shipped")
                .receiver(order.getEmail());
        notificationService.senNotification(notification);

    }
}
public class AlertNotificationService implements NotificationService {

    private AlertService alertService;

    @Override
    public void senNotification(Notification notification) {
        AlertMessage alertMessage = new AlertMessage();
        alertMessage.setMessage(notification.getTitle());
        alertMessage.setFor(notification.getReceiver());
        alertService.add(alertMessage);
    }
}