Domain Modelling
Domain modelling turns real-world concepts into classes, relationships, and behaviours before you write code.Domain modelling 是把“业务里的真实东西”整理成 class、relationship 和行为。它不是先写代码,而是先弄清楚世界里有哪些概念。
A domain is the world of the problemdomain 是问题所在的世界
In a booking system, the domain contains tourists, schedules, cities, hotels, rooms, and bookings.如果你在做旅游预订系统,domain 就是游客、行程、城市、酒店、房间、预订这些概念组成的世界。
Nouns suggest classes; verbs suggest behaviour名词找 class,动词找行为
| Word词 | Likely model可能变成 | Reason原因 |
|---|---|---|
| Tourist | class | The system needs to remember tourists.系统里需要记录游客。 |
| HotelRoom | class | A room has grade, number, and availability.房间有等级、编号、可预订状态。 |
| book | method / relationship | A tourist books a room.游客和房间之间发生预订。 |
Common modelling mistakes建模时常见错误
- Do not start with generic Manager or Helper classes; find real domain concepts first.不要把
Manager、Helper当成第一个答案,先找真实 domain concept。 - Do not inherit just because two classes share fields; check the is-a relationship.不要因为两个 class 都有字段就强行继承,先判断 is-a 是否成立。