Another interesting aspect to note about object-oriented programming is that while we try our best to mimic things in the real world, some things don t have exact physical counterparts in the real world. Some things are conceptual, or may at first be considered too simple to be broken down into real-world objects.
For example, a sorting algorithm or technique isn t a real world thing but rather more a method. A linked list also isn t a real-world thing, though we might see examples of them or use them programmatically on a daily basis. With a bit of work, we can start to imagine how these things might work in the real world, but it would be in a way that nobody actually does things.
Let s take the linked list example a step further: A linked list is a collection of nodes. Each node contains a reference (or pointer) to the next node as well as some additional data we wish to manage. Now, we would never actually have a linked list node in the real world, but we can picture how one might work. Let s imagine we have a blank fixed-page notebook, where each page contains only a preprinted page number, as in Figure 8-1.
We want to keep track of a person s name and phone number, ordered by their name . Now, in a linked list, the data portion would be the person s name and number; this is what we really are interested in working with. The linked list also has a reference to the next item in the list, which makes everything work as desired. In our example now, the reference is actually another page number representing the next person, but in a programming language it might be an object reference, pointer, or index (into an array).
Let s imagine that our first person to add is Eric Carruthers. We would write down on the first page his name, because he is the first person. Because no other people exists after him, we would also write down a Next and Previous value of 0, meaning there are no more people before or after him. Our book now looks like Figure 8-2.
Next, Rene Winston is added to our book. We write her name on page 2, and we set her Previous value to 1, meaning go to page 1 for the person before. We write 0 for the Next value on her page, as we did for Eric, but we also modify Eric s page so that Next now indicates page 2. Figure 8-3 shows how the book looks now.
Finally, we add Harry Gee. Because our notebook isn t a loose-leaf binder, we can t just reshuffle pages. Instead, we write his name on page 3. We have all our data, but we still need to make the book work, so we write down page 2 as Harry s Next value, and page 1 as Harry s Previous value. We also need to modify Eric Carruthers record and change his Next value to page 3, and we need to change Rene Winston s record so that her Previous value is page 3. The end result is shown in Figure 8-4.
As you can see from this example, a real-world implementation of a linked list would not be a practical solution. In order to find a person, you would have to start at the first page and read it to find out where the page for the next person ( alphabetically ) is, and keep doing that until you find the person you want. However, in computer terms, this is a very straightforward and rapid process (because a computer is performing the task, not a human).
If you find yourself designing classes and find out that the system needs some sort of mechanism that doesn t have a real-world counterpart , don t panic. Just switch to computer world mode and design the objects to make them work as needed. Note that in this section we have discussed something called a linked list , which, due to the fact that we can call it a thing means there s a good chance we can create a class for it.