Using Prototypes Rather than RTTI

In some situations, you may want a certain data member of two different objects to be identical. For example, suppose you are writing a graphics program that will render a 3D scene, you need to store material properties (such as reflection coefficients) for different objects in the scene. The simplest way to do this to define an abstract base class called Material that is derived from to define different concrete classes of materials.

 class Material{..   // one or more pure virtual functions};class MaterialType1 : public Material{..  // a concrete class};class MaterialType2 : public Material{..  // a concrete class};

Now objects may contain a pointer to a material object:

 class Object{private:Material *mtrl;..public:void SetMaterial(Material *m) { mtrl = m;}Material *GetMaterial() const { return mtrl; }..};

Suppose you want two objects to have exactly the same material properties, but you don’t want to share the material object between them. The first object’s material is set as follows:

 Object obj1, obj2;obj1.SetMaterial(new MaterialType1);.. // set various material properties

To set the material of obj2 to the same material as of obj1, you can’t use this code:

 obj2.SetMaterial(new Material(obj1.GetMaterial()));

This is because Material is an abstract class and cannot be instantiated with ‘new’. You need to know the dynamic type of obj1’s material. Using run-time type information (RTTI) is an inelegant and inflexible solution:

 if (typeid(obj1.GetMaterial()) == typeid(MaterialType1))  obj2.SetMaterial(new MaterialType1(obj1.GetMaterial());else if (typeid(obj1.GetMaterial() == typeid(MaterialType2))  obj2.SetMaterial(new MaterialType2(obj1.GetMaterial());

Not only does the caller need to know the dynamic type of the material, but if you now add other classes to the Material class hierarchy, the if-statement will need to be changed to accommodate the new types as well. This is clearly not desirable.

A better way is to use the Prototype design pattern. First, define a Clone function for each class in the Material hierarchy:

 class Material{public:virtual Material *Clone() const = 0;..};class MaterialType1 : public Material{public:virtual MaterialType1 *Clone() const{ return new MaterialType1(*this); }..};class MaterialType2 : public Material{public:virtual MaterialType2 *Clone() const{ return new MaterialType2(*this); }..};

To add a new class to this hierarchy, just make sure that it defines the Clone function. To set obj2’s material properties to the same as obj1’s, simply say:

 obj2.SetMaterial(obj1.GetMaterial()->Clone());

The existing material object in obj1 functions as a prototype, replicating itself and returning the result. The caller need never know the dynamic type of the material, and you don’t need to change this code if the Material class hierarchy changes.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job