التغليف (Encapsulation)
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- مُعدِّلات الوصول
- getters/setters
- الثوابت الثابتة (invariants)
- عدم القابلية للتغيير
تعلّم أولًا
68 دقيقة قراءة · 3 ساعة تدريب (تقدير)
مخرجات التعلم
- Apply
private, package-private,protected, andpubliccorrectly. - Protect invariants (e.g., balance never negative) inside the class, not in its callers.
- Decide when a getter or setter is appropriate and when a behavior method is better.
- Create an immutable class (final fields, no setters, defensive copies).
الدروس
المختبر والتمارين
Lab
BEG-021-LAB1 · Bank Accounts v2
Outcomes: 1, 2, 3, 4 · Time: ~2 h Make the fields of Account private. Enforce invariants (balance never negative, amounts positive) in the constructor and withdraw. Replace setBalance with behavior methods. Add an immutable Money class (final fields, no setters). Acceptance: no way to reach an invalid state from outside the class.
Exercises (assessment: code review exercise)
| ID | Outcome | Task |
|---|---|---|
| BEG-021-EX01 | 1 | Which of private, package-private, protected, public would you use for: a helper method, a field, a service method called from other packages, a method meant for subclasses? |
| BEG-021-EX02 | 2 | Add the rule "age is between 0 and 150" to a Person class so it cannot be broken. |
| BEG-021-EX03 | 3 | Rewrite if (c.getItems() > 0) c.setItems(c.getItems() - 1); as a behavior method. |
| BEG-021-EX04 | 4 | Make Money fully immutable and add plus. |
| BEG-021-EX05 | 4 | Fix a Team class that returns its internal List<String> members directly. |
| BEG-021-EX06 | 1, 2, 3, 4 | Code review: in a provided class, list every encapsulation violation and how to fix it. |
أظهر الحلول
- EX01 private; private; public; protected.
- EX02 Private field; check in the constructor and the setter (if any) and throw
IllegalArgumentExceptionwhen out of range. - EX03
public void removeItem() { if (items == 0) throw new IllegalStateException(); items--; }thenc.removeItem();. - EX04
public final class Money { private final long cents; ... public Money plus(Money o) { return new Money(cents + o.cents); } }. - EX05 Return
List.copyOf(members)(or an unmodifiable view), and copy any incoming list in the constructor. - EX06 Typical findings: public fields, a setter that skips validation, reflex getters/setters, returned mutable collection, missing constructor validation. Graded against the review checklist.
الاختبار
Outcomes: O1 access modifiers · O2 invariants · O3 getters/setters vs behavior · O4 immutability.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
Code review exercise BEG-021-EX06 plus EX01 to EX05 and Bank Accounts v2. Pass: review finds at least four of the planted violations and the lab meets its acceptance.
أخطاء شائعة
Generating getters/setters for every field by reflex; returning internal mutable arrays.
التقييم
Code review exercise: find encapsulation violations in a provided class.
الصلة بالمسار الوظيفي
Value objects (MID-060) and safe APIs (JNR-020).