Lesson 7 / الدرس 7
Modelling a small system / نمذجة نظام صغير
Turning a paragraph of plain description into classes. This is the skill the previous six lessons were for, and the only one of them that has no right answer — but it has a method.
تحويل فقرة وصف عادي إلى أصناف. وهذه هي المهارة التي وُجدت الدروس الستة السابقة من أجلها، والوحيدة منها بلا جواب صحيح — لكن لها طريقة.
The method: nouns, verbs, rules
"A GUEST books a ROOM for a number of NIGHTS. A BOOKING is pending until it
is confirmed. A guest may PAY in parts. A booking cannot be confirmed if
the room is already taken for those dates. Cancelling within 24 hours of
arrival keeps 50% of what was paid."
nouns -> candidates for classes Guest, Room, Booking, Payment
verbs -> candidates for methods book(), confirm(), pay(), cancel()
rules -> where the real work is pending-until-confirmed, no double
booking, the 50% refund
Guest ── makes ──▶ Booking ── for ──▶ Room
│
└── has many ──▶ Payment
Where a rule belongs
| Rule | Whose business | Lives in |
|---|---|---|
| A stay is at least one night | One booking | Booking constructor |
| You cannot pay more than you owe | One booking | Booking.pay |
| A cancelled booking cannot be confirmed | One booking | Booking.confirm |
| The room must be free | All bookings | Diary |
| A payment must be positive | One payment | Payment constructor |
| Cancelled rows are shown in grey | A screen | Nowhere near any of these |
The test is which object can see enough to answer. A booking cannot know whether the room is free — it has never heard of the other bookings — so asking it to check would mean handing it the whole diary, and then every booking would know about every booking. The rule goes where the knowledge already is.
Two things in the code are worth pointing at. First, not every noun became a class: "nights" stayed a number and "status" stayed a string, because neither has any behaviour of its own — a noun earns a class by having rules, not by being a noun. Second, Diary is a class that was not in the description at all. It appeared because a rule needed an owner and none of the obvious classes could see enough. Both moves are normal, and both come from asking about rules rather than about words.
Diary صنف لم يكن في الوصف البتة. ظهر لأن قاعدة احتاجت مالكًا ولم يستطع أي من الأصناف الظاهرة أن يرى ما يكفي. والحركتان طبيعيتان، وكلتاهما تأتي من السؤال عن القواعد لا عن الكلمات.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1. Which sentences in a description matter most?
Anyone can store a booking; the value is in refusing the ones that should be refused.
2.
Why does "the room must be free" not live in Booking?
Booking؟Handing every booking the whole diary would mean every booking knows about every booking.
3. Should every noun in the description become a class?
And a class may appear that was not a noun at all, like Diary, because a rule needed an owner.
Diary، لأن قاعدة احتاجت مالكًا.Score / النتيجة: 0 / 3
Your task / مهمتك
Write your own five-sentence description of a small system — a clinic, a library, a delivery service — including at least three rules using the words cannot, must or only. Underline the nouns, verbs and rules separately. Then build it: classes with private state, methods named as questions and commands, and every rule enforced inside an object rather than by the caller. Show each rule refusing something. Finish by naming one noun you did not turn into a class and one class you invented that was not a noun.
اكتب وصفك أنت من خمس جمل لنظام صغير — عيادة أو مكتبة أو خدمة توصيل — يتضمن ثلاث قواعد على الأقل بكلمات "لا يمكن" أو "يجب" أو "فقط". وسطّر تحت الأسماء والأفعال والقواعد كلٍّ على حدة. ثم ابنِه: أصناف بحالة خاصة، وطرائق مسمّاة أسئلةً وأوامر، وكل قاعدة مفروضة داخل كائن لا من المستدعي. وأظهر كل قاعدة وهي ترفض شيئًا. واختم بتسمية اسم واحد لم تحوّله إلى صنف وصنف واحد اخترعته ولم يكن اسمًا.
- A five-sentence description with three rules وصف من خمس جمل بثلاث قواعد
- Classes with private state and validating constructors أصناف بحالة خاصة وبناة يتحققون
- Every rule shown refusing something كل قاعدة معروضة وهي ترفض شيئًا
- One noun left out and one invented class, both named اسم متروك وصنف مخترَع، كلاهما مسمّى