Designing a Program Around Entities
Object-oriented programming models a problem as a set of objects instead of a sequence of tasks, so the program's structure follows the entities of the problem.
Where to Begin with a Lending Library
Suppose you are asked to write a program that keeps track of books, borrowers, and due dates at a small lending library. Where do you start? Many people's first instinct is to think about what the program must do: check a book out, record a return, send a reminder. That instinct is not wrong, but it points you toward one particular style of design.
Object-oriented programming, usually abbreviated OOP, starts from a different question: what are the things in this problem, and how do they relate to one another? The aim is to build a program whose structure looks like the situation it describes, so that the code reads the way people already think about the world. In this article you will see what changes when you begin with the things rather than with the jobs, and how to recognise the mistakes that come from skipping that step.
Two Ways to Organise a Design
In older styles of programming, the work begins by identifying a computing task that solves the problem. The programmer then looks for a sequence of instructions that accomplishes that task. The program is organised around the procedure, the ordered steps that get something done.
Object-oriented programming reverses the emphasis. Instead of centring the design on tasks, it centres the design on objects. Designing the program means deciding which objects are needed and how they should work together, so that the set of objects as a whole models the problem at hand. Once the objects are in place, the tasks tend to show up as things the objects do, rather than as the starting point of the design.
Notice what the left column is doing: it is a list of jobs, and the design is shaped by the order in which those jobs happen. The right column is a list of entities. The jobs have not disappeared; they will reappear as behaviors of these entities. The difference is where the design starts.
Anatomy of an Object
An object is an entity that has behaviors, holds information, and can interact with other objects. Those three properties are the whole checklist. When you design an object, you decide what it holds, what it can do, and how it works with the other objects around it.
- Information: the facts the object stores about itself, such as a book's title or a due date's day
- Behaviors: the things the object can do, such as a book being lent out or a due date reporting that it has passed
- Interactions: the way the object works with other objects, asking them for help or responding when they ask it for something
The three parts depend on each other. An object's behaviors usually read or change its own information, and its interactions are how it gets information it does not hold itself. A book cannot record its own return without the borrower's record also being updated, so the book and the borrower must interact. Designing well means identifying the entities first, then deciding what each one holds, what each one can do, and how the objects work together.
Following a Checkout Through Objects
Now trace what happens when a borrower takes a book home. In an object-centred design, no single procedure controls the whole job. Instead, the objects involved send requests to one another, and each object does the part of the work that belongs to it. The diagram below shows the order of those requests for a checkout in the library scenario. This example is original to this article and applies the three properties of an object to the library problem.
Tracing one checkout
A borrower asks to take out a book. Which objects are involved, and what does each one contribute?
- 1Identify the entities
The problem involves a Borrower, a Book, and a Loan. Each one holds information: the borrower holds a name, the book holds its status, and the loan holds a due date.
- 2Send the first request
The borrower does not change the book's status directly. It asks the Book to be checked out to it, which keeps the book's own rules in the book.
- 3Let the Book create the Loan
The Book asks a new Loan object to record the arrangement. The Loan holds the due date, so the due date belongs to the Loan rather than being scattered through the program.
- 4Get the information back
When the Book needs the due date to confirm the checkout, it interacts with the Loan and receives the date from the object that holds it.
- 5Confirm
The Book confirms the checkout to the Borrower. No one procedure reached into every object to set every value.
The checkout is completed by four requests between three objects, and each piece of information is held by the object responsible for it.
Which Things Become Objects
Software objects can represent real entities, such as a physical book, or abstract entities, such as a due date, as long as they matter to the problem. The entity does not have to be something you can touch. A reservation, a due date, or a rule about overdue fines can be an abstract entity that a program still treats as an object, because it carries information and takes part in behavior.
The question to ask about any candidate is whether the thing matters to the problem and can be described by what it knows and what it does. A book matters to a lending library and knows its own status. A due date matters because the library cares when items are due, and it knows a date and can report whether that date has passed. A passing comment about the weather in the library's opening hours probably does not matter, so it does not need to become an object.
What do you think happens?
In the library problem, should a due date become an object, or is it just a number stored inside something else?
Reveal answer
It can be an object if it matters to the problem and carries information and behavior.
The source states that abstract entities such as a due date can be objects when they carry information and take part in behavior, and that the deciding question is whether the thing matters to the problem. A due date that the library checks for overdue status fits that description.
Mapping Entities in a Clinic Scenario
A small clinic wants a program to track patients, appointments, and doctors. Before writing any task list, identify the entities. For each entity you choose, name one piece of information it holds and one behavior it has. Then describe one interaction in which two of your objects work together to book an appointment.
Hints
- Start with nouns in the scenario: which of them matter to the problem?
- An abstract entity such as a time slot can be an object if it holds information and takes part in behavior.
- Describe the interaction as one object asking another to do part of the work.
Mistakes from Thinking in Tasks
Common mistake Starting the design with a list of jobs and then creating one large procedure for each job
What it looks likeA checkout procedure that reads the book's status, sets the borrower's name, writes the due date, and prints a receipt, all in one place.
Why it failsThe design is shaped by the order of the jobs, so the information each job touches is not owned by any one entity. The source describes this procedure-centred style as the older approach that OOP reverses.
Do this insteadList the entities first, decide what each one holds, and let each job become a behavior of the object that owns the information it needs.
Common mistake Treating shared data as something every procedure may change freely
What it looks likeSeveral functions each update a book's status directly, with no single owner of that status.
Why it failsWhen many tasks change the same data, it becomes hard to see which part of the program is responsible for a given value.
Do this insteadLet the book own its status. Other objects ask the book to change it through a behavior, so the object decides how its own information is updated.
Common mistake Making every noun in the problem into an object
What it looks likeCreating an object for the library's opening hours or for the color of the shelves when the program never uses them.
Why it failsThe source says an entity should become an object only when it matters to the problem.
Do this insteadKeep the entities that matter, the ones that carry information and take part in behavior, and leave out the rest.
Common mistake Forgetting that the objects are meant to work together, so each one is designed in isolation and no interactions are planned
Key Takeaways
- Object-oriented programming models a problem as a set of objects, so the program's structure follows the entities of the problem rather than a sequence of tasks.
- An object has three things: behaviors, information it holds, and interactions with other objects.
- Entities can be real, such as a book, or abstract, such as a due date, as long as they matter to the problem.
- Designing well means identifying the entities first, then deciding what each one holds, what each one can do, and how the objects work together.
- Tasks usually reappear as behaviors of the objects, so the mistake to avoid is letting a task list decide the structure before the entities are known.