SOLID Principle: แนวทางของซูเปอร์ฮีโร่ในการเขียน Clean Code!

Feb 18 2023
บันทึกรหัสของคุณจากก้นบึ้งของรหัส Spaghetti ด้วยหลักการง่าย ๆ เหล่านี้!
หากคุณเป็นนักพัฒนาซอฟต์แวร์ คุณอาจเคยได้ยินเกี่ยวกับ SO
ฉากจาก Bruce Almighty

หากคุณเป็นนักพัฒนาซอฟต์แวร์ คุณอาจเคยได้ยินเกี่ยวกับ SOLID ซึ่งเป็นตัวย่อที่ย่อมาจาก:

  • หลักการความรับผิดชอบเดียว (SRP)
  • หลักการเปิด/ปิด (OCP)
  • หลักการทดแทนลิสคอฟ (LSP)
  • หลักการแยกส่วนต่อประสาน (ISP)
  • หลักการผกผันการพึ่งพา (DIP)

มาดูรายละเอียดเพิ่มเติมในหลักการแต่ละข้อ พร้อมตัวอย่างโค้ด Java เพื่อช่วยอธิบายแนวคิด

1. หลักการรับผิดชอบคนเดียว (SRP)

SRP ระบุว่าชั้นเรียนควรมีเพียงจุดประสงค์เดียวเท่านั้น กล่าวอีกนัยหนึ่ง ชั้นเรียนควรมีความรับผิดชอบเพียงอย่างเดียว หลักการนี้ช่วยให้แน่ใจว่าโค้ดของคุณเน้น อ่านได้ และดูแลรักษาง่าย เราไม่ควรสร้างภาระให้กับชั้นเรียนด้วยฟังก์ชันที่มากเกินไป

สร้างโดยผู้เขียนโดยใช้ imgflip.com

สมมติว่าคุณมีUserคลาสที่จัดการการรับรองความถูกต้องของผู้ใช้และการแจ้งเตือนทางอีเมล ต่อไปนี้คือตัวอย่างวิธีที่คุณสามารถปรับโครงสร้างคลาสนี้ใหม่ให้เป็นไปตาม SRP:

// Before refactoring

public class User {
  public void login(String username, String password) {
    // Authenticate user
  }
  
  public void sendEmail(String to, String subject, String body) {
    // Send email
  }
}
// After refactoring

public class User {
  public void login(String username, String password) {
    // Authenticate user
  }
}

public class EmailService {
  public void sendEmail(String to, String subject, String body) {
    // Send email
  }
}

OCP ระบุว่าเอนทิตีของซอฟต์แวร์ (คลาส โมดูล ฟังก์ชัน ฯลฯ) ควรเปิดให้ขยายแต่ปิดเพื่อแก้ไข กล่าวอีกนัยหนึ่ง คุณควรจะสามารถเพิ่มฟังก์ชันใหม่ให้กับโค้ดของคุณได้โดยไม่ต้องเปลี่ยนโค้ดที่มีอยู่

สร้างโดยผู้เขียนโดยใช้ imgflip.com

สมมติว่าคุณมีPaymentProcessorชั้นเรียนที่จัดการการประมวลผลการชำระเงินสำหรับวิธีการชำระเงินต่างๆ ต่อไปนี้คือตัวอย่างวิธีที่คุณสามารถปรับโครงสร้างคลาสนี้ใหม่ให้เป็นไปตาม OCP:

// Before refactoring

public class PaymentProcessor {
  public void processPayment(Payment payment) {
    if (payment.getMethod() == PaymentMethod.CREDIT_CARD) {
      // Process credit card payment
    } else if (payment.getMethod() == PaymentMethod.PAYPAL) {
      // Process PayPal payment
    } else if (payment.getMethod() == PaymentMethod.BITCOIN) {
      // Process Bitcoin payment
    }
  }
}
// After refactoring

public interface PaymentMethodProcessor {
  void processPayment(Payment payment);
}

public class CreditCardProcessor implements PaymentMethodProcessor {
  public void processPayment(Payment payment) {
    // Process credit card payment
  }
}

public class PayPalProcessor implements PaymentMethodProcessor {
  public void processPayment(Payment payment) {
    // Process PayPal payment
  }
}

public class BitcoinProcessor implements PaymentMethodProcessor {
  public void processPayment(Payment payment) {
    // Process Bitcoin payment
  }
}

public class PaymentProcessor {
  private Map<PaymentMethod, PaymentMethodProcessor> processors = new HashMap<>();
  
  public PaymentProcessor() {
    processors.put(PaymentMethod.CREDIT_CARD, new CreditCardProcessor());
    processors.put(PaymentMethod.PAYPAL, new PayPalProcessor());
    processors.put(PaymentMethod.BITCOIN, new BitcoinProcessor());
  }
  
  public void processPayment(Payment payment) {
    PaymentMethodProcessor processor = processors.get(payment.getMethod());
    if (processor != null) {
      processor.processPayment(payment);
    }
  }
}

3. หลักการทดแทนลิสคอฟ (LSP)

