MSDN Magazine - December 2007 - (Page 113) EEK!—Time to Design the Mouse STANLEY B. LIPPMAN n a series of recent columns, I have been working through the design of a software simulation of a mouse (EEK!) and its environment using the resources of the Microsoft® .NET Framework and C++/CLI, the revised C++ language binding to .NET that was introduced in Visual Studio® 2005. In my last column (msdn.microsoft.com/msdnmag/issues/07/10/nettingC), I looked at how EEK! can be initialized using an XML world description file and the facilities of the System::XML and System::Data namespaces. This time, I’ll show you the beginnings of the actual Mouse class design. I Figure 1 The SentientEntity Abstract Base Class public ref class SentientEntity abstract : ISimulation { public: // virtual void onInit() abstract; virtual void onTick() abstract; virtual void onTerminate() abstract; virtual void onLog( int levelOfDetail ) abstract; }; Where to Begin In EEK!, a mouse is a kind of sentient entity. All of us working in .NET recognize that an is-a relationship is represented using class inheritance, which provides the first type/subtype class relationship I’ll create: public ref class SentientEntity abstract { }; public ref class Mouse : SentientEntity { }; Unlike native C++, under .NET, a class is distinguished by its projected use within your design. Inheritance is supported only by reference classes, indicated in C++/CLI by the contextual keyword, ref, followed by the reserved keyword, class. (Unlike a reserved keyword, which a programmer may never use as an identifier, a contextual keyword is treated as a keyword only in certain program contexts; otherwise, the program is free to use it.) In the case of ref, it is treated as a keyword only if it occurs before the class keyword. A reference class is a garbage-collected, heap-allocated entity manipulated by a named handle. The C++/CLI reserved keyword, gcnew, is used to allocate memory from the .NET managed heap and construct an instance of the reference class: Mouse ^milkyWhite = gcnew Mouse; previous column. Sentient entities are different. The simulation manipulates them through a set of four functions: 1. void onInit performs all the necessary initialization of the sentient entity with the simulated environment, such as binding the mouse’s internal map to the initial world tile where the mouse is released into the simulation. 2. void onTick performs the actual behavior of the sentient entity on each tick of the simulation. The simulator iterates across each sentient entity within the environment, invoking its onTick method in turn. 3. void onTerminate performs all cleanup behavior at the close of the simulation, such as persisting the internal state of the mouse to a database. 4. void onLog provides the sentient entity’s logging service at the indicated level of detail. (Recall that default arguments are not supported in .NET.) In order to generalize these methods, I am going to factor them into a separate interface class: interface class ISimulation { void onInit(); void onTick(); void onTerminate(); void onLog( int levelOfDetail ); }; The hat (^) is a C++/CLI token indicating that milkyWhite is a handle to a reference class object of type Mouse residing on the managed heap. What this is-a relationship between the Mouse and SentientEntity class implies is that I need to design the abstract SentientEntity base class before I tackle the Mouse class. I’ll begin by considering how a SentientEntity functions within the simulation. Sentient Entities All entities within the simulation, other than sentient entities, are fully described within the XML world file, as I described in my Interfaces represent a refinement of the original object-oriented model as supported by C++. They resolve the complexity associated with multiple inheritance. It is considerably more lightweight and simpler to manage. In EEK!’s design, any class—be it a rock or a rodent, a tree or a titmouse—that wishes to be an active participant in the simulation need only implement the ISimulation interface. In fact, that becomes the definition of agency within EEK! Because the SentientEntity class does not represent an actual entity in the simulation, but represents the base class from which all actual entities are derived, it must be declared as abstract and december2007 113 http://msdn.microsoft.com/msdnmag/issues/07/10/nettingC http://msdn.microsoft.com/msdnmag/issues/07/10/nettingC
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.