Using Prototypes Rather than RTTI

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes