interface Notification { void send(String message); } class EmailNotification implements Notification { public void send(String message) { System.out.println("Отправлено письмо: " + message); } } class SmsNotification implements Notification { public void send(String message) { System.out.println("Отправлено SMS: " + message); } } // Класс, использующий абстракцию, а не конкретную реализацию class OrderService { private final Notification notification; public OrderService(Notification notification) { this.notification = notification; } void placeOrder(String product) { System.out.println("Заказ оформлен: " + product); notification.send("Ваш заказ отправлен!"); } } public class Main { public static void main(String[] args) { OrderService service = new OrderService(new EmailNotification()); service.placeOrder("Ноутбук"); } }
Здесь:
SRP: OrderService отвечает только за оформление заказа.
OCP: можно добавить PushNotification без изменения OrderService.
LSP: любые Notification можно взаимозаменять.
ISP: интерфейс Notification содержит ровно то, что нужно.
DIP: OrderService зависит от интерфейса, а не от конкретного класса.
interface Notification { void send(String message); } class EmailNotification implements Notification { public void send(String message) { System.out.println("Отправлено письмо: " + message); } } class SmsNotification implements Notification { public void send(String message) { System.out.println("Отправлено SMS: " + message); } } // Класс, использующий абстракцию, а не конкретную реализацию class OrderService { private final Notification notification; public OrderService(Notification notification) { this.notification = notification; } void placeOrder(String product) { System.out.println("Заказ оформлен: " + product); notification.send("Ваш заказ отправлен!"); } } public class Main { public static void main(String[] args) { OrderService service = new OrderService(new EmailNotification()); service.placeOrder("Ноутбук"); } }
Здесь:
SRP: OrderService отвечает только за оформление заказа.
OCP: можно добавить PushNotification без изменения OrderService.
LSP: любые Notification можно взаимозаменять.
ISP: интерфейс Notification содержит ровно то, что нужно.
DIP: OrderService зависит от интерфейса, а не от конкретного класса.
// Пример кода
function hello(name) {
console.log(`Hi, ${name}!`);
}
hello("Sergey");