Online Book Reader

Home Category

Professional C__ - Marc Gregoire [479]

By Root 1495 0
Assuming that you go with the first hierarchy, organized around attackers and defenders, you need some way to work movement into the equation. One possibility is that, even though only a portion of the subclasses support movement, you could add a move() method to the Item base class. The default implementation would do nothing. Certain subclasses would override move() to actually change their location on the grid.

The other approach is to write a Movable mix-in class. The elegant hierarchy from Figure 28-1 could be preserved, but certain classes in the hierarchy would subclass Movable in addition to their parent in the diagram. Figure 28-3 shows this design.

FIGURE 28-3

Implementing a Mix-In Class

Writing a mix-in class is no different from writing a normal class. In fact, it’s usually much simpler. Using the earlier war simulation, the Movable mix-in class might look as follows:

class Movable

{

public:

virtual void move() = 0;

};

This Movable mix-in class doesn’t contain any actual functionality. However, it does two very important things. First, it provides a type for Items that can be moved. This allows you to create, for example, an array of all movable items without knowing or caring what actual subclass of Item they belong to. The Movable class also declares that all movable items must implement a method called move(). This way, you could iterate over all of the Movable objects and tell each of them to move.

Using a Mix-In Class

The code for using a mix-in class is syntactically equivalent to multiple inheritance. In addition to subclassing your parent class in the main hierarchy, you also subclass the mix-in class:

class FloatingCastle : public Castle, public Movable

{

public:

virtual void move();

// Other methods and members not shown here

};

The only remaining task is to provide a definition of the move() method for FloatingCastle. Once that is done, you’ll have a class that exists in the most logical place in the hierarchy, but still shares commonality with objects elsewhere in the hierarchy.

OBJECT-ORIENTED FRAMEWORKS


When graphical operating systems first came on the scene in the 1980s, procedural programming was the norm. At the time, writing a GUI application usually involved manipulating complex data structures and passing them to OS-provided functions. For example, to draw a rectangle in a window, you might populate a Window struct with the appropriate information and pass it to a drawRect() function.

As object-oriented programming grew in popularity, programmers looked for a way to apply the OO paradigm to GUI development. The result is known as an Object-Oriented Framework. In general, a framework is a set of classes that are used collectively to provide an object-oriented interface to some underlying functionality. When talking about frameworks, programmers are usually referring to large class libraries that are used for general application development. However, a framework can really represent functionality of any size. If you write a suite of classes that provides database functionality for your application, those classes could be considered a framework.

Working with Frameworks

The defining characteristic of a framework is that it provides its own set of techniques and patterns. Frameworks usually require a bit of learning to get started with because they have their own mental model. Before you can work with a large application framework, such as the Microsoft Foundation Classes (MFC), you need to understand its view of the world.

Frameworks vary greatly in their abstract ideas and in their actual implementation. Many frameworks are built on top of legacy procedural APIs, which may affect various aspects of their design. Other frameworks are written from the ground up with object-oriented design in mind. Some frameworks might ideologically oppose certain aspects of the C++ language, such as the BeOS framework, which consciously shunned the notion of multiple inheritance.

When you start working with a new framework, your first task is to find out what makes it tick. To

Return Main Page Previous Page Next Page

®Online Book Reader