So far, we have seen simple examples of how the Prototype pattern is implemented. If you thought that the examples were easy, you are in luck--it doesn't get much harder than that. We also discussed a few ways in which an object instance and a virtual constructor can be useful in a game. Now let's look at how the Mach 5 Engine uses the Prototype pattern in the M5Component
and M5Object
classes. Since the M5Object
class uses the M5Component Clone
method, let's look at the components first.
In Chapter 3, Improving on the Decorator Pattern with the Component Object Model, we examined almost all the methods and member variables in the M5Component
class. However, the method we didn't talk about was the Clone
method:
//! M5Component.h class M5Component { public: //! virtual constructor for M5Component, must override virtual M5Component* Clone(void) const = 0; //The rest of the class is the same as before };
As you can see, the M5Component
class implements...