Modeling Real-World Systems

One of the primary goals of object-oriented (OO) programming is to model real-world systems in ways similar to the ways in which people actually think. Designing classes is the object-oriented way to create these models. Rather than using a structured, or top-down, approach, where data and behavior are logically separate entities, the OO approach encapsulates the data and behavior into objects that interact with each other. We no longer think of a problem as a sequence of events or routines operating on separate data files. The elegance of this mindset is that classes literally model real-world objects and how these objects interact with other real-world objects.

These interactions occur in a way similar to the interactions between real-world objects, such as people. Thus, when creating classes, you should design them in a way that represents the true behavior of the object. Let’s use the cabbie example from previous chapters. The Cab class and the Cabbie class model a real-world entity. As illustrated in Figure 5.1, the Cab and the Cabbie objects encapsulate their data and behavior, and they interact through each other’s public interfaces.

Image

Figure 5.1. A cabbie and a cab are real-world objects.

When moving to OO development for the first time, many people tend to still think in a structured way. One of the primary mistakes is to create a class that has behavior but no class data. In effect, they are creating a set of functions or subroutines in the structured model. This is not what you want to do because it doesn’t take advantage of the power of encapsulation.


Note

One of my favorite books pertaining to class design guidelines and suggestions remains Effective C++: 50 Specific Ways to Improve Your Programs and Designs, by Scott Meyers. It offers important information about program design in a very concise manner.


One of the reasons that Effective C++ interests me so much is that because C++ is backward-compatible with C, the compiler allows you to write structured code in C++ without using OO design principles. This is exactly the reason why following guidelines, such as the ones presented in Effective C++, is so important. As I mentioned earlier, during interviews, some people claim that they are OO programmers simply because they program in C++. This indicates a total misunderstanding of what OO design is all about. Thus, you may have to pay more attention to the OO design issues in languages such as C++ and Objective-C as opposed to Java or .NET.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.251.3