Single Responsibility Principle(SRP)
Wannan ka'ida ta bayyana cewa ajin ya kamata ya kasance yana da dalili ɗaya kawai don canzawa, ma'ana kowane aji ya yi takamaiman aiki.
Misali: Sarrafa bayanan mai amfani da aika email sanarwa.
class UserManager {
constructor() {}
createUser(userData) {
// Logic for creating a user
}
}
class EmailService {
constructor() {}
sendEmail(emailData) {
// Logic for sending an email
}
}
Open/Closed Principle(OCP)
Wannan ƙa'ida tana ƙarfafa faɗaɗa ayyuka ta ƙara sabon lamba maimakon canza lambar da ke akwai.
Misali: Karɓar hanyoyin biyan kuɗi daban-daban a cikin aikace-aikacen kasuwancin e-commerce.
class PaymentProcessor {
processPayment() {
// Common logic for payment processing
}
}
class CreditCardPaymentProcessor extends PaymentProcessor {
processPayment() {
// Logic for processing credit card payment
}
}
class PayPalPaymentProcessor extends PaymentProcessor {
processPayment() {
// Logic for processing PayPal payment
}
}
Liskov Substitution Principle(LSP)
Wannan ƙa'ida ta tabbatar da cewa abubuwan da aka samo asali yakamata su kasance a musanya su da abubuwan ajin tushe ba tare da yin tasiri ga daidaiton shirin ba.
Misali: Sarrafar da siffofi na geometric.
class Shape {
area() {}
}
class Rectangle extends Shape {
constructor(width, height) {}
area() {
return this.width * this.height;
}
}
class Square extends Shape {
constructor(side) {}
area() {
return this.side * this.side;
}
}
Ƙa'idar Rarraba nterface(ISP)
Wannan ƙa'idar tana ba da shawarar ɓata musaya zuwa ƙanana don guje wa tilasta azuzuwan aiwatar da hanyoyin da ba sa buƙata.
Misali: Interfaces don ɗaukakawa da nuna bayanai.
class UpdateableFeature {
updateFeature() {}
}
class DisplayableFeature {
displayFeature() {}
}
Dependency Inversion Principle(DIP)
Wannan ka'ida ta nuna cewa matakan matakan ba za su dogara da ƙananan matakan ba; duka biyu ya kamata su dogara da abstractions.
Misali: Amfani dependency injection don sarrafa abin dogara.
class OrderProcessor {
constructor(dbConnection, emailService) {
this.dbConnection = dbConnection;
this.emailService = emailService;
}
}
Ka tuna, waɗannan misalan misalai ne kawai na yadda ake amfani da SOLID ƙa'idodin a cikin Node.js. A aikace, kuna buƙatar amfani da su cikin sassauƙa bisa manufa da sikelin aikinku.