What is an Object?
Every object has a responsibility; finding the objects in a problem, and the job of each, is the first step in designing a program.
Novice · C# · OOP · Updated
After this lesson you can
- Identify the objects in a real-world system and the responsibility of each
Key takeaways
- Every object has a responsibility: the job it's there to do.
- To find the objects in a problem, read its description. The nouns are often the objects, and the verbs are often their jobs.
- A good object has one clear job you can say in a sentence. When its job takes more than a sentence, it's often two objects.
Before writing any code, take a few minutes to list the things in your kitchen. For each one, ask: why do you have it? How do you use it? What is its job, its responsibility?
| Object | Its responsibility |
|---|---|
| Refrigerator | Keeps food cold |
| Dishwasher | Cleans dishes |
| Cupboard | Stores dishes and glasses |
| Plate | Holds food while you eat |
| Oven | Heats food to a set temperature |
Each object has a role, and no two objects have quite the same one. If you were to model a kitchen in software, you'd create virtual objects that do the same jobs they do in real life.
Finding the objects in a problem
When you design a program, you do the same thing with the problem's description. The nouns are often the objects, and the verbs are often their jobs.
A backyard basketball league has teams. Each team plays games, and the league keeps a standings table that ranks the teams by their winning percentage.
| Object | Its responsibility |
|---|---|
| Team | Knows its name and record; records wins and losses; works out its winning percentage |
| Game | Knows the two teams and the final score; says who won |
| Standings | Holds every team, and lists them in order |
One clear job each
A good object has one clear responsibility, which you can say in a sentence. A refrigerator that also washed the dishes would be a strange appliance, and a Team that also printed web pages would be a strange object. When an object's job takes more than a sentence to describe, it's often two objects.
Check yourself
1 question to try on the lesson page.
So what
Designing with objects starts with words, not code: find the things in the problem, and give each one a single, clear job.