0Pricing
Clean Architecture & Design Patterns in Practice · درس

فرض قواعد الأعمال باستخدام الثوابت

تعلّم ترميز قواعد الأعمال الحرجة كثوابت داخل الكيانات، بحيث لا يمكن للمجال الدخول في حالة غير صالحة.

فرض قواعد الأعمال باستخدام الثوابت درس مجاني في 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 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Rules That Must Always Hold

Beyond use-case logic, a domain has rules that must be true at all times. An account balance never goes below zero; an order always has at least one line item.

These are invariants, and the core should make them impossible to violate.

Entity Invariants vs Use-Case Rules

Two layers of rules exist:

  • Entity invariants: always true regardless of operation (Enterprise Business Rules).
  • Use-case rules: govern a specific workflow (Application Business Rules).

Invariants belong inside the entity itself.

The Danger of Anemic Entities

An anemic entity is just public fields with no protection.

class Account {
    public double balance; // anyone can set anything
}

Guarding Construction

Enforce invariants when the entity is created. An invalid entity should never exist.

class Account {
    private double balance;
    Account(double initial) {
        if (initial < 0) throw new IllegalArgumentException("balance < 0");
        this.balance = initial;
    }
}

Guarding Mutation

Every state change must preserve the invariant. Expose intent-revealing methods, not setters.

void withdraw(double amount) {
    if (amount <= 0) throw new IllegalArgumentException("amount");
    if (amount > balance) throw new IllegalStateException("insufficient funds");
    balance -= amount;
}

Make Illegal States Unrepresentable

A stronger goal than checking: design types so invalid states cannot be expressed.

Use value objects, enums, and required constructor arguments so the compiler enforces what it can before runtime checks ever run.

Value Objects Carry Their Own Rules

Wrap primitives in small immutable types that validate themselves.

final class Email {
    private final String value;
    Email(String v) {
        if (!v.contains("@")) throw new IllegalArgumentException("bad email");
        this.value = v;
    }
}

A Runnable Demonstration

The entity rejects an invalid operation, protecting the invariant.

public class Main {
  static class Account {
    private double balance;
    Account(double b){ if(b<0) throw new IllegalArgumentException(); balance=b; }
    void withdraw(double a){ if(a>balance) throw new IllegalStateException("insufficient"); balance-=a; }
    double getBalance(){ return balance; }
  }
  public static void main(String[] args){
    Account acc = new Account(100);
    acc.withdraw(30);
    System.out.println("Balance: " + acc.getBalance());
    try { acc.withdraw(1000); } catch(Exception e){ System.out.println("Blocked: " + e.getMessage()); }
  }
}

Keep Infrastructure Out

Invariant checks are pure domain logic. They must not reach for a database, network, or framework.

If validating a rule needs external data, that check belongs in a use case (interactor), not the entity.

Invariants and Testing

Because invariants live in pure entities, they are trivial to test: construct, call a method, assert the rule held or the operation was rejected.

No mocks, no database, no framework — fast and reliable tests.

Design Guidelines

  • Validate on construction and on every mutation.
  • Replace public setters with intent-revealing methods.
  • Use immutable value objects for self-validating data.
  • Keep all checks framework-free.

Quick Check

Test your understanding of invariants.

Recap

You learned to protect the domain with invariants.

  • Entity invariants always hold; use-case rules govern workflows.
  • Validate on creation and mutation; prefer unrepresentable illegal states.
  • Keep checks pure for easy testing.

الأسئلة الشائعة

هل درس «فرض قواعد الأعمال باستخدام الثوابت» مجاني؟

نعم — نص درس «فرض قواعد الأعمال باستخدام الثوابت» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تصميم كيانات الأعمال
  2. تطبيق Use Cases (Interactors)
  3. منافذ الإدخال والإخراج
  4. فرض قواعد الأعمال باستخدام الثوابت
← العودة إلى Clean Architecture & Design Patterns in Practice