Examples of the clone method in Mach5

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 a pure virtual Clone method just like we saw above in the Shape class. Since the M5Component class is used only as an abstract base class, we don't want to provide any default behaviors for cloning. Cloning only makes sense for derived classes. This part of the component was shown again so we can understand what the interface for overloading this method should be.

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

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