SOLID yaiku lima prinsip desain kanggo ngbangun sistem OOP sing manteb lan fleksibel. Saben siji ngatasi penyebab umum kode sing kaku lan rapuh.
| Huruf | Prinsip | Makna sabaris |
|---|
| S | Single Responsibility | Kelas kudu duwe siji alasan kanggo owah |
| O | Open/Closed | Mbukak kanggo extensi, ditutup kanggo modifikasi |
| L | Liskov Substitution | Subtipe kudu bisa dipakai ing pacokan kang basa tipe |
| I | Interface Segregation | Akeh interface cilik luwih apik tinimbang siji gedhe |
| D | Dependency Inversion | Gumantung ing abstraksi, ora ing kelas konkrit |
# Dependency Inversion: high-level code depends on an abstraction
class Notifier: # abstraction
def send(self, msg): ...
class EmailNotifier(Notifier):
def send(self, msg): print("email:", msg)
class OrderService:
def __init__(self, notifier: Notifier): # injected abstraction
self.notifier = notifier # not "new EmailNotifier()"
def place(self):
self.notifier.send("order placed") # works with ANY Notifier
OrderService ora gumantung ing email kanthi khusus — ganti karo SMS utawa test double tanpa menyenggol (D). Nambah tipe notifier anyar ora perlu owah OrderService (O).
SOLID minangka tuntunan, dudu hukum. Over-applika — interface saben kelas, indirections ing endi-endi — ngasilake "abstraction soup" sing luwih angel diikut tinimbang masalah sing dikempal.
SOLID menehi basa diagnostik bareng: panyawat bisa jeneng kenapa kelas angel owah ("iki nglanggar SRP") bubar debat miturut rasa.
Bisa ditrapake karo bijak, prinsip-prinsip kuwi ngurangi ripple effect — owah tetep lokal, fitur anyar nambah tinimbang nulis maneh, lan tes bisa ganti fake kanggo dependensi nyata.