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
Underline the nouns, then the verbs, then the sentences that contain the words "cannot", "must", "only" or "until" — those last ones are the rules, and they are what the software is actually for. Anyone can store a booking; the value is in refusing the ones that should be refused.
Guest    ── makes ──▶  Booking  ── for ──▶  Room
                              │
                              └── has many ──▶  Payment
Here is that description as working code. It is deliberately small — four classes, no inheritance at all — and it still enforces every rule in the paragraph. Run it and read the refusals: each one is a line of the description that would otherwise have been a bug report.

Where a rule belongs

RuleWhose businessLives in
A stay is at least one nightOne bookingBooking constructor
You cannot pay more than you oweOne bookingBooking.pay
A cancelled booking cannot be confirmedOne bookingBooking.confirm
The room must be freeAll bookingsDiary
A payment must be positiveOne paymentPayment constructor
Cancelled rows are shown in greyA screenNowhere 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.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. Which sentences in a description matter most?

2. Why does "the room must be free" not live in Booking?

3. Should every noun in the description become a class?

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 اسم متروك وصنف مخترَع، كلاهما مسمّى
How do you want to submit? / كيف تريد التسليم؟