Add creational patterns, Interpreter; remove scripts; update README
This commit is contained in:
11
01-creational/factory-method/EmailNotification.java
Normal file
11
01-creational/factory-method/EmailNotification.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package factorymethod;
|
||||
|
||||
public class EmailNotification implements Notification {
|
||||
private final String email;
|
||||
public EmailNotification(String email) { this.email = email; }
|
||||
|
||||
@Override
|
||||
public void send(String message) {
|
||||
System.out.println("Email -> " + email + ": " + message);
|
||||
}
|
||||
}
|
||||
11
01-creational/factory-method/EmailService.java
Normal file
11
01-creational/factory-method/EmailService.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package factorymethod;
|
||||
|
||||
public class EmailService extends NotificationService {
|
||||
private final String email;
|
||||
public EmailService(String email) { this.email = email; }
|
||||
|
||||
@Override
|
||||
protected Notification createNotification() {
|
||||
return new EmailNotification(email);
|
||||
}
|
||||
}
|
||||
17
01-creational/factory-method/Main.java
Normal file
17
01-creational/factory-method/Main.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package factorymethod;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Factory Method Pattern Demo ===\n");
|
||||
|
||||
NotificationService[] services = {
|
||||
new EmailService("alice@example.com"),
|
||||
new SmsService("+1-555-0123"),
|
||||
new PushService("device-token-abc123")
|
||||
};
|
||||
|
||||
for (NotificationService service : services) {
|
||||
service.notifyUser("Your order #1042 has been shipped!");
|
||||
}
|
||||
}
|
||||
}
|
||||
5
01-creational/factory-method/Notification.java
Normal file
5
01-creational/factory-method/Notification.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package factorymethod;
|
||||
|
||||
public interface Notification {
|
||||
void send(String message);
|
||||
}
|
||||
11
01-creational/factory-method/NotificationService.java
Normal file
11
01-creational/factory-method/NotificationService.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package factorymethod;
|
||||
|
||||
/** Creator - declares the factory method */
|
||||
public abstract class NotificationService {
|
||||
protected abstract Notification createNotification();
|
||||
|
||||
public void notifyUser(String message) {
|
||||
Notification n = createNotification();
|
||||
n.send(message);
|
||||
}
|
||||
}
|
||||
11
01-creational/factory-method/PushNotification.java
Normal file
11
01-creational/factory-method/PushNotification.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package factorymethod;
|
||||
|
||||
public class PushNotification implements Notification {
|
||||
private final String deviceToken;
|
||||
public PushNotification(String deviceToken) { this.deviceToken = deviceToken; }
|
||||
|
||||
@Override
|
||||
public void send(String message) {
|
||||
System.out.println("Push -> " + deviceToken + ": " + message);
|
||||
}
|
||||
}
|
||||
11
01-creational/factory-method/PushService.java
Normal file
11
01-creational/factory-method/PushService.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package factorymethod;
|
||||
|
||||
public class PushService extends NotificationService {
|
||||
private final String token;
|
||||
public PushService(String token) { this.token = token; }
|
||||
|
||||
@Override
|
||||
protected Notification createNotification() {
|
||||
return new PushNotification(token);
|
||||
}
|
||||
}
|
||||
11
01-creational/factory-method/SmsNotification.java
Normal file
11
01-creational/factory-method/SmsNotification.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package factorymethod;
|
||||
|
||||
public class SmsNotification implements Notification {
|
||||
private final String phone;
|
||||
public SmsNotification(String phone) { this.phone = phone; }
|
||||
|
||||
@Override
|
||||
public void send(String message) {
|
||||
System.out.println("SMS -> " + phone + ": " + message);
|
||||
}
|
||||
}
|
||||
11
01-creational/factory-method/SmsService.java
Normal file
11
01-creational/factory-method/SmsService.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package factorymethod;
|
||||
|
||||
public class SmsService extends NotificationService {
|
||||
private final String phone;
|
||||
public SmsService(String phone) { this.phone = phone; }
|
||||
|
||||
@Override
|
||||
protected Notification createNotification() {
|
||||
return new SmsNotification(phone);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user