Attributes and Behaviors of Objects
Object-oriented programming designs a program around a set of objects that model the entities in a problem, rather than around a single sequence of tasks.
Where Does a Library Program Begin
Imagine you have been asked to write a program that keeps track of books in a small lending library. Where do you begin? One habit many programmers learn first is to ask what the computer has to do: look up a title, record a loan, print a report. Each of those answers becomes a step, and the program becomes a long chain of steps in a particular order. This is a perfectly workable way to think, and it is how many programs were structured before newer styles became popular.
What do you think happens?
In object-oriented programming, what does the first design question focus on?
Reveal answer
The things that exist in the problem, such as the books, the borrowers, and the loans, rather than the actions the computer must perform.
Object-oriented programming, usually shortened to OOP, starts with the entities in the problem instead of starting with the list of actions. The rest of the topic explains what that shift means and why it matters.
Task-First Versus Object-First Structure
In a task-first design, the program is a single sequence that moves through the work from beginning to end. Its shape is a line of steps, with each step depending on the one before it. In an object-first design, the work is distributed across several objects, and each one is responsible for its own part of the information and behavior. The overall program then emerges from how those objects interact rather than from one long ordered list.
The diagram is a picture of structure rather than a list of steps. In the object-first column, the items are things that exist in the lending library, and the program is described by which objects exist and how they relate. The task-first column is still a valid way to think, but it describes what the computer does in order, not what the library contains.
Tracing a Book Through a Loan
An object holds information about itself. A book object might remember its title, its author, and whether it is currently on loan. Suppose we start with a book that is available on the shelf. Its on-loan attribute says no. When the book is asked to mark itself as borrowed, its state changes, and the on-loan attribute now says yes. The title and author are unchanged, because the behavior only affected the part of the information concerned with availability.
Notice the direction of the change. The behavior is the thing that moves the object from one state to another, and the information stored in the object is what records the new state. A reader of the program can see the book's attributes and the behavior that changes them, without needing to follow a long list of steps.
The Three Traits of an Object
Object-oriented programming designs a program around a set of objects that model the entities in a problem, rather than around a single sequence of tasks. An object has three defining traits: it holds information, it offers behaviors, and it can interact with other objects. Each of these should be considered when designing one.
Following a Borrowing Event
The clearest way to see the object view at work is to follow a single event through the system. Here we trace what happens when a member borrows a book. The question to keep in mind is which object is responsible for each part of the work, and which object sends each request to which.
Tracing the borrowing request through the objects
A member wants to borrow a book from the lending library. Which objects take part, and what does each one do?
- 1The member asks for the book
The member object begins the event. The request is passed to the checkout process, which brings the member and the book together.
- 2The checkout process asks the book
The checkout process does not store the book's status itself. It asks the book object whether it is available.
- 3The book answers
The book object uses its own stored information to reply. If it is not on loan, it can be borrowed.
- 4The book becomes unavailable
The checkout process asks the book to become unavailable. The book updates its own on-loan attribute through its own behavior.
- 5The result is recorded in the book
The book reports that it is now on loan. Each object handled only the part of the work it is responsible for.
The borrowing event is completed by several objects cooperating through requests, with the book keeping control of its own availability.
Real Things and Abstract Ideas as Objects
Not every object in a program has to be a physical thing. A software object can stand for something you could touch, such as a book or a bicycle. It can also stand for something more abstract, such as a loan agreement, a reservation, or a rule about how many books a member may hold at once. What matters is that the object corresponds to some entity in the problem domain, which is the area of the world the program is meant to describe.
- Physical entity in the library: a book, a member
- Abstract entity in the library: a loan policy that limits how many books a member may hold
- Abstract entity in the library: a reservation for a book that is currently on loan
Mistakes When Designing Objects
Common mistake Designing the program as a list of steps and then labeling the steps as objects
What it looks likeCreating objects named LookUpTitle, RecordLoan, and PrintReport, each of which only performs one action.
Why it failsThis keeps the task-first shape. The objects do not correspond to recognizable entities in the library, so the design does not follow the shape of the problem.
Do this insteadStart from the entities that exist in the problem, such as books, members, and loans, and then decide what each one knows and can do.
Common mistake Giving an object information it never uses, or behaviors with no information to work on
What it looks likeA book object that stores its favorite color but has no behavior that reads it.
Why it failsThe three traits are meant to be considered together. Information exists so that behaviors have something to act on.
Do this insteadFor each attribute, ask which behavior uses it. For each behavior, ask which attribute it reads or changes.
Common mistake Assuming that starting from entities guarantees a good design
What it looks likeMaking a separate object for every noun in the problem description without checking whether each one is a meaningful entity.
Why it failsStarting from entities usually produces a design that is easier to understand, but the quality still depends on choosing good objects.
Do this insteadChoose objects that are meaningful in the problem domain, and check that each one has a clear role in the cooperation.
Designing Objects for a Bicycle Rental
A small bicycle rental shop wants a program that tracks its bicycles and the customers who rent them. Identify at least three objects. For each object, list two pieces of information it holds and one behavior it offers. Then describe one event, such as a customer renting a bicycle, and explain which object sends each request to which.
Hints
- Start with the things that exist in the shop, such as bicycles and customers, before thinking about the actions.
- A rental agreement or a rule about how many bicycles a customer may rent at once can also be an object, because it stands for an abstract idea in the problem.
- Ask of each behavior which piece of information it reads or changes, and which other object might ask for it.
Bringing the Objects Together
- Object-oriented programming starts from the entities in a problem, such as books, members, and loans, rather than from one sequence of tasks.
- An object holds information about itself, offers behaviors that work with that information, and can interact with other objects.
- Software objects can stand for physical things or for abstract ideas, as long as each one corresponds to an entity in the problem domain.
- Tracing one event, such as a borrowing request, shows which object is responsible for each part of the work and how the objects cooperate.
Key Takeaways
- Object-oriented programming designs a program around objects that model the entities in a problem rather than around a single sequence of tasks.
- Each object has three defining traits to consider: the information it holds, the behaviors it offers, and its ability to interact with other objects.
- Software objects can represent real things, such as books and members, or abstract things, such as loan policies and reservations.
- Tracing a single event through the objects shows which object is responsible for each part of the work and how they cooperate.