
SOLID คือหลักการออกแบบห้าข้อ: Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation และ Dependency Inversion หลักการเหล่านี้ทำให้โค้ดเบสสะอาด ทดสอบได้ และขยายได้ง่าย ด้านล่างแต่ละข้อถูกใช้กับแอปร้านผลไม้ใน Dart และ Flutter พร้อมโค้ดเวอร์ชันแย่และเวอร์ชันดี
ไม่ใช่ทุกแอปต้องใช้ครบทั้งห้า สิ่งที่สำคัญคือรู้ว่าข้อไหนใช้กับงานนั้น
S: หลักการความรับผิดชอบเดียว (SRP)
คลาสควรมีเหตุผลเดียวที่จะเปลี่ยน ในร้านผลไม้ การเปลี่ยนวิธีแสดงผลไม้กับการเปลี่ยนวิธีคำนวณใบแจ้งหนี้ไม่ควรตกอยู่ในคลาสเดียวกัน
แย่:
class FruitAndInvoice {
final String fruitName;
final double fruitPrice;
final List<double> allFruitPrices;
FruitAndInvoice(this.fruitName, this.fruitPrice, this.allFruitPrices);
void displayFruit() {
print('Fruit: $fruitName, Price: $fruitPrice');
}
double calculateTotal() {
return allFruitPrices.fold(0, (total, price) => total + price);
}
void displayInvoice() {
displayFruit();
print('Total: ${calculateTotal()}');
}
}
ใน FruitAndInvoice การแสดงผลกับการคำนวณใบแจ้งหนี้อยู่ในคลาสเดียว การเปลี่ยนอย่างใดอย่างหนึ่งคือการแก้คลาสนี้
ดี:
class Fruit {
final String name;
final double price;
Fruit(this.name, this.price);
void displayFruit() {
print('Fruit: $name, Price: $price');
}
}
class Invoice {
final List<Fruit> fruits;
Invoice(this.fruits);
double calculateTotal() {
return fruits.fold(0, (total, fruit) => total + fruit.price);
}
void displayInvoice() {
for (final fruit in fruits) {
fruit.displayFruit();
}
print('Total: ${calculateTotal()}');
}
}
Fruit เก็บข้อมูลผลไม้ Invoice เก็บรายการผลไม้และคำนวณยอดรวม การเปลี่ยนการแสดงผลคือการแก้ Fruit การเปลี่ยนยอดรวมคือการแก้ Invoice
O: หลักการเปิด/ปิด (OCP)
ชนิดข้อมูลควรเปิดให้ขยาย และปิดไม่ให้แก้ไข ในร้านผลไม้ ส่วนลดใหม่คือคลาสใหม่ ไม่ใช่การแก้ Fruit
แย่:
class Fruit {
final String name;
final double price;
Fruit(this.name, this.price);
double discountPrice(double discount) {
return price - (price * discount);
}
}
class SeasonalFruit extends Fruit {
SeasonalFruit(String name, double price) : super(name, price);
// Override discountPrice method to offer a greater discount
@override
double discountPrice(double discount) {
return price - (price * discount * 1.5);
}
}
SeasonalFruit ทับ discountPrice และเปลี่ยนพฤติกรรมที่ Fruit มีอยู่แล้ว นั่นคือการแก้พฤติกรรมเดิม แทนการขยาย
ดี:
abstract class Fruit {
final String name;
final double price;
Fruit(this.name, this.price);
double discountPrice(double discount);
}
class RegularFruit extends Fruit {
RegularFruit(String name, double price) : super(name, price);
@override
double discountPrice(double discount) {
return price - (price * discount);
}
}
class SeasonalFruit extends Fruit {
SeasonalFruit(String name, double price) : super(name, price);
@override
double discountPrice(double discount) {
return price - (price * discount * 1.5);
}
}
Fruit เป็น abstract และประกาศ discountPrice RegularFruit กับ SeasonalFruit ขยาย Fruit และอิมพลีเมนต์เมธอดนั้น ส่วนลดใหม่คือซับคลาสใหม่ Fruit อยู่เหมือนเดิม
L: หลักการแทนที่ของ Liskov (LSP)
อ็อบเจกต์ของคลาสลูกต้องแทนอ็อบเจกต์ของคลาสฐานได้ โดยไม่ทำให้ความถูกต้องของโปรแกรมเสีย ในร้านผลไม้ ชนิดผลไม้ใหม่ต้องรักษาสัญญาเรื่องส่วนลดนั้น
แย่:
class Fruit {
final String name;
final double price;
Fruit(this.name, this.price);
double discountPrice(double discount) {
return price - (price * discount);
}
}
class SpecialFruit extends Fruit {
SpecialFruit(String name, double price) : super(name, price);
@override
double discountPrice(double discount) {
if (discount > 0.5) {
throw Exception("Discount for special fruit can't be more than 50%");
}
return price - (price * discount);
}
}
SpecialFruit ทับ discountPrice และโยน exception ถ้าส่วนลดมากกว่า 50% ผู้เรียกที่ถือ Fruit อยู่แทนที่ด้วย SpecialFruit แล้วให้โปรแกรมยังถูกต้องไม่ได้
ดี:
abstract class Fruit {
final String name;
final double price;
Fruit(this.name, this.price);
double discountPrice(double discount);
}
class RegularFruit extends Fruit {
RegularFruit(String name, double price) : super(name, price);
@override
double discountPrice(double discount) {
return price - (price * discount);
}
}
class SpecialFruit extends Fruit {
SpecialFruit(String name, double price) : super(name, price);
@override
double discountPrice(double discount) {
final adjustedDiscount = discount > 0.5 ? 0.5 : discount;
return price - (price * adjustedDiscount);
}
}
SpecialFruit เปลี่ยนสูตรส่วนลด และยังรักษาสัญญาจาก Fruit จึงยืนแทน Fruit ได้ RegularFruit กับ SpecialFruit แต่ละตัวอิมพลีเมนต์สัญญานั้น ชนิดผลไม้ใหม่คือซับคลาสอีกตัวของ Fruit
I: หลักการแยกอินเทอร์เฟซ (ISP)
ไคลเอนต์ไม่ควรถูกบังคับให้พึ่งอินเทอร์เฟซที่ตนไม่ได้ใช้ ในร้านผลไม้ การแสดงผล ส่วนลด และคลังสินค้า เป็นคนละอินเทอร์เฟซ
แย่:
abstract class FruitOperations {
void displayFruit();
double calculateDiscountPrice(double discount);
void updateInventory(int quantity);
}
class Apple implements FruitOperations {
final String name = 'Apple';
final double price = 2.0;
@override
void displayFruit() {
print('Fruit: $name, Price: $price');
}
@override
double calculateDiscountPrice(double discount) {
return price - (price * discount);
}
/**
* code to update inventory
**/
@override
void updateInventory(int quantity) {
// no-op
}
}
FruitOperations รวมการทำงานไว้ด้วยกัน ดังนั้น Apple ต้องอิมพลีเมนต์เมธอดที่ใช้กับมันไม่ได้
ดี:
abstract class FruitDisplay {
void displayFruit();
}
abstract class FruitDiscount {
double calculateDiscountPrice(double discount);
}
abstract class FruitInventory {
void updateInventory(int quantity);
}
การทำงานถูกแยกเป็น FruitDisplay, FruitDiscount และ FruitInventory Apple อิมพลีเมนต์เฉพาะอินเทอร์เฟซที่ใช้กับมัน
class Apple implements FruitDisplay, FruitDiscount {
final String name = 'Apple';
final double price = 2.0;
@override
void displayFruit() {
print('Fruit: $name, Price: $price');
}
@override
double calculateDiscountPrice(double discount) {
return price - (price * discount);
}
}
class InventoryManager implements FruitInventory {
@override
void updateInventory(int quantity) {
// code to update inventory
}
}
แต่ละอินเทอร์เฟซคือสัญญาหนึ่งฉบับ Apple กับ InventoryManager อิมพลีเมนต์เฉพาะการทำงานที่ใช้กับตัวเอง
D: หลักการกลับด้านการพึ่งพา (DIP)
โมดูลระดับสูงไม่ควรพึ่งโมดูลระดับต่ำ ทั้งคู่ควรพึ่ง abstraction ในร้านผลไม้ FruitShop พึ่ง FruitListing ไม่พึ่งคลาส Inventory ที่เป็นรูปธรรม
แย่:
class Inventory {
final List<Fruit> fruits;
Inventory(this.fruits);
void listFruits() {
for (final fruit in fruits) {
print('Fruit: ${fruit.name}, Price: ${fruit.price}');
}
}
}
class FruitShop {
final Inventory inventory;
FruitShop(this.inventory);
void displayInventory() {
inventory.listFruits();
}
}
FruitShop ถูกผูกกับ Inventory การเปลี่ยนใน Inventory บังคับให้ FruitShop เปลี่ยนตาม
ดี:
abstract class FruitListing {
void listFruits();
}
Inventory อิมพลีเมนต์ abstraction FruitListing FruitShop พึ่ง FruitListing ไม่พึ่ง Inventory
class Inventory implements FruitListing {
final List<Fruit> fruits;
Inventory(this.fruits);
@override
void listFruits() {
for (final fruit in fruits) {
print('Fruit: ${fruit.name}, Price: ${fruit.price}');
}
}
}
class FruitShop {
final FruitListing fruitListing;
FruitShop(this.fruitListing);
void displayInventory() {
fruitListing.listFruits();
}
}
FruitShop ใช้การอิมพลีเมนต์ใดก็ได้ของ FruitListing ไม่ใช่แค่ Inventory
ไม่ต้องใช้ครบทั้งห้าในครั้งเดียว ถ้าอยากได้จุดเริ่ม ให้หยิบไฟล์ที่ยุ่งที่สุดในโปรเจกต์ Flutter ปัจจุบัน แล้วถามว่ามันรักษาความรับผิดชอบเดียวข้อไหนไม่ได้ คำถามข้อเดียวนั้นมักดึง SOLID ที่เหลือตามมาด้วย
เขียนโค้ดให้สนุก!
davthecoder
ถ้าชอบบทความนี้ อย่าลืมติดตามและปรบมือให้โพสต์นี้ ;).