التماسك والاقتران وفصل الاهتمامات
أتقن ثلاث خصائص أساسية في تصميم البرمجيات تحدد قابلية صيانة الشفرة: التماسك العالي والاقتران المنخفض والفصل الواضح للاهتمامات
التماسك والاقتران وفصل الاهتمامات درس مجاني في Clean Architecture & Design Patterns in Practice على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Clean Architecture & Design Patterns in Practice، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Clean Architecture & Design Patterns in Practice 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why These Properties Matter
Under clean code and SOLID lie three deeper design properties: cohesion, coupling, and separation of concerns. They explain why the rules work.
What is Cohesion
Cohesion measures how well a module’s responsibilities belong together. High cohesion = one well-defined job; low cohesion = a grab-bag of unrelated methods.
Low vs High Cohesion
Compare the two: the Util class mixes unrelated concerns, while UserRepository is tightly focused. That focus is high cohesion.
// Low cohesion
class Util {
saveUser() {}
sendEmail() {}
formatDate() {}
}
// High cohesion
class UserRepository {
save() {}
findById() {}
}What is Coupling
Coupling measures how much one module depends on another’s internals. Tight coupling spreads changes everywhere; loose coupling keeps interactions minimal.
Tight Coupling Example
Here OrderService creates its own SmtpMailer, locking it to that exact class. Swapping the mailer means editing the service — tight coupling.
class OrderService {
constructor() {
this.mailer = new SmtpMailer();
}
}Loosening the Coupling
Inject the mailer instead. Now OrderService depends on an abstraction and accepts any implementation. That’s loose coupling.
class OrderService {
constructor(mailer) {
this.mailer = mailer;
}
}The Sweet Spot
The sweet spot is high cohesion and low coupling together: focused modules that stay independent, so systems are easy to change, test, and reason about.
Separation of Concerns
Separation of concerns means splitting a program so each part owns one distinct concern — keep UI, business logic, and data access in separate layers.
Mixed Concerns
This function tangles data access, logic, and presentation together. Touch any one concern and you risk breaking the others — mixed concerns.
function showTotal(id) {
const rows = db.query('SELECT * FROM orders WHERE id=' + id);
const total = rows.reduce((s, r) => s + r.price, 0);
document.body.innerHTML = 'Total: ' + total;
}Separated Concerns
Split it into a repository, a calculator, and a view. Each concern now changes and tests independently. Much cleaner.
const order = repository.findById(id);
const total = calculator.total(order);
view.render(total);How They Connect to SOLID
These properties underpin SOLID: single responsibility drives cohesion, dependency inversion drives low coupling, and layering expresses separation of concerns.
Quick Check
High cohesion, low coupling — can you tell them apart?
Recap
You’ve got the foundations: cohesion keeps related work together, low coupling minimizes dependencies, and separation of concerns divides by purpose.
الأسئلة الشائعة
هل درس «التماسك والاقتران وفصل الاهتمامات» مجاني؟
نعم — نص درس «التماسك والاقتران وفصل الاهتمامات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Clean Architecture & Design Patterns in Practice، انتقل إلى CoddyKit PRO. تتضمن دورة Clean Architecture & Design Patterns in Practice 4 دروس في المجموع.
ماذا ستتعلم في «التماسك والاقتران وفصل الاهتمامات»؟
أتقن ثلاث خصائص أساسية في تصميم البرمجيات تحدد قابلية صيانة الشفرة: التماسك العالي والاقتران المنخفض والفصل الواضح للاهتمامات تتمرن على Clean Architecture & Design Patterns in Practice مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Clean Architecture & Design Patterns in Practice؟
لا تُشترط خبرة سابقة. Clean Architecture & Design Patterns in Practice على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «التماسك والاقتران وفصل الاهتمامات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Clean Architecture & Design Patterns in Practice هذا؟
نعم. كل درس في Clean Architecture & Design Patterns in Practice يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- مقدمة إلى التعليمات البرمجية النظيفة
- نظرة عامة على مبادئ SOLID
- قيمة التصميم الجيد
- التماسك والاقتران وفصل الاهتمامات