Interface
Nội dung bài · 5 mục
- 1.Khái niệm
- 2.Ví dụ
- 3.Thử ngay
- 4.Lỗi hay gặp
- 5.Tóm tắt
Đặt hàng xong, hệ thống gửi email cho khách. Tháng sau sếp muốn gửi thêm SMS. Nếu code đặt hàng gắn chặt với email thì phải sửa chính code đó. Interface giúp code đặt hàng chỉ cần biết "có ai đó gửi được thông báo", còn gửi bằng gì thì tuỳ.
Khái niệm
📜 Interface: danh sách method mà một class phải có, thường không kèm cách làm.
🤝 Implement: class ghi implements TênInterface thì cam kết viết đủ mọi method của interface đó, thiếu một cái là lỗi compile.
Ví dụ
// Main.java
void main() {
var service = new OrderService(new EmailNotifier());
service.place(); // Email: Đã đặt hàng
}
// Notifier.java
interface Notifier {
void send(String message);
}
// EmailNotifier.java
class EmailNotifier implements Notifier {
@Override
public void send(String message) {
System.out.println("Email: " + message);
}
}
// OrderService.java
class OrderService {
private final Notifier notifier;
public OrderService(Notifier notifier) {
this.notifier = notifier;
}
public void place() {
notifier.send("Đã đặt hàng");
}
}interface Notifierchỉ khai báosend, không có thân.EmailNotifier implements Notifier, nên phải có methodsend, đểpublicvà ghi@Overridenhư khi override method của class cha.OrderServicechỉ biếtNotifier, không biết email. Nó nhận notifier qua constructor.final: field chỉ gán được một lần, lúc khai báo hoặc trong constructor.- Không
new Notifier()được, nhưng biến kiểuNotifierthì giữ được bất kỳ class nào implement nó. - Một class implement được nhiều interface:
class Shop implements Notifier, Printer.
Thử ngay
Giữ các file của ví dụ. Tạo thêm file SmsNotifier.java dưới đây, rồi
thay main trong Main.java bằng đoạn gọi mới:
// SmsNotifier.java
class SmsNotifier implements Notifier {
@Override
public void send(String message) {
System.out.println("SMS: " + message);
}
}// Main.java
void main() {
new OrderService(new EmailNotifier()).place();
new OrderService(new SmsNotifier()).place();
}Đoán trước khi chạy: dòng thứ hai in ra gì?
Xem kết quả
Email: Đã đặt hàng
SMS: Đã đặt hàngDòng thứ hai là SMS: Đã đặt hàng. OrderService gọi send của object được
truyền vào. Thêm cách gửi mới chỉ cần viết class mới, không sửa
OrderService.
Lỗi hay gặp
Implement thiếu method. Class đã ghi implements Notifier thì phải viết
đủ.
// SAI — lỗi compile: thiếu method send
// Main.java
void main() {
new PushNotifier();
}
// PushNotifier.java
class PushNotifier implements Notifier {
}// ĐÚNG
// PushNotifier.java
class PushNotifier implements Notifier {
@Override
public void send(String message) {
System.out.println("Push: " + message);
}
}Tạo object từ interface. Interface không có code để chạy.
// SAI — lỗi compile: không new được interface
Notifier notifier = new Notifier();// ĐÚNG — new một class implement interface đó
Notifier notifier = new EmailNotifier();Tóm tắt
- Interface liệt kê method mà class phải có, thường không kèm cách làm.
- Class
implements TênInterfacephải viết đủ mọi method, đểpublic. - Code chỉ phụ thuộc vào interface thì đổi được cách làm mà không phải sửa code đó.
- Không
newđược interface. Một class implement được nhiều interface. - Method trong interface có thể kèm thân sẵn nếu ghi
defaultở đầu, class không bắt buộc viết lại method đó.
Tự kiểm tra
0/3 câuinterface Printer { void print(String text); } Class nào implement Printer đúng?
Class ReportService nhận Exporter qua constructor. Muốn xuất thêm định dạng Excel, cần làm gì?
Dòng nào KHÔNG compile được, biết EmailNotifier implement Notifier?