رقصنده با کد
782 subscribers
1.69K photos
850 videos
207 files
666 links
Here are some interesting things I've come across during my learning process. That's it. Admin ID:
@alithecodeguy
Download Telegram
SOLID principles in React 🔥
#solid
💡 اصول SOLID چیست؟ (به زبان ساده و با مثال‌های جاوااسکریپتی)

اصول SOLID پنج اصل کلیدی در طراحی شی‌ء‌گرا (Object-Oriented Design) هست که کمک می‌کنه کدت:

• قابل توسعه باشه (افزودن ویژگی جدید راحت باشه)
• قابل نگهداری باشه (تغییرات خراب نکنه)
• قابل تست باشه (نوشتن تست ساده‌تر بشه)
• و خواناتر و منظم‌تر باشه

این اصول توسط Robert C. Martin (Uncle Bob) معرفی شدند، و پایه‌ای برای نوشتن کد تمیز (Clean Code) هستند.

کلمه SOLID مخفف ۵ اصل زیره:

------------------------

🔹 1 — Single Responsibility Principle (SRP)

اصل مسئولیت یکتا

A class should have one, and only one, reason to change.

🔸 یعنی هر کلاس یا ماژول فقط یک وظیفه‌ی مشخص باید داشته باشه. اگر چند کار مختلف انجام بده، تغییر در یکی ممکنه کل کلاس رو خراب کنه.

مثال صحیح :


class User {
constructor(name, email) {
this.name = name;
this.email = email;
}
}

class UserRepository {
save(user) {
// save to database
}
}


------------------------

🔹 2 — Open/Closed Principle (OCP)

اصل باز برای توسعه، بسته برای تغییر

Software entities should be open for extension, but closed for modification.

🔸 یعنی باید بتونی بدون تغییر دادن کد قبلی، رفتار جدیدی بهش اضافه کنی (مثلاً با ارث‌بری یا ترکیب کلاس‌ها).

مثال صحیح :


class Discount {
calculate(price) {
return price;
}
}

class GoldDiscount extends Discount {
calculate(price) {
return price * 0.8;
}
}


------------------------

🔹 3 — Liskov Substitution Principle (LSP)

اصل جایگزینی لیسکوف

Subtypes must be substitutable for their base types without breaking the program.

🔸 یعنی کلاس فرزند باید بتونه بدون خراب کردن منطق برنامه، جای کلاس والد استفاده بشه.

مثال صحیح :


class Bird {
makeSound() {
console.log("chirp");
}
}

class Parrot extends Bird {}

function playSound(bird) {
bird.makeSound();
}

playSound(new Parrot()); // درست کار می‌کنه


مثال نادرست:


class Ostrich extends Bird {
fly() {
throw new Error("Can't fly");
}
}


------------------------

🔹 4 — Interface Segregation Principle (ISP)

اصل تفکیک واسط‌ها

Clients should not be forced to depend on interfaces they do not use.

🔸 یعنی به جای ساختن یک کلاس یا اینترفیس بزرگ، چند تای کوچکتر بساز که فقط نیازهای خاص رو پوشش بده.

مثال صحیح :


class Printer {
print() {}
}

class Scanner {
scan() {}
}

class AllInOneMachine {
constructor(printer, scanner) {
this.printer = printer;
this.scanner = scanner;
}
}


------------------------

🔹 5 — Dependency Inversion Principle (DIP)

اصل وارونگی وابستگی

High-level modules should not depend on low-level modules. Both should depend on abstractions.

🔸 یعنی کدهای سطح بالا نباید مستقیماً به جزئیات پیاده‌سازی وابسته باشن؛ باید به abstraction (مثل interface) وابسته باشن.

مثال صحیح :


class EmailService {
send(msg) {
console.log("Sending email:", msg);
}
}

class Notifier {
constructor(service) {
this.service = service;
}

notify(message) {
this.service.send(message);
}
}

const email = new EmailService();
const notifier = new Notifier(email);
notifier.notify("Hello!");


مطالب بیشتر در کانال رقصنده با کد:
https://t.iss.one/danceswithcode

#interview #solid @alithecodeguy