Add all 23 GoF design pattern implementations
This commit is contained in:
24
01-creational/factory-method/NotificationService.java
Normal file
24
01-creational/factory-method/NotificationService.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package factorymethod;
|
||||
|
||||
public class NotificationService {
|
||||
|
||||
public void send(String type, String recipient, String message) {
|
||||
|
||||
// Creation logic mixed with business logic
|
||||
Notification notification;
|
||||
if ("EMAIL".equals(type)) {
|
||||
notification = new EmailNotification();
|
||||
} else if ("SMS".equals(type)) {
|
||||
notification = new SmsNotification();
|
||||
} else if ("PUSH".equals(type)) {
|
||||
notification = new PushNotification();
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unknown type: " + type);
|
||||
}
|
||||
|
||||
// Business logic that should never change
|
||||
System.out.println("Sending " + type + " to " + recipient);
|
||||
notification.send(recipient, message);
|
||||
System.out.println("Delivered.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user