It took me more than a decade of doing object-oriented programming-and then teaching it-before I really started to understand the subtle power of using objects, and how to create behavior through the interaction of objects rather than through program logic. I struggled for many years with how to explain this different way of thinking. It’s easy to teach algorithms or the keywords of the language, but to teach a new way of thinking is a challenge in any field.
So, what is the essence of thinking with objects? It starts by having multiple perspectives is a program.
In procedural programs, we took a single perspective, which was the instruction counter the CPU. In other words, to understand what was going on we traced through our code just like the computer did, albeit much slower. That may seem reasonable and straightforward, and it worked well in many situations, but the problem came when we wanted to make changes to a program.
For example, let’s say you want to travel from point A to point B. There are at least a few ways that you can get guidance. You could get directions-series of steps to follow-and write them down, e.g. first go to the next light then turn left then go three lights down and turn right, etc. Alternatively, you could use a map, and while there are no instructions on how to get from where you are now to where you want to go, you can use the map to figure out how to get there.
Now let’s say you decide that you want to take a detour on your way and stop off for some ice cream. Yum. This may be difficult to do when you have a set of instructions but if you have to map that shows where the ice cream shop is located then you can use it to see how to detour to get there and then get back on track to your final destination. This is not the perfect metaphor for the difference between procedural programming and object-oriented programming, but it does get close.
Object-oriented programming takes many perspectives. Each object in the system represents an entity and each entity has a particular perspective in the system. By having separate entities, each with their own perspective, we can limit the amount of knowledge that each entity has, giving us the flexibility to change things without affecting everything else in the system.
It really wasn’t until I started to study design patterns that I saw how we can create complex behavior through the interaction of objects. Some patterns really taught me the essence of what OO is about, and that’s one of the main benefits I get from studying design patterns. There are many other benefits of studying design patterns. Patterns help us communicate with higher fidelity and understand the essence of a problem quickly. You can implement design patterns in virtually any language including procedural languages, although it is much more difficult in a non-OO language.
While the Gang of Four’s design patterns book is really revolutionary, even today, it is also a mess. There so many little mistakes throughout the text and it seems that there are no common definitions for some of the words they use. However, the big things are so amazingly groundbreaking that it makes up for some of the smaller problems. Also, there are some subtle assumption and certain presuppositions in the book that is also profound. For example, patterns often use polymorphic behavior and this implies for right objects are created prior to their use, and in fact, there’s a whole group of patterns called creational patterns that are involved simply in creating objects. This reflects one of the most fundamental yet misunderstood aspects of OO, to separate the use of objects from the construction of objects.
The great “ah-ha” moments for me came from the realization that this separation of using objects and creating objects has a huge effect on how we design systems that are more resilient to change. More on this in future posts.
{2 Comments }
Previous Post: « Commit Frequently
Next Post: Aggregation or Composition »
The Gang of Four book is no doubt a classic. I found Design Patterns Explained by Allan Shalloway and James Trott to be a very enjoyable and “cleaned up” version of the GoF book.
Hi Marc,
Thank you for your comment and I totally agree. I’ve read Design Patterns Explained several times (both editions) and it’s my favorite book on design patterns. Some people say that’s because Al is my good friend, but I sought him out years ago after reading his book.
I also like Head First Design Patterns by the Freemans. I don’t recommend reading Gang of Four, it’s an amazing book full of great ideas and it did start the pattern revolution but it’s a hard read that could have used another edit pass before publication.
Design patterns taught me what OO is really about.
I had been an OO developer and trainer for two decades before reading Design Patterns Explained but it was really this book that showed me what OO is really about. I highly recommend it.
David.