Liskov Substitution Principle (LSP) ระบุว่าอ็อบเจ็กต์ของซูเปอร์คลาสควรแทนที่ได้ด้วยอ็อบเจ็กต์ของคลาสย่อยโดยไม่ทำให้แอปพลิเคชันเสียหาย กล่าวอีกนัยหนึ่ง คลาสย่อยควรสามารถใช้แทนซูเปอร์คลาสได้โดยไม่ทำให้เกิดพฤติกรรมหรือข้อผิดพลาดที่ไม่คาดคิด

สร้างโดยผู้เขียนโดยใช้ imgflip.com

สมมติว่าคุณมีRectangleคลาสที่คำนวณพื้นที่ของสี่เหลี่ยมผืนผ้า ต่อไปนี้คือตัวอย่างวิธีที่คุณสามารถปรับโครงสร้างคลาสนี้ใหม่ให้เป็นไปตาม LSP:

// Before refactoring

public class Rectangle {
  private int width;
  private int height;
  
  public int getWidth() {
    return width;
  }
  
  public void setWidth(int width) {
    this.width = width;
  }
  
  public int getHeight() {
    return height;
  }
  
  public void setHeight(int height) {
    this.height = height;
  }
  
  public int getArea() {
    return width * height;
  }
}

public class Square extends Rectangle {
  public void setWidth(int width) {
    super.setWidth(width);
    super.setHeight(width);
  }
  
  public void setHeight(int height) {
    super.setWidth(height);
    super.setHeight(height);
  }
}

// Test code
Rectangle rect = new Square();
rect.setWidth(5);
rect.setHeight(10);
System.out.println(rect.getArea()); // Expected output: 50

// After refactoring

public class Rectangle {
  protected int width;
  protected int height;
  
  public Rectangle(int width, int height) {
    this.width = width;
    this.height = height;
  }
  
  public int getWidth() {
    return width;
  }
  
  public void setWidth(int width) {
    this.width = width;
  }
  
  public int getHeight() {
    return height;
  }
  
  public void setHeight(int height) {
    this.height = height;
  }
  
  public int getArea() {
    return width * height;
  }
}

public class Square extends Rectangle {
  public Square(int size) {
    super(size, size);
  }
}

// Test code
Rectangle rect = new Rectangle(5, 10);
System.out.println(rect.getArea()); // Expected output: 50

Square square = new Square(5);
System.out.println(square.getArea()); // Expected output: 25

หลักการแยกอินเทอร์เฟซ (ISP) ระบุว่าไคลเอนต์ไม่ควรถูกบังคับให้ใช้อินเทอร์เฟซที่ไม่ได้ใช้ กล่าวอีกนัยหนึ่ง คุณควรแยกอินเทอร์เฟซออกเป็นอินเทอร์เฟซที่เล็กลงและเน้นมากขึ้น ซึ่งจะนำไปใช้และเปลี่ยนแปลงได้ง่ายกว่า

สร้างโดยผู้เขียนโดยใช้ imgflip.com

สมมติว่าคุณมี อินเทอร์เฟ ซPrinterที่มีสองวิธี: print()และ scan()ต่อไปนี้คือตัวอย่างวิธีที่คุณสามารถปรับเปลี่ยนโครงสร้างอินเทอร์เฟซนี้ให้เป็นไปตาม ISP:

// Before refactoring

public interface Printer {
  void print();
  void scan();
}

public class LaserPrinter implements Printer {
  public void print() {
    // Print document
  }
  
  public void scan() {
    // Scan document
  }
}

public class InkjetPrinter implements Printer {
  public void print() {
    // Print document
  }
  
  public void scan() {
    // Do nothing
  }
}
// After refactoring

public interface Printable {
  void print();
}

public interface Scanable {
  void scan();
}

public class LaserPrinter implements Printable, Scanable {
  public void print() {
    // Print document
  }

public void scan() {
  // Scan document
  }
}

public class InkjetPrinter implements Printable {
  public void print() {
    // Print document
  }
}

// Test code
Printable laserPrinter = new LaserPrinter();
laserPrinter.print();
((Scanable)laserPrinter).scan();

Printable inkjetPrinter = new InkjetPrinter();
inkjetPrinter.print();

5. หลักการผกผันการพึ่งพา (DIP)

หลักการพึ่งพาการผกผัน (DIP) ระบุว่าโมดูลระดับสูงไม่ควรขึ้นอยู่กับโมดูลระดับต่ำ ทั้งสองควรขึ้นอยู่กับสิ่งที่เป็นนามธรรม สิ่งที่เป็นนามธรรมไม่ควรขึ้นอยู่กับรายละเอียด รายละเอียดควรขึ้นอยู่กับนามธรรม กล่าวอีกนัยหนึ่ง โค้ดของคุณควรขึ้นอยู่กับอินเทอร์เฟซหรือคลาสนามธรรม แทนที่จะนำไปใช้อย่างเป็นรูปธรรม

สร้างโดยผู้เขียนโดยใช้ imgflip.com

สมมติว่าคุณมีBookคลาสที่ต้องการดึงข้อมูลจากDatabaseคลาส ต่อไปนี้คือตัวอย่างวิธีที่คุณสามารถปรับเปลี่ยนโครงสร้างโค้ดนี้เพื่อให้เป็นไปตาม DIP:

// Before refactoring

public class Book {
 private Database database;
 
 public Book() {
 this.database = new Database();
 }
 
 public String getTitle(int id) {
 return database.getTitle(id);
 }
}
public class Database {
 public String getTitle(int id) {
 // Retrieve title from database
 return "Title";
 }
}
// After refactoring

public interface Database {
 String getTitle(int id);
}
public class Book {
 private Database database;
 
 public Book(Database database) {
 this.database = database;
 }
 
 public String getTitle(int id) {
 return database.getTitle(id);
 }
}
public class DatabaseImpl implements Database {
 public String getTitle(int id) {
 // Retrieve title from database
 return "Title";
 }
}
// Test code
Database database = new DatabaseImpl();
Book book = new Book(database);
System.out.println(book.getTitle(123)); // Expected output: "Title"

บทสรุป

หลักการ SOLID คือแนวทางชุดหนึ่งที่สามารถช่วยให้คุณเขียนโค้ดได้ดีขึ้นและสามารถบำรุงรักษาได้มากขึ้น เมื่อปฏิบัติตามหลักการเหล่านี้ คุณจะสามารถทำให้โค้ดของคุณเป็นแบบแยกส่วน ยืดหยุ่น และนำมาใช้ซ้ำได้

โปรดจำไว้ว่าหลักการเหล่านี้ไม่ใช่กฎที่ยากและรวดเร็ว แต่เป็นหลักเกณฑ์ที่จะช่วยให้คุณออกแบบซอฟต์แวร์ได้ดีขึ้น เช่นเดียวกับแนวปฏิบัติอื่นๆ อาจมีบางกรณีที่ควรเบี่ยงเบนไปจากหลักการเพื่อให้บรรลุเป้าหมายที่เฉพาะเจาะจง อย่างไรก็ตาม โดยทั่วไปแล้ว การปฏิบัติตามหลักการ SOLID จะช่วยให้คุณเขียนโค้ดได้สะอาดขึ้น บำรุงรักษาได้มากขึ้น และขยายได้มากขึ้น

เพื่อสรุปหลักการ SOLID สามารถสรุปได้ดังนี้:

  • SRP: ชั้นเรียนควรมีจุดประสงค์เดียวเท่านั้น
  • OCP: เอนทิตีซอฟต์แวร์ควรเปิดเพื่อขยาย แต่ปิดเพื่อแก้ไข
  • LSP: ชนิดย่อยควรใช้แทนประเภทฐานได้
  • ISP: ไคลเอนต์ไม่ควรถูกบังคับให้ใช้อินเทอร์เฟซที่ไม่ได้ใช้
  • กรมทรัพย์สินทางปัญญา: โมดูลระดับสูงไม่ควรขึ้นอยู่กับโมดูลระดับต่ำ ทั้งสองควรขึ้นอยู่กับสิ่งที่เป็นนามธรรม

ในหมายเหตุสุดท้าย ควรกล่าวว่าหลักการ SOLID เป็นเพียงแง่มุมหนึ่งของการออกแบบซอฟต์แวร์ที่ดี มีหลักการออกแบบและแนวทางปฏิบัติที่ดีที่สุดอื่นๆ อีกมากมายที่คุณสามารถนำไปใช้กับโค้ดของคุณเพื่อให้มีประสิทธิภาพและบำรุงรักษาได้มากขึ้น อย่างไรก็ตาม เมื่อปฏิบัติตามหลักการ SOLID คุณจะสามารถสร้างรากฐานที่มั่นคงให้กับซอฟต์แวร์ของคุณ และเตรียมตัวเองให้พร้อมสำหรับความสำเร็จในระยะยาว

หากคุณต้องการเรียนรู้เพิ่มเติมเกี่ยวกับหลักการ SOLID มีแหล่งข้อมูลดีๆ มากมายทางออนไลน์ รวมถึงหนังสือ บทความ และวิดีโอ คุณยังสามารถสำรวจรูปแบบการออกแบบและแนวทางปฏิบัติที่ดีที่สุดอื่นๆ ที่สามารถช่วยให้คุณเขียนโค้ดได้ดีขึ้น

ฉันขอแนะนำให้คุณเรียนรู้และสำรวจแนวคิดและเทคนิคใหม่ๆ ต่อไปเพื่อปรับปรุงการออกแบบซอฟต์แวร์ของคุณ ด้วยการลงทุนในทักษะและความรู้ของคุณ คุณสามารถเป็นนักพัฒนาซอฟต์แวร์ที่ดีขึ้นและมีประสิทธิภาพมากขึ้น และมีส่วนร่วมในความสำเร็จของทีมและองค์กรของคุณ

ขอบคุณสำหรับการอ่านและมีความสุขในการเขียนโค้ด!

Want to Connect? If you have any feedback, 
please ping me on my LinkedIn: https://linkedin.com/in/shuhanmirza/

      
                
Source