refactoring : alternative classes with different interfaces

This commit is contained in:
haerong22
2022-04-13 00:05:50 +09:00
parent 9187ff9e2e
commit dd7f5c0917
21 changed files with 282 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces;
public class AlertMessage {
public void setMessage(String message) {
}
public void setFor(String email) {
}
}

View File

@@ -0,0 +1,14 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces;
public class AlertNotificationService implements NotificationService {
private AlertService alertService;
@Override
public void sendNotification(Notification notification) {
AlertMessage alertMessage = new AlertMessage();
alertMessage.setMessage(notification.getTitle());
alertMessage.setFor(notification.getReceiver());
alertService.add(alertMessage);
}
}

View File

@@ -0,0 +1,5 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces;
public interface AlertService {
void add(AlertMessage alertMessage);
}

View File

@@ -0,0 +1,15 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces;
public class EmailMessage {
public void setTitle(String title) {
}
public void setTo(String to) {
}
public void setFrom(String from) {
}
}

View File

@@ -0,0 +1,14 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces;
public class EmailNotificationService implements NotificationService {
private EmailService emailService;
@Override
public void sendNotification(Notification notification) {
EmailMessage emailMessage = new EmailMessage();
emailMessage.setTitle(notification.getTitle());
emailMessage.setTo(notification.getReceiver());
emailMessage.setFrom(notification.getSender());
emailService.sendEmail(emailMessage);
}
}

View File

@@ -0,0 +1,5 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces;
public interface EmailService {
void sendEmail(EmailMessage emailMessage);
}

View File

@@ -0,0 +1,38 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces;
public class Notification {
private String title;
private String receiver;
private String sender;
private Notification(String title) {
this.title = title;
}
public static Notification newNotification(String title) {
return new Notification(title);
}
public Notification receiver(String receiver) {
this.receiver = receiver;
return this;
}
public Notification sender(String sender) {
this.sender = sender;
return this;
}
public String getTitle() {
return title;
}
public String getReceiver() {
return receiver;
}
public String getSender() {
return sender;
}
}

View File

@@ -0,0 +1,6 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces;
public interface NotificationService {
void sendNotification(Notification notification);
}

View File

@@ -0,0 +1,7 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces;
public class Order {
public String getEmail() {
return null;
}
}

View File

@@ -0,0 +1,16 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces;
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.sendNotification(notification);
}
}

View File

@@ -0,0 +1,18 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces;
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@email.com");
notificationService.sendNotification(notification);
}
}

View File

@@ -0,0 +1,11 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces;
public class Shipping {
public String getOrder() {
return "Order 11231";
}
public String getEmail() {
return "aaa@email.com";
}
}

View File

@@ -0,0 +1,12 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces._before;
public class AlertMessage {
public void setMessage(String message) {
}
public void setFor(String email) {
}
}

View File

@@ -0,0 +1,5 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces._before;
public interface AlertService {
void add(AlertMessage alertMessage);
}

View File

@@ -0,0 +1,15 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces._before;
public class EmailMessage {
public void setTitle(String title) {
}
public void setTo(String to) {
}
public void setFrom(String from) {
}
}

View File

@@ -0,0 +1,5 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces._before;
public interface EmailService {
void sendEmail(EmailMessage emailMessage);
}

View File

@@ -0,0 +1,38 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces._before;
public class Notification {
private String title;
private String receiver;
private String sender;
private Notification(String title) {
this.title = title;
}
public static Notification newNotification(String title) {
return new Notification(title);
}
public Notification receiver(String receiver) {
this.receiver = receiver;
return this;
}
public Notification sender(String sender) {
this.sender = sender;
return this;
}
public String getTitle() {
return title;
}
public String getReceiver() {
return receiver;
}
public String getSender() {
return sender;
}
}

View File

@@ -0,0 +1,7 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces._before;
public class Order {
public String getEmail() {
return null;
}
}

View File

@@ -0,0 +1,13 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces._before;
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);
}
}

View File

@@ -0,0 +1,15 @@
package com.example.refactoring._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@email.com");
emailService.sendEmail(emailMessage);
}
}

View File

@@ -0,0 +1,11 @@
package com.example.refactoring._21_alternative_classes_with_different_interfaces._before;
public class Shipping {
public String getOrder() {
return "Order 11231";
}
public String getEmail() {
return "aaa@email.com";
}
}