การแยกอินเทอร์เฟซในทางปฏิบัติ
ประยุกต์ใช้หลักการแยกอินเทอร์เฟซเพื่อสร้างอินเทอร์เฟซที่กระชับและเฉพาะเจาะจง หลีกเลี่ยงอินเทอร์เฟซขนาดใหญ่ที่บังคับให้ไคลเอ็นต์พึ่งพาเมธอดที่ไม่ได้ใช้งาน
การแยกอินเทอร์เฟซในทางปฏิบัติ เป็นบทเรียน Clean Architecture & Design Patterns in Practice ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Clean Architecture & Design Patterns in Practice และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Clean Architecture & Design Patterns in Practice มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is ISP?
Welcome to the Interface Segregation Principle (ISP)! This principle is one of the five SOLID principles of object-oriented design.
At its core, ISP states that clients should not be forced to depend on interfaces they do not use. In simpler terms, don't make classes implement methods they don't need.
The Problem: Fat Interfaces
A 'fat interface' is an interface that contains too many methods, some of which are irrelevant to certain classes that implement it.
When a class implements a fat interface, it's forced to provide implementations for all methods, even those it doesn't use. This often leads to empty or placeholder method bodies, which is a code smell.
Fat Interface Example
Consider a general IWorker interface. While a human worker might perform all these actions, a robot worker might not eat or sleep. Let's see how this creates issues.
interface IWorker {
void work();
void eat();
void sleep();
void manageTeam();
}
class Robot implements IWorker {
@Override
public void work() {
System.out.println("Robot working...");
}
@Override
public void eat() {
// Robots don't eat, forced to implement
System.out.println("Robot can't eat.");
}
@Override
public void sleep() {
// Robots don't sleep, forced to implement
System.out.println("Robot can't sleep.");
}
@Override
public void manageTeam() {
// Not all robots manage teams
System.out.println("Robot can't manage team.");
}
}
public class Main {
public static void main(String[] args) {
Robot robot = new Robot();
robot.work();
robot.eat(); // This call is meaningless for a robot
}
}Consequences of Fat Interfaces
As seen with the Robot example, implementing a fat interface leads to several problems:
- Code Smells: Empty or placeholder method implementations.
- Brittle Code: Changes to an interface (e.g., adding a new method) might force unrelated clients to update their code.
- Reduced Cohesion: The interface has too many responsibilities, making it less focused.
- Increased Coupling: Clients are coupled to methods they don't use, making the system harder to maintain and test.
Solution: Segregating Interfaces
The Interface Segregation Principle proposes splitting large, 'fat' interfaces into smaller, more specific ones.
Each client (class) then only implements the interfaces that are relevant to its specific responsibilities. This ensures clients are not forced to depend on methods they don't need.
ISP in Action: Refactored Code
Let's refactor our IWorker example by creating smaller, role-specific interfaces. Now, each worker type can implement only what it truly needs.
interface IWorkable {
void work();
}
interface IEatable {
void eat();
}
interface ISleepable {
void sleep();
}
interface IManager extends IWorkable { // Managers also work
void manageTeam();
}
class HumanWorker implements IManager, IEatable, ISleepable {
@Override
public void work() { System.out.println("Human working..."); }
@Override
public void eat() { System.out.println("Human eating..."); }
@Override
public void sleep() { System.out.println("Human sleeping..."); }
@Override
public void manageTeam() { System.out.println("Human managing team..."); }
}
class RobotWorker implements IWorkable { // Only works
@Override
public void work() { System.out.println("Robot working..."); }
}
public class Main {
public static void main(String[] args) {
HumanWorker human = new HumanWorker();
human.work();
RobotWorker robot = new RobotWorker();
robot.work();
// robot.eat(); // This would now be a compile error, as expected!
}
}Benefits of Using ISP
Applying ISP brings significant advantages to your software design:
- Improved Cohesion: Interfaces become more focused and represent a single, clear responsibility.
- Reduced Coupling: Clients depend only on the specific methods they require, leading to looser coupling.
- Easier Maintenance: Changes to one interface don't impact clients that don't implement it.
- Better Testability: Smaller, focused interfaces are easier to mock and test in isolation.
- Flexibility: New functionalities can be added by creating new interfaces or extending existing small ones without affecting existing clients.
ISP in Practice
ISP is particularly useful in systems with diverse client types or complex functionalities:
- Role-Based Systems: Different user roles (e.g., Administrator, Editor, Viewer) might interact with different parts of a system, each needing a specific interface.
- API Design: When designing APIs, provide specific endpoints or interfaces for different client applications (e.g., mobile apps vs. web apps).
- Plugin Architectures: Plugins can implement only the specific interfaces that define the functionalities they provide to the main application.
ISP vs. SRP: A Quick Look
While both ISP and the Single Responsibility Principle (SRP) promote focused design, they operate at different levels:
- SRP (Single Responsibility Principle): Focuses on classes, stating a class should have only one reason to change.
- ISP (Interface Segregation Principle): Focuses on interfaces and clients, ensuring clients aren't forced to depend on methods they don't use.
They often work hand-in-hand. Applying SRP to classes might naturally lead to clearer interfaces, and applying ISP can help classes better adhere to SRP by only implementing what's truly relevant to their single responsibility.
Check Your Understanding
Given an interface IDocumentProcessor with methods printDocument(), scanDocument(), faxDocument(), and copyDocument(), which of the following are good ways to apply the Interface Segregation Principle?
Lesson Summary
Great job! You've learned about the Interface Segregation Principle:
- Clients should not be forced to depend on interfaces they do not use.
- 'Fat interfaces' lead to irrelevant method implementations and brittle code.
- ISP solves this by splitting large interfaces into smaller, client-specific ones.
- This improves cohesion, reduces coupling, and makes code more maintainable and testable.
Keep these principles in mind to design more robust and flexible software!
คำถามที่พบบ่อย
บทเรียน “การแยกอินเทอร์เฟซในทางปฏิบัติ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การแยกอินเทอร์เฟซในทางปฏิบัติ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Clean Architecture & Design Patterns in Practice ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Clean Architecture & Design Patterns in Practice มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การแยกอินเทอร์เฟซในทางปฏิบัติ”
ประยุกต์ใช้หลักการแยกอินเทอร์เฟซเพื่อสร้างอินเทอร์เฟซที่กระชับและเฉพาะเจาะจง หลีกเลี่ยงอินเทอร์เฟซขนาดใหญ่ที่บังคับให้ไคลเอ็นต์พึ่งพาเมธอดที่ไม่ได้ใช้งาน คุณปฏิบัติ Clean Architecture & Design Patterns in Practice ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Clean Architecture & Design Patterns in Practice หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Clean Architecture & Design Patterns in Practice บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การแยกอินเทอร์เฟซในทางปฏิบัติ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Clean Architecture & Design Patterns in Practice นี้ได้ไหม
ได้ บทเรียน Clean Architecture & Design Patterns in Practice ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เจาะลึกการกลับทิศทางการพึ่งพา
- การแยกอินเทอร์เฟซในทางปฏิบัติ
- การปรับโครงสร้างโค้ดด้วยรูปแบบการออกแบบ
- ความเชี่ยวชาญด้านความรับผิดชอบเดียวและการเปิดรับการขยาย