Professional C__ - Marc Gregoire [48]
Mix-in Classes
Mix-in classes represent another type of relationship between classes. In C++, a mix-in class is implemented syntactically just like multiple inheritance, but the semantics are refreshingly different. A mix-in class answers the question “What else is this class able to do?” and the answer often ends with “-able.” Mix-in classes are a way that you can add functionality to a class without committing to a full is-a relationship. You can think of it as a shares-with relationship.
Going back to the zoo example, you might want to introduce the notion that some animals are “pettable.” That is, there are some animals that visitors to the zoo can pet, presumably without being bitten or mauled. You might want all pettable animals to support the behavior “be pet.” Since pettable animals don’t have anything else in common and you don’t want to break the existing hierarchy you’ve designed, Pettable makes a great mix-in class.
Mix-in classes are used frequently in user interfaces. Instead of saying that a PictureButton class is both an Image and a Button, you might say that it’s an Image that is Clickable. A folder icon on your desktop could be an Image that is Draggable. Software developers tend to make up lots of fun adjectives.
The difference between a mix-in class and a superclass has more to do with how you think about the class than any code difference. In general, mix-in classes are easier to digest than multiple inheritance because they are very limited in scope. The Pettable mix-in class just adds one behavior to any existing class. The Clickable mix-in class might just add “mouse down” and “mouse up” behaviors. Also, mix-in classes rarely have a large hierarchy so there’s no cross-contamination of functionality.
ABSTRACTION
In Chapter 2, you learned about the concept of abstraction — the notion of separating implementation from the means used to access it. Abstraction is a good idea for many reasons explored earlier. It’s also a fundamental part of object-oriented design.
Interface versus Implementation
The key to abstraction is effectively separating the interface from the implementation. Implementation is the code you’re writing to accomplish the task you set out to accomplish. Interface is the way that other people use your code. In C, the header file that describes the functions in a library you’ve written is an interface. In object-oriented programming, the interface to a class is the collection of publicly accessible properties and behaviors. A good interface contains only public behaviors. Properties/variables of a class should never be made public but can be exposed through public behaviors also called getters and setters.
Deciding on an Exposed Interface
The question of how other programmers will interact with your objects comes into play when designing a class. In C++, a class’s properties and behaviors can each be public, protected, or private. Making a property or behavior public means that other code can access it. protected means that other code cannot access the property or behavior but subclasses can access them. private is a stricter control, which means that not only are the properties or behaviors locked for other code, but also even subclasses can’t access them.
Designing the exposed interface is all about choosing what to make public. When working on a large project with other programmers, you should view the exposed interface design as a process.
Consider the Audience
The first step in designing an exposed interface is to consider for whom you are designing it. Is your audience another member of your team? Is this an interface that you will personally be using? Is it something that a programmer external to your company will use? Perhaps a customer or an off-shore contractor? In addition to determining who will be coming to you for help with the interface, this should shed some